What is the best way to create a HashMap string for a string vector in C ++?

Criteria; No need to create copies of objects everywhere. It should be fast, efficient memory and should not create leaks. Must be thread safe.

Ideally, I would like to store pointers to vectors in a HashMap, but I'm worried about memory leaks this way.

Is this the best way?

std::map<std::string, std::auto_ptr<std::vector<std::string> > > adjacencyMap;
+3
source share
7 answers

You are not allowed to store auto_ptrin 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 the assigned types. std::auto_ptrDoes not meet this requirement.

+5
source

A std:: map < > hash_map, - (. http://en.wikipedia.org/wiki/Map_(C%2B%2B))

std:: unordered_map < > ++ 0x std:: tr1:: unordered_map < > non ++ 0x.

Boost .

+4

++ 0x unique_ptr. , boost::shared_ptr.

+1

...

map<string, vector<string> >

, , . .

http://www.sgi.com/tech/stl/Map.html:

" , , . , , , , , ".

, , , - , -place, , ala:

vector<string>& this_one = my_map[my_key];
// work on this_one
+1

...

  • : std::unordered_map, std::tr1::unordered_map boost::unordered_map -. , (. ).
  • : vector, , ++ 0x , , , , , , ... , , , .
  • Thread-Safety: - , , , - ...

: , tbb::concurrent_hash_map container.

  • 1 if (!container.empty())
  • 2 container.clear()
  • 1 { value = container.front(); } // Undefined behavior

, - . , .

, , , STL , , , , , , .

+1

: STL . , , -.

, , -. Intel Threading Building Blocks concurrent_hash_map IIRC, , , .

, , , , -; TBB concurrent_vector, .

0

Source: https://habr.com/ru/post/1761829/


All Articles