Std :: multimap gets two ranges

I am using C ++ std::multimapand I need to iterate over two different keys. Is there an effective way to do this, besides creating two ranges and cycling through these ranges separately?

Here's how to do it now:

std::pair<std::multimap<String, Object*>::iterator,std::multimap<String, Object*>::iterator> range;
std::pair<std::multimap<String, Object*>::iterator,std::multimap<String, Object*>::iterator> range2;

// get the range of String key
range = multimap.equal_range(key1);
range2 = multimap.equal_range(key2);

for (std::multimap<String, Object*>::iterator it = range.first; it != range.second; ++it)
{
    ...
}
for (std::multimap<String, Object*>::iterator it2 = range2.first; it2 != range2.second; ++it2)
{
    ...
}
+5
source share
3 answers

The code you started with is the simplest.

, , , , , . , , , , .

: ; .

for (std::multimap<String, Object*>::iterator it = range.first; it != range2.second; ++it)
{
    if (it == range.second)
    {
        it = range2.first;
        if (it == range2.second)
            break;
    }
    ...
}
+3

Boost , . Boost.Range join , . . : .

+3

++ - 11 (Visual Studio 10+, gcc-4.5 +) auto - :

// get the range of String key
auto range = multimap.equal_range(key1);
auto range2 = multimap.equal_range(key2);

for (auto it = range.first; it != range.second; ++it)
{
    ...
}
for (auto it2 = range2.first; it2 != range2.second; ++it2)
{
    ...
}

, , key2!= key1. .

std:: set_difference . , std:: set_union back_inserter , ?

. . , . , , / , , .

0
source

All Articles