源码

#include <iostream>
#include <memory>
#include <map>
#include <string>
#include <vector>
#include <utility>
using namespace std;

class message{
public:
int num;
string ptr;
vector<string> vue;
};

int main()
{
map<std::string,shared_ptr<message>> test;
pair<std::string,shared_ptr<message>> ptr;
auto tk = make_shared<message>();
tk->num = 1;
tk->ptr = "hello";
tk->vue.emplace_back(move(string("love")));
ptr = make_pair(string("haha"),tk);
test.emplace(ptr);

for(auto iter = test.begin();iter!=test.end();++iter)
{
cout << iter->first << endl;
shared_ptr<message> p = move(iter->second);
cout << p->num << endl;
cout << p->ptr << endl;
for(auto it : p->vue)
{
cout << it << endl;
}
}
return 0;
}

Run

C++ map中使用pair构造键值对小记_c++