如何在C ++中破坏地图并填充地图指针?(How do I destruct a map and populate a map pointer in C++?)

编程入门 行业动态 更新时间:2024-10-27 19:24:19
如何在C ++中破坏地图并填充地图指针?(How do I destruct a map and populate a map pointer in C++?)

如果我们创建一个map<int,int> ,我们可以清除它但它仍然保留在内存中,对吧? 例如

#include <map> using namespace std; int main(){ map<int,int> myMap; myMap[1] = 2; myMap.clear(); return 0; }

但是如果我们设置一个指针而不是实际的map ,我可以用delete来破坏它,但是我无法以相同的方式填充地图:

#include <map> using namespace std; int main(){ map<int,int> *myMap = new map<int,int>; // myMap[1] = 2; delete myMap; return 0; }

取消注释myMap[1] = 2; 行结束时出现错误:

alvas @ ubi:〜$ g ++ test.cpp test.cpp:在函数'int main()'中:test.cpp:8:14:错误:'operator ='不匹配(操作数类型是'std :: map'和'int')myMap [1] = 2; ^在/ usr / include / c ++ / 5 / map:61:0中包含的文件,来自test.cpp:2:/usr/include/c++/5/bits/stl_map.h:296:7:注意:候选人: std :: map <_Key,_Tp,_Compare,_ Alloc>&std :: map <_Key,_Tp,_Compare,_Alloc> :: operator =(const std :: map <_Key,_Tp,_Compare,_Alloc>&)[with _Key = int; _Tp = int; _Compare = std :: less; _Alloc = std :: allocator>] operator =(const map&__ x)^ /usr/include/c++/5/bits/stl_map.h:296:7:注意:参数1从'int'到'const'没有已知的转换的std ::地图及”

如何在C ++中破坏map ? 它是“破坏性的”吗?

另外,如何初始化/填充map<>指针的值?

If we create a map<int,int>, we can clear it but it still stays in memory, right? E.g.

#include <map> using namespace std; int main(){ map<int,int> myMap; myMap[1] = 2; myMap.clear(); return 0; }

But if we set a pointer instead of the actual map, I could destruct it with delete, but I couldn't populate the map the same way:

#include <map> using namespace std; int main(){ map<int,int> *myMap = new map<int,int>; // myMap[1] = 2; delete myMap; return 0; }

Uncommenting the myMap[1] = 2; line ends up with the error:

alvas@ubi:~$ g++ test.cpp test.cpp: In function ‘int main()’: test.cpp:8:14: error: no match for ‘operator=’ (operand types are ‘std::map’ and ‘int’) myMap[1] = 2; ^ In file included from /usr/include/c++/5/map:61:0, from test.cpp:2: /usr/include/c++/5/bits/stl_map.h:296:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = int; _Tp = int; _Compare = std::less; _Alloc = std::allocator >] operator=(const map& __x) ^ /usr/include/c++/5/bits/stl_map.h:296:7: note: no known conversion for argument 1 from ‘int’ to ‘const std::map&’

How do I destruct a map in C++? Is it "destructable"?

Also, How do I initialize/populate the values of a map<> pointer?

最满意答案

如何在C ++中破坏地图?

当您将其声明为对象(第一种情况)时,它会在超出范围时被销毁。

当声明为指针时,如果使用new ,则delete关键字会起作用。 如果使用智能指针,则无需显式调用delete 。

如何初始化/填充map <>指针的值?

使用insert()方法,如下所示:

#include <map> using namespace std; int main(){ map<int,int> *myMap = new map<int,int>; myMap->insert(make_pair<int, int>(1, 2)); delete myMap; return 0; }

或者,正如@Michael所建议的那样,你可以使用(*myMap)[1] = 2; 但我更喜欢使用API​​。

注意:正如注释中所指出的, std::map operator[]和insert()在行为方面并不相似。 只是在这个最小的例子中,它们并没有反映任何差异。

How do I destruct a map in C++?

When you declare it as an object (first case), it gets destroyed when it goes out of scope.

When declared as a pointer, delete keyword does the trick if new is used. If smart pointers are used, there's no need to explicitly call delete.

How do I initialize/populate the values of a map<> pointer?

Use the insert() method, like this:

#include <map> using namespace std; int main(){ map<int,int> *myMap = new map<int,int>; myMap->insert(make_pair<int, int>(1, 2)); delete myMap; return 0; }

Or, as suggested by @Michael, you can use (*myMap)[1] = 2; But I prefer using the API.

NOTE: As pointed out in the comments, operator[] and insert() for a std::map aren't similar in terms of behavior. It's just that, in this minimal example, they don't reflect any difference.

更多推荐

本文发布于:2023-08-03 00:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382523.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:地图   指针   如何在   destruct   populate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!