简体   繁体   English

c ++ map中的元素类型

[英]Type of elements in c++ map

I heard in this lecture about C++ associative containers that if you have a map like: 我在讲座中听说过C ++关联容器,如果你有一个地图,如:

map<string,string> m;

its elements are of type: 其元素类型:

pair<const string,string>

I have a little class representing phone number lists 我有一个小班级代表电话号码列表

PhoneList operator+(const PhoneList & g){
      PhoneList copy(*this);
      for(map<string,string>::const_iterator it = g.datos.begin(); it != g.datos.end(); ++it){
        copy.insert(*it);
      }
      return copy;
}

the problem is that insert method has the following header: 问题是insert方法有以下标题:

 pair<map<string,string>::iterator,bool>  insert(pair<string,string> p)

so apparently I'm converting a pair into a pair. 显然我正在将一对变成一对。

I would just like to know why this works. 我想知道为什么会这样。 Is there a conversion between const string and string? const字符串和字符串之间是否有转换?

There are two reasons why your code works. 您的代码有两个原因。 First, because you're passing insert ()'s parameters by value: 首先,因为你按值传递insert ()的参数:

pair<map<string,string>::iterator,bool>  insert(pair<string,string> p)

If you were passing the parameter by reference instead, your code would like fail to compile: 如果您通过引用传递参数,那么您的代码将无法编译:

pair<map<string,string>::iterator,bool>  insert(const pair<string,string> &p)

Passing non-trivial parameters by value is generally less efficient, but because here you are passing this parameter by value, the parameter gets, essentially, copy-constructed. 按值传递非平凡参数通常效率较低,但由于此处您通过值传递此参数,因此参数基本上是复制构造的。 That's the second reason. 这是第二个原因。 You end up using the following std::pair template constructor: 您最终使用以下std::pair模板构造函数:

template<class U, class V> pair(const pair<U, V>& p);

Requires: is_constructible<first_type, const U&>::value is true and
is_constructible<second_type, const V&>::value is true.

Effects: Initializes members from the corresponding members of the
argument.

And that's why you are able to compile your code. 这就是为什么你能够编译你的代码。 To summarize, this template constructor allows std::pair<A, B> to be constructed from std::pair<C, D> , different classes, if A can be constructed from C, and B can be constructed from D. In your case A, B, and D are std::string , and C is a const std::string . 总而言之,这个模板构造函数允许std::pair<A, B>std::pair<C, D>构造,不同的类,如果A可以用C构造,B可以从D构造。你的情况A,B和D是std::string ,C是const std::string And there's obviously not an issue with constructing a new std::string from some other std::string , it's an ordinary copy-constructor. 从其他std::string构造一个新的std::string显然没有问题,它是一个普通的拷贝构造函数。

The reason the elements are of type 元素属于类型的原因

pair<const string,string>

is to disallow you from changing the key in the map. 是禁止您更改地图中的键。 When you insert a key-value pair into the map, the key is used to choose where in the internal data structure that the key-value pair should go. 将键值对插入映射时,该键用于选择键值对应在内部数据结构中的位置。 STL just doesn't let you change the key because then it would have to delete that element and reinsert it again with a different key. STL只是不允许您更改密钥,因为它必须删除该元素并使用不同的密钥重新插入它。 Sure they could have done this, but they didn't. 当然他们可以做到这一点,但他们没有。 You are able to change the value all you want, but the key will remain the same as it was originally made. 您可以根据需要更改所有值,但密钥将保持与原始密钥相同。 You could always do this yourself if you needed to and copy the value, create a new key-value pair with the new key and the copied value, then delete the old key-value pair. 如果您需要复制值,使用新密钥和复制的值创建新的键值对,则可以自己执行此操作,然后删除旧的键值对。

Also, you can simplify your code by using the range-based insert. 此外,您可以使用基于范围的插入来简化代码。

template <class InputIterator>
void insert (InputIterator first, InputIterator last);

This will just make your code look cleaner and why not take advantage of functions that already exist for you? 这只会让您的代码看起来更清晰,为什么不利用已经存在的功能?

References: http://www.cplusplus.com/reference/map/map/insert/ 参考文献: http//www.cplusplus.com/reference/map/map/insert/

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

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