c++ - How to make my "own" templated map? -


i want implement own "simple" container have map properties keeps insertion order. i've heard boost::multi_index find difficult understand want.

so made templated class:

template<typename key, typename value> class mymap {     private :          std::vector<key>    m_keys;         std::vector<value>  m_values;      public :         void insert(key& key, value& val) {             //test if key exists //             m_keys.push_back(key);             m_values.push_back(val);         }          /* other methods erase/size/operator[]/begin/etc. */  }; 

just test it, wanted this:

int main() {     mymap<string,int> m;     m.insert("test",1);     m.insert("cat",2);      for(auto& item : m) {         cout << item << endl;         cout << m[item] << endl;     } } 

but keep getting compilation error on inserts (and [ ]) translates key basic_string , not string. it's driving me crazy , can't find answer (or word describe problem research answer). guess has allocators can't manage understand how fix it. how can make map conversion stays general need other (own-implemented) classes?

edit : after solving "string" problem, had problems when passing int because waiting &int. followed kebs advice , implemented vector> instead , got rid of conversion problems... :)

you can't build reference const char* (even though gets casted string), try instead:

    template<typename key, typename value>     void insert(key key, value val) {         m_keys.push_back(key);         m_values.push_back(val);     } 

more precisely, compiler pretty clear problem:

error: invalid initialization of non-const reference of type 'std::basic_string&' rvalue of type 'std::basic_string' m.insert("test",1);


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -