目录
1、set的使用编辑
1、初始化
2、遍历
3、查找编辑
4、插入编辑
5、不支持修改与下标
2、map的使用
1、初始化编辑
2、遍历
3、map的下标(重点)
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <utility>
#include <string>using std::cout;
using std::endl;
using std::vector;
using std::set;
using std::map;
using std::pair;
using std::string;void test()
{/* vector<int> vec; *//* vec.push_back(1); *///set的基本特征//1、元素是唯一的,不能重复//2、默认情况下,元素会按照从小到大的顺序进行排列//3、set的底层使用的是红黑树结构////初始化set<int> number2;set<int> number = {1, 2, 5, 7, 9, 8, 5, 3, 2};//迭代器(看成是一个指针)//遍历set<int>::iterator it;for(it = number.begin(); it != number.end(); ++it){cout << *it << " ";}cout << endl;//auto可以让编译器自动推导变量的类型/* auto a = 10; *//* auto s1 = "hello"; *//* auto b;//error */for(auto &elem : number){cout << elem << " ";}cout << endl;cout << endl << "set的查找" << endl;size_t cnt1 = number.count(7);size_t cnt2 = number.count(10);cout << "cnt1 = " << cnt1 << endl;cout << "cnt2 = " << cnt2 << endl;/* auto it2 = number.find(3); */set<int>::iterator it2 = number.find(3);if(it2 == number.end())//迭代器判空{cout << "该元素不存在" << endl;}else{cout << "该元素存在, *it2 = " << *it2 << endl;}cout << endl << "set的插入操作" << endl;pair<set<int>::iterator, bool> ret = number.insert(6);if(ret.second){cout << "插入成功 : " << *ret.first << endl;}else{cout << "插入失败" << endl;}for(auto &elem : number){cout << elem << " ";}cout << endl;cout << endl << endl;vector<int> vec = {1, 4, 20, 10, 7, 0};number.insert(vec.begin(), vec.end());for(auto &elem : number){cout << elem << " ";}cout << endl;/* cout << endl << endl; *///set不支持下标访问/* cout << "number[0] = " << number[0] << endl;//error */cout << endl << endl;it = number.begin();++it;++it;cout << "*it = " << *it << endl;/* *it = 100;//error,不能修改 */
}void test00()
{pair<int, string> number = {1, "hello"};cout << number.first << " " << number.second << endl;
}void test2()
{//map的特征://1、存放的是key-value类型,也就是pair类型,//key值是唯一的,不能重复//2、默认情况下, 会按照key值进行升序排列//3、map底层使用的也是红黑树////初始化map<int, string> number = {pair<int, string>(1, "hello"),pair<int, string>(4, "hello"),pair<int, string>(2, "world"),{5, "wangdao"},{3, "wuhan"},{1, "hello"},};//遍历map<int, string>::iterator it;for(it = number.begin(); it != number.end(); ++it){cout << it->first << " " << it->second << endl;}cout << endl << endl;for(auto &elem : number){cout << elem.first << " " << elem.second << endl;}cout << endl << endl;cout << endl << "map的查找" << endl;size_t cnt1 = number.count(3);size_t cnt2 = number.count(7);cout << "cnt1 = " << cnt1 << endl;cout << "cnt2 = " << cnt2 << endl;/* auto it2 = number.find(3); */map<int,string>::iterator it2 = number.find(3);if(it2 == number.end())//迭代器判空{cout << "该元素不存在" << endl;}else{cout << "该元素存在, *it2 = " << it2->first << " "<< it2->second << endl;}cout << endl << "map的插入操作" << endl;pair<map<int, string>::iterator, bool> ret = number.insert(pair<int, string>(6, "kiki"));if(ret.second){cout << "插入成功 : " << ret.first->first << " "<< ret.first->second << endl;}else{cout << "插入失败" << endl;}for(auto &elem : number){cout << elem.first << " " << elem.second << endl;}cout << endl << endl;cout << "number[0] = " << number[0] << endl;//插入cout << "number[5] = " << number[5] << endl;//查找cout << endl << endl;for(auto &elem : number){cout << elem.first << " " << elem.second << endl;}cout << endl << endl;number[0] = "zhongguo";//修改number[1] = "zhongguo";//修改for(auto &elem : number){cout << elem.first << " " << elem.second << endl;}}int main(int argc, char **argv)
{test2();return 0;
}
test的运行结果:
1 2 3 5 7 8 9 1 2 3 5 7 8 9set的查找 cnt1 = 1 cnt2 = 0 该元素存在, *it2 = 3set的插入操作 插入成功 : 6 1 2 3 5 6 7 8 90 1 2 3 4 5 6 7 8 9 10 20*it = 2
test2的运行结果:
1 hello 2 world 3 wuhan 4 hello 5 wangdao1 hello 2 world 3 wuhan 4 hello 5 wangdaomap的查找 cnt1 = 1 cnt2 = 0 该元素存在, *it2 = 3 wuhanmap的插入操作 插入成功 : 6 kiki 1 hello 2 world 3 wuhan 4 hello 5 wangdao 6 kikinumber[0] = number[5] = wangdao0 1 hello 2 world 3 wuhan 4 hello 5 wangdao 6 kiki0 zhongguo 1 zhongguo 2 world 3 wuhan 4 hello 5 wangdao 6 kiki