1

What I need to do is to combine file names and their sizes in a one container, the file names can repeat and of course the sizes too. And second thing is that I need to sort this container by the sizes co it would be like this after sorting:

1100 -> a.txt
1100 -> a.txt
1200 -> a.txt
1200 -> b.txt

So I can then iterate from smallest to biggest file using for loop. Something similar to arrays in php, but with both values allowed to repeat.

4

1 回答 1

4

Since you want the same as associative arrays with multiple values per key your practically asking for std::multimap.

Example

typedef std::multimap<size_t, std::string> SFMap;
SFMap fileMap;

fileMap.insert(SFMap::value_type(1100,"a.txt"));
fileMap.insert(SFMap::value_type(1100,"a.txt"));
fileMap.insert(SFMap::value_type(1200,"a.txt"));
fileMap.insert(SFMap::value_type(1200,"b.txt"));

for(SFMap::iterator it = fileMap.begin(); it != fileMap.end(); it++){
    std::cout << it->first << " -> " << it->second << "\n";
}
于 2012-12-22T10:49:19.167 回答