std::map和 std::map之间有区别吗? 和 std::map

发布于 2024-07-30 06:21:44 字数 166 浏览 3 评论 0原文

据我了解, std::map 中值对中的键一旦插入就无法更改。 这是否意味着使用关键模板参数作为 const 创建映射没有效果?

std::map<int, int> map1;
std::map<const int, int> map2;

From what I understand, the key in a value pair in an std::map cannot be changed once inserted. Does this mean that creating a map with the key template argument as const has no effect?

std::map<int, int> map1;
std::map<const int, int> map2;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云雾 2024-08-06 06:21:44

std::map 无论如何都会配置其键类型:std::map::value_typestd::pair;。 如果将 const 添加到键类型,const const int 将简单地折叠为 const int

std::map constifies its key type anyway: std::map<int, int>::value_type is std::pair<const int, int>. If you add a const to the key type, const const int will simply collapse to const int.

幸福丶如此 2024-08-06 06:21:44

正如 Dewfy 所说,对于您给出的示例,这并不重要,因为 int 是内置类型,它将按值复制,但是对于 char* ,情况有点不同......

如果您有

std::map<char *, int> map;

那么您将无法插入声明为 const char* 的变量将失败,

char * y = new char[4];
const char * x = "asdf";
std::map<char *, int> map;
map.insert(make_pair(y, 4)); //ok
map.insert(make_pair(x, 4)); //fail

并显示 a

std::map<char*, int> map;

实际上可以说,

char * x = new char[1];
(*x) = 'a';
map<char*,int>::iterator it = map.begin();
cout<<it->first; //prints 'a'
(it->first)[0] = 'x'
cout<<it->first; //prints 'x'

 std::map<const char *, int>

您将被限制使用

 map<const char*, int>::iterator 

As Dewfy said, with the example you gave, it doesn't matter since int is a built in type and it will be copied by value, but with char* it's a little different...

If you had

std::map<char *, int> map;

Then you can't insert a variable declared as const char* will fail

char * y = new char[4];
const char * x = "asdf";
std::map<char *, int> map;
map.insert(make_pair(y, 4)); //ok
map.insert(make_pair(x, 4)); //fail

with a

std::map<char*, int> map;

you can actually say

char * x = new char[1];
(*x) = 'a';
map<char*,int>::iterator it = map.begin();
cout<<it->first; //prints 'a'
(it->first)[0] = 'x'
cout<<it->first; //prints 'x'

with a

 std::map<const char *, int>

you'll be restricted to using

 map<const char*, int>::iterator 
活泼老夫 2024-08-06 06:21:44

由于 int 是按值复制的,因此 const 声明没有任何意义。
另一方面

std::map<const char*, int> map2; 

极大地改变了画面

since int is copied by value this declaration of const has no sense.
On other hand

std::map<const char*, int> map2; 

dramatically changes a picture

岁月无声 2024-08-06 06:21:44

你标题问题的答案是肯定的。 它们是有区别的。 您无法将 std::map 传递给采用 std::map 的函数。

然而,尽管地图的类型不同,但它们的功能行为是相同的。 这并不罕见。 在许多情况下,int 和 long 的行为相同,尽管它们在形式上是不同的类型。

The answer to your title question is yes. There is a difference. You cannot pass a std::map<int, int> to a function that takes a std::map<const int, int>.

However, the functional behavior of the maps is identical, even though they're different types. This is not unusual. In many contexts, int and long behave the same, even though they're formally different types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文