Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从多重贴图中删除重复的元素<;int,std::pair<;int,bool>>;?_C++_Containers_Multimap - Fatal编程技术网

C++ 如何从多重贴图中删除重复的元素<;int,std::pair<;int,bool>>;?

C++ 如何从多重贴图中删除重复的元素<;int,std::pair<;int,bool>>;?,c++,containers,multimap,C++,Containers,Multimap,我有一张多重地图,上面有重复的。当我收集完元素后,我想擦除DUP 这是集装箱: std::multimap<int, std::pair<int, bool>> container; 这是个好办法吗 std::pair<int, std::pair<int, bool>> lastValue {-1 , {-1, -1}}; for (auto it = container.cbegin(); it != container.cend(

我有一张多重地图,上面有重复的。当我收集完元素后,我想擦除DUP

这是集装箱:

std::multimap<int, std::pair<int, bool>> container;
这是个好办法吗

std::pair<int, std::pair<int, bool>> lastValue {-1 , {-1, -1}}; 
    for (auto it = container.cbegin(); it != container.cend();)
    {
        if (it->first == lastValue.first && it->second == lastValue.second)
        {
            it = container.erase(it);
        } else
        {
            lastValue = *it;
            ++it;
        }
    }
std::pairlastValue{-1,{-1,-1};
for(auto it=container.cbegin();it!=container.cend();)
{
如果(it->first==lastValue.first&&it->second==lastValue.second)
{
它=容器。擦除(它);
}否则
{
lastValue=*it;
++它;
}
}
这是个好办法吗

std::pair<int, std::pair<int, bool>> lastValue {-1 , {-1, -1}}; 
    for (auto it = container.cbegin(); it != container.cend();)
    {
        if (it->first == lastValue.first && it->second == lastValue.second)
        {
            it = container.erase(it);
        } else
        {
            lastValue = *it;
            ++it;
        }
    }
除非您在multimap中对内部对进行排序,否则这不是一个好的解决方案,因为它会丢失重复项。如果可以更改数据类型,则可以使用:

std::map<int, std::vector<std::pair<int, bool>>>

您可以将
std::pair
operator==
进行比较,您不必像这样手动编写:std::pair lastValue{-1,{-1,-1}?你是说
std::pair>lastValue{-1,{-1,-1};
?使用
std::map
它不会在
中编译,如果
不在
lastValue=*it;
,请更正。是的。我更正了代码。会
std::nextendent\u找到重复的,然后删除它们吗?是的,完全忘记了排序的部分。谢谢sirI无法将我的数据类型更改为:std::m这就是我问这个问题的原因。我会试试。谢谢!
std::set<std::pair<int, bool>> tset;
int lastValue = 0;
for (auto it = container.cbegin(); it != container.cend();)
{
    if( it->first != lastValue ) {
        tset.clear();
        lastValue = it->first;
    }

    if( !tset.insert( it->second ).second )
        it = container.erase( it );
    else
        ++it;
}