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 528 views
528 views asked Apr 13, 2020 by avibootz
1 answer 165 views
165 views asked May 6, 2018 by avibootz
2 answers 230 views
3 answers 213 views
213 views asked Oct 11, 2022 by avibootz
1 answer 143 views
143 views asked Feb 23, 2022 by avibootz
1 answer 134 views
134 views asked Feb 23, 2022 by avibootz
...