#include <iostream>
#include <map>
using namespace std;
struct MyStruct
{
int i;
char c;
MyStruct(int i, char c)
{
this->i = i;
this->c = c;
}
};
int main()
{
MyStruct* pst1 = new MyStruct(1, 'a');
MyStruct* pst2 = new MyStruct(1, 'a');
MyStruct* pst3 = new MyStruct(3, 'c');
map<int, MyStruct*> map1; // 其中的value为指针型的
map1.insert(pair<int, MyStruct*>(10, pst1));
map1.insert(pair<int, MyStruct*>(11, pst2));
map1.insert(pair<int, MyStruct*>(12, pst3));
map<int, MyStruct*>::iterator iter = map1.find(11);
cout << iter->second->i << ' ' << iter->second << endl;
cout << iter->second->i << ' ' << iter->second->c << endl;
MyStruct* pst = iter->second;
pst->c = 'b';
iter = map1.find(11)
stl map 以指针作为value
最新推荐文章于 2022-12-31 21:42:41 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)