How to insert a list of pairs most efficiently?

Let's say I have

(A,B)
(A,C)
(A,D)
(B,C)
(B,D)
(C,D)
(D,E)

in a text file. I will extract it using regular expressions.

I would like to insert data into a container so that it looks like this.

A->B,C,D
B->C,D
C->D
D->E

Which container do I use?

I need to be able to search for data on both the left and right sides of the container, that is, by the value of the key. Therefore, I need to be able to search / search

A, B, C, D in

A->B,C
B->C,D
C->D
D->E

and b, c in

A->B,C
+3
source share
2 answers

std :: multimap comes to mind ... Basically this is a map, but allows duplicating maps in a key (i.e. you can have several "A" keys, each of which is displayed on "B", "C" or "D "to continue your example).

: : multimap , , - std::pair, , insert, multimap:

std::multimap<char, char> myMap;
myMap.insert(std::pair<char, char>('A', 'B'));
myMap.insert(std::pair<char, char>('A', 'C'));
myMap.insert(std::pair<char, char>('A', 'D'));
myMap.insert(std::pair<char, char>('B', 'C'));
// ... etc

, , A, B, C .. stand-ins - . stand-ins, .

multimap "A" :

typedef std::multimap<char, char>::iterator mmIter; // For brevity...
std::pair<mmIter, mmIter> iters = myMap.equal_range('A');
// Iterate over values for key
for (mmIter iter = iters.first ; iter != iters.second; ++iter)
{
    // Print out value.
    cout << " " << (*iter).second;
}

multimap.equal_range pair, , , , , . , , .

EDIT 2 , . Multimap (.. , ), - . , , .

, Boost, ( - ) t Boost, , - ).

+6

, , "" , , .

- std::map<keytype, std::vector<valuetype> >.

+2

All Articles