To initialize global variable std::map<int, int>

Asked 2 years ago, Updated 2 years ago, 81 views

If map is a global variable, how do I initialize it? Should I write static function?

c++ stdmap

2022-09-22 22:34

1 Answers

#include <map>
using namespace std;

map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
map<int,int> create_map()
{
    map<int,int> m;
    m[1] = 2;
    m[3] = 4;
    m[5] = 6;
    return m;
}
map<int, int> mymap = create_map();
#include <map>
#include "boost/assign.hpp"
using namespace std;
using namespace boost::assign;

map<int, char> m = map_list_of (1, 'a') (3, 'b') (5, 'c') (7, 'd');


2022-09-22 22:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.