#include <cstring>
#include <cstdio>
#include<map>
#include<memory>
#include <iostream>
#include<string>
using namespace std;
struct Comparator : public std::binary_function<shared_ptr<string>, shared_ptr<string>, bool>{
bool operator()(shared_ptr<string> const & left, shared_ptr<string> const& right) const{
return left->compare(*right)<0;//The comparer should return whether the first string should go before the second string. Either return x.compare(y) < 0
}
};
int main(){
map<shared_ptr<string>,int,Comparator> test;
test.insert(make_pair(make_shared<string>("test1"),123));
test.insert(make_pair(make_shared<string>("test2"),234));
test.insert(make_pair(make_shared<string>("test3"),369));
test.insert(make_pair(make_shared<string>("test4"),258));
//test.insert(make_pair(make_shared<string>("test4"),99));
// cout<<"test:"<<test[make_shared<string>("test5")]<<endl;//carefull
cout<<"size:"<<test.size()<<endl;
shared_ptr<string> key=make_shared<string>("test4");
int count = test.count(key);
if(count==1){
map<shared_ptr<string>,int,Comparator>::iterator it = test.find(key);
cout<<"value:"<<it->second<<endl;
}
return 0;
}
c++中std::map key使用智能指针笔记
最新推荐文章于 2024-08-30 19:45:11 发布