How to make a static map in c++? -


i working on 2 files, lets take file1.cpp , file2.cpp. file1.cpp contains:

//file 1 #include <iostream> #include <map>  struct category {     int id; };  void fun();  std::map<char, category> mymap1; static std::map<char, category> mymap; std::map<char, category>::iterator map_iter; std::map<char, category>::iterator map_iter1;  int main () {      mymap1['a'] = {20};     mymap1['b'] = {30};     mymap1['c'] = {40};      for(int = 0;i < 4; i++)         fun();      return 0; }  //file 2 #include<file2.h> void fun() {      mymap = mymap1;      map_iter = mymap.begin();      (map_iter1 = mymap1.begin(); map_iter1 != mymap1.end();++map_iter1)      {         map_iter->second.id = map_iter1->second.id - map_iter->second.id;         std::cout<<map_iter1->second.id<<"  " <<map_iter->second.id;         map_iter->second.id=map_iter1->second.id;         ++map_iter;      }  } 

i trying initialize mymap once when loop run first time after initialization should not happen happen in static variable.

but behaviour not shown , each time mymap getting initialize.

i don't want "id" getting initialize every time.

how work?

create function returns static map

static std::map<char, category> maps() {     static std::map<char, category> m;     //...     return m; } 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -