Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to create a HashMap of String to Vector of Strings in C++?

Tags:

c++

Criteria, don't want creating copies of objects all over the place. Should be fast, memory efficient and should not create leaks. Should be threadsafe.

Ideally I would want to store pointers to vectors in the HashMap, but I am worried about memory leaks that way.

Is this the best way?

std::map<std::string, std::auto_ptr<std::vector<std::string> > > adjacencyMap;
like image 972
user855 Avatar asked Dec 12 '25 04:12

user855


2 Answers

You're prohibited from storing an auto_ptr in any standard container. §23.1/3: "The type of objects stored in these components must meet the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignable types." std::auto_ptr doesn't meet that requirement.

like image 120
Jerry Coffin Avatar answered Dec 14 '25 19:12

Jerry Coffin


A std::map<> is not implemented as a hash_map, but as a red-black tree (see http://en.wikipedia.org/wiki/Map_(C%2B%2B))

You can use std::unordered_map<> for c++0x compilers or std::tr1::unordered_map<> for non c++0x compilers.

Boost also has its version.

like image 44
Edison Gustavo Muenz Avatar answered Dec 14 '25 18:12

Edison Gustavo Muenz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!