1.数据自带排序
2.数据具有唯一性(单集合)
3.自定义类型数据需要重载<
#include <iostream> #include <set> #include <string> #include <ctime> #include <bitset> using namespace std; //基本操作 void testSet() { srand((unsigned int)time(nullptr));//随机数种子 set<int> setdata1; set<int, less<int>> setdata2; for (int i = 0; i < 10; i++) { setdata1.insert(rand() % 10); } for (set<int>::iterator iter = setdata1.begin(); iter != setdata1.end(); iter++) { cout << *iter << endl; } } //自定义类型操作 class Boy { public: Boy(string name, int age) :name(name), age(age){} bool operator<(const Boy& boy)const { return this->name < boy.name; } void print() { cout << name << "\t" << age << endl; } protected: string name; int age; }; void testusedata() { set<Boy> boydata; boydata.insert(Boy("name1", 1)); boydata.insert(Boy("name2", 2)); boydata.insert(Boy("name1", 3)); boydata.insert(Boy("name2", 4)); for (auto v : boydata) { v.print(); } } //多重集合 void testmultiset() { multiset<int> muldata; for (int i = 0; i < 10; i++) { muldata.insert(rand() % 10); } for (auto v : muldata) { cout << v << endl; } } //bitset void testbitset() { bitset<8> bsdata(1110000);//多少个二进制位 cout << bsdata << endl; cout<<bsdata.flip() << endl;//反转 } int main() { testSet(); testusedata(); testmultiset(); testbitset(); return 0; }
1.自带排序
2.数据唯一性
3.自定义数据类型重载键的符号(<)
4.多重映射,存在相同的键,不可采用下标
#include <iostream> #include <map> #include <string> using namespace std; //数对 void testpair() { pair<int, string> pdata(1, "string"); cout << pdata.first << " " << pdata.second << endl; } //单映射 void testmap() { map<int, string> mapdata; //1.insert插入 mapdata.insert(pair<int, string>(1, "string1")); //2.make_pair构建数对插入 mapdata.insert(make_pair<int,string>(1,"string0")); //3.单映射,可以直接采用数组下标方式 mapdata[-1] = string("string-1"); for (map<int, string>::iterator iter = mapdata.begin(); iter != mapdata.end(); iter++) { //相同的键采用覆盖的方式 cout << iter->first << "\t" << iter->second << endl;//-1 string-1 1 string1先插后出 } //删除(删除键值) mapdata.erase(1); } class A { public: A() = default; A(string name, int age) :name(name), age(age){} bool operator<(const A& a)const { return this->name < a.name; } void print()const { cout << name << "\t" << age << endl; } protected: string name; int age; }; class B { public: B(string name, int age) :name(name), age(age){} B() = default; void print()const { cout << name << "\t" << age << endl; } protected: string name; int age; }; //自定义类型 void testclass() { map<A, B> mapclass; mapclass[A("classA1", 1)] = B("classB1", 1); mapclass[A("classA1", 1)].print(); mapclass[A("classA2", 2)] = B("classB2", 2); for (auto v : mapclass) { v.first.print(); v.second.print(); } } //多重映射 void testmultimap() { multimap<int, string> muldata; muldata.insert(pair<int, string>(1, "string1")); muldata.insert(pair<int, string>(1, "string2")); muldata.insert(pair<int, string>(2, "string1")); muldata.insert(pair<int, string>(2, "string2")); muldata.insert(pair<int, string>(3, "string1")); for (auto v : muldata) { cout << v.first << "\t" << v.second << endl; } } int main() { testmap(); testclass(); testmultimap(); return 0; }
#include <iostream> #include <array> #include <vector> #include <list> #include <initializer_list> using namespace std; class A { public: A(int a1, int a2, int a3, int a4) :a1(a1), a2(a2), a3(a3), a4(a4){} A(const initializer_list<int> inilist) { for (auto iter = inilist.begin(); iter != inilist.end(); iter++) { cout << *iter << "\t"; } cout << endl; } protected: int a1; int a2; int a3; int a4; }; void print(const initializer_list<int> inilist) { for (auto iter = inilist.begin(); iter != inilist.end(); iter++) { cout << *iter << "\t"; } cout << endl; } int main() { array<int, 3> arrdata = { 1, 2, 3 }; vector<int> vecdata = { 1, 2, 3, 4, 5, 6 }; A a = { 1, 2, 3, 4 }; A b = { 1, 2, 3 }; print({ 1, 2, 3, 4, 5 }); return 0; }
1.整合数据为一组
#include <iostream> #include <tuple> #include <string> using namespace std; //元组 int main() { //初始化 tuple<string, int, int, string> boy = make_tuple("name", 1, 1, "111"); tuple<string, string> name = forward_as_tuple("name1", "name2"); tuple<string, int, int, string> boy1[2]; //查看 //1.get 不可用for cout << get<0>(boy) << "\t"; cout << get<1>(boy) << "\t"; cout << get<2>(boy) << "\t"; cout << get<3>(boy) << "\t" << endl; //2.tie string boyname; int age; int num; string tel; tie(boyname, age, num, tel) = boy; cout << boyname << "\t" << age << "\t" << num << "\t" << tel << endl; //链接 auto result = tuple_cat(boy, name); return 0; }