简体   繁体   English

map :: erase:通过键或迭代器擦除之间的区别?

[英]map::erase: difference between erase by key or by iterator?

If I want to remove a single element from a map (and I don't care about the return value apart from possibly error checking), there are two ways that I could it: erase by key or erase by value: 如果我想从地图中删除一个元素(和我不关心的返回值除了可能的错误检查功能),有两种方法,我可以是: erase通过按键或erase的价值:

http://ideone.com/YWocN7 http://ideone.com/YWocN7

#include <iostream>
#include <map>
using namespace std;

void printMap(const std::map<int, double>& m)
{
    for (auto kv : m)
    {
        std::cout << '{' << kv.first << ", " << kv.second << '}';
    }
    std::cout << '\n';
}

int main() {
    std::cout << "erase by iterator:\n";
    std::map<int, double> m1 = { { 1, 1.1 }, { 2, 2.2 }, { 3, 3.3 } };
    printMap(m1);
    m1.erase(m1.find(2));
    printMap(m1);

    std::cout << "erase by key:\n";
    std::map<int, double> m2 = { { 1, 1.1 }, { 2, 2.2 }, { 3, 3.3 } };
    printMap(m2);
    m2.erase(2);
    printMap(m2);
    return 0;
}

Output: 输出:

erase by iterator:
{1, 1.1}{2, 2.2}{3, 3.3}
{1, 1.1}{3, 3.3}
erase by key:
{1, 1.1}{2, 2.2}{3, 3.3}
{1, 1.1}{3, 3.3}

Are the two methods entirely equivalent , or is there any practical reason or situation where I might prefer one over the other ? 这两种方法是完全等效 吗?还是有任何实际的原因或情况让我更喜欢一种方法

In the scenario you describe ( m1.erase(2); vs. m1.erase(m1.find(2)); ), the two methods are should be entirely equivalent, give or take the cost of creating and returning iterators, though this depends on your STL implementation. 在您描述的场景( m1.erase(2); vs m1.erase(m1.find(2)); ))中,这两种方法应该完全等效,尽管要付出或付出创建和返回迭代器的代价这取决于您的STL实现。
The point of erase by iterator is to remove an entry from the key, when you already have an iterator because of other operations your program needed to execute on the element this iterator refers to. 当您已经有一个迭代器时,由于需要在该迭代器引用的元素上执行程序的其他操作erase by iterator的目的是从键中删除一个条目。 For instance: 例如:

void processEntry(const std::pair<int, double>& p) {
     // do something like maybe writing it to a file
}

std::map<int, double> m1 = { { 1, 1.1 }, { 2, 2.2 }, { 3, 3.3 } };

const auto it = std::find_if(m1.begin(), m1.end(), [](const std::pair<int, double>& p) {
    return p.first > 1 && p.second < 3.0;
});

if (it != m1.end()) {
    processEntry(*it);
    m1.erase(it);
}

Map erase supports erasing elemnt(s) in different ways as below 地图擦除支持通过以下不同方式擦除元素

void erase( iterator pos );
iterator erase( const_iterator pos );
void erase( iterator first, iterator last );
iterator erase( const_iterator first, const_iterator last );        
size_type erase( const key_type& key );

In you case you are using erase( iterator) and erase( const key_type) even though result of both the operation is same how the operation is executed might be different. 在这种情况下,即使两个操作的结果相同,但您使用的是Erase(迭代器)和Ease(const key_type),但执行方式可能不同。

erase( const key_type& key ); will find the key and then erase it. 将找到密钥,然后将其擦除。

erase( iterator) already has the position which wants to erase the element erase( iterator)已经具有要删除元素的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM