How to initialize map with array in C++

2 Answers

0 votes
#include <iostream>
#include <map>
  
int main ()
{
    std::map<std::string, std::array<float, 3>> mp {
        {"ab", std::array<float, 3>{34, 567, 9727}},
        {"cd", std::array<float, 3>{2, 46, 999}}
    };
 
    std::cout << mp["ab"][0] << " " << mp["cd"][1] << " " << std::endl;
 
    return 0;
}
    
    
    
/*
run:
    
34 46 
    
*/

 



answered Apr 14, 2020 by avibootz
edited Apr 14, 2020 by avibootz
0 votes
#include <iostream>
#include <map>
 
int main ()
{
    std::map<std::string, std::array<float, 3>> mp {
        {"ab", {34, 567, 9727}},
        {"cd", {2, 46, 999}}, 
        {"xy", {1, 8, 3}}
    };

    std::cout << mp["ab"][0] << " " << mp["cd"][1] << " " << mp["xy"][2] << '\n';

    return 0;
}
   
   
   
/*
run:
   
34 46 3
   
*/

 



answered Apr 14, 2020 by avibootz
edited Apr 14, 2020 by avibootz

Related questions

2 answers 491 views
491 views asked Apr 13, 2020 by avibootz
1 answer 140 views
140 views asked May 6, 2018 by avibootz
2 answers 192 views
3 answers 154 views
154 views asked Oct 11, 2022 by avibootz
1 answer 120 views
120 views asked Feb 23, 2022 by avibootz
1 answer 112 views
112 views asked Feb 23, 2022 by avibootz
...