//unordered_map的方法记录
#include <iostream>
#include <unordered_map>
#include <map>
using namespace std;
int main()
{
unordered_map<int,string> test;//声明
test.insert(make_pair(1,"hello"));//插入元素
test.insert(make_pair(13,"hello13"));
test.insert(make_pair(14,"hello14"));
test.insert(make_pair(15,"hello15"));
//pair形式的insert,可以有pair的返回值,first是相应map的迭代器指针,指向这个元素,second是bool,插入成功与否
pair<unordered_map<int,string>::iterator,bool> return_pair;
return_pair = test.insert(make_pair(2,"hello2"));
cout<<"insert data="<<return_pair.first->second<<endl;
cout<<"insert2="<<return_pair.second<<endl;
//再次插入相同键值的时候,不能插入成功,不做操作
return_pair = test.insert(make_pair(2,"lalala"));
cout<<"insert2 again="<<return_pair.second<<endl;
cout<<"insert data="<<return_pair.first->second<<endl;
//也可以以数组方式插入
test[3] = "hello3";
unordered_map<int,string>::iterator it;//迭代器
//迭代器查找元素
it = test.find(3);
if(it!=test.end())
cout<<"find success"<<it->second<<endl;
else
cout<<"find failed"<<endl;
if(test.empty()) cout<<"empty"<<endl;//检查是否为空
else cout<<"not empty"<<endl;
//删除指定元素
it = test.find(3);//迭代器找到再删除
test.erase(it);
int n = test.erase(2);//删除了返回1,没删除成功返回0
cout<<"erase="<<n<<endl;
n = test.erase(2);
cout<<"erase="<<n<<endl;
test.insert(make_pair(19,"hello19"));
test.insert(make_pair(20,"hello20"));
test.insert(make_pair(56,"hello56"));
test.insert(make_pair(19,"hello19"));
//元素遍历
for(it = test.begin();it!=test.end();it++) cout<<"key="<<it->first<<" value="<<it->second<<endl;
//元素全部删除
test.clear();
test.erase(test.begin(),test.end());
return 0;
}
//map(红黑树)方法记录
#include <iostream>
#include <unordered_map>
#include <map>
using namespace std;
int main()
{
map<int,string> test_map;//声明
test_map.insert(make_pair(1,"hello"));//插入元素
test_map.insert(make_pair(13,"hello13"));
test_map.insert(make_pair(14,"hello14"));
test_map.insert(make_pair(15,"hello15"));
//pair形式的insert,可以有pair的返回值,first是相应map的迭代器指针,指向这个元素,second是bool,插入成功与否
pair<map<int,string>::iterator,bool> return_pair_map;
return_pair_map = test_map.insert(make_pair(2,"hello2"));
cout<<"insert data="<<return_pair_map.first->second<<endl;
cout<<"insert2="<<return_pair_map.second<<endl;
//再次插入相同键值的时候,不能插入成功,不做操作
return_pair_map = test_map.insert(make_pair(2,"lalala"));
cout<<"insert2 again="<<return_pair_map.second<<endl;
cout<<"insert data="<<return_pair_map.first->second<<endl;
//也可以以数组方式插入
test_map[3] = "hello3";
map<int,string>::iterator it;//迭代器
//迭代器查找元素
it = test_map.find(3);
if(it!=test_map.end())
cout<<"find success"<<it->second<<endl;
else
cout<<"find failed"<<endl;
if(test_map.empty()) cout<<"empty"<<endl;//检查是否为空
else cout<<"not empty"<<endl;
//删除指定元素
it = test_map.find(3);//迭代器找到再删除
test_map.erase(it);
int n = test_map.erase(2);//删除了返回1,没删除成功返回0
cout<<"erase="<<n<<endl;
n = test_map.erase(2);
cout<<"erase="<<n<<endl;
test_map.insert(make_pair(10,"hello10"));
test_map.insert(make_pair(90,"hello90"));
test_map.insert(make_pair(50,"hello50"));
//元素遍历
for(it = test_map.begin();it!=test_map.end();it++) cout<<"key="<<it->first<<" value="<<it->second<<endl;
//反向迭代器
map<int,string>::reverse_iterator rit;
for(rit = test_map.rbegin();rit!=test_map.rend();rit++) cout<<"key="<<rit->first<<" value="<<rit->second<<endl;
//元素全部删除
test_map.clear();
test_map.erase(test_map.begin(),test_map.end());
return 0;
}