15.C++STL容器(三)

  1. set容器

    1. set
      #include <iostream>
      #include <set>
      #include<ctime>
      #include <string>
      using namespace std;
      
      class Student
      {
      public:
          Student(string name,int stuNum):name(name),stuNum(stuNum){}
          bool operator<(const Student&obeject)const {
              return this->stuNum < obeject.stuNum;
          }
          friend ostream& operator<<(ostream& out, const Student& object) {
              out << object.name << " " << object.stuNum;
              return out;
          }
          void print() {
              cout << name << " " << stuNum << endl;
          }
      protected:
          string name;
          int stuNum;
      private:
      };
      
      int main() {
      
          //set:集合
          //1.数据自带排序
          //2.数据唯一性\
      
          //1.集合存储普通类型数据
          set<int>setData1;//默认排序由小到大
          set<int, less<int>>setData2;//由小到大
          set<int, greater<int>>setData3;//由大到小
          srand((unsigned int)time(nullptr));
      	int nArray[10] = { 0 };
          for (int i=0;i<10;i++)
          {
              int temp = rand() % 10;
              nArray[i] = temp;
              setData1.insert(temp);
          }
          for (auto v:setData1)
          {
              cout << v << " ";
          }
          cout << endl;
          for (set<int>::iterator iter=setData1.begin();iter!=setData1.end();iter++)
          {
              cout << *iter << " ";
          }
          cout << endl;
          //2.集合存储自定义类型数据
          set<Student>setStu;
      	setStu.insert(Student("张三", 1001));
      	setStu.insert(Student("李四", 1002));
      	setStu.insert(Student("王五", 1003));
          for (auto v:setStu)
          {
              v.print();
          }
          cout << endl;
      	for (set<Student>::iterator iter = setStu.begin(); iter != setStu.end(); iter++)
      	{
      		cout << *iter << " ";
      	}
          cout << endl;
          //去重功能
          int Array[15] = { 1,2,3,4,5,6,6,6,6,6,77,6,9,5,5 };
          set<int>nSet;
          for (int i=0;i<15;i++)
          {
              nSet.insert(Array[i]);
          }
          for (auto v:nSet)
          {
              cout << v << " ";
          }
      
      	return 0;
      }

    2. multiset
      #include <iostream>
      #include <set>
      using namespace std;
      
      int main() {
      	srand((unsigned int)time(nullptr));
      	multiset<int> mSet;
      	for (int i=0;i<10;i++)
      	{
      		mSet.insert(rand() % 20);
      	}
      	for (auto v:mSet)
      	{
      		//多重集合: 只具有排序功能,不具有去重功能
      		cout << v << " ";
      	}
      
      	return 0;
      }

    3. bitset
      #include <iostream>
      #include <bitset>
      using namespace std;
      
      int main(){
      	bitset<8>num(18);
      	cout << num << endl;
      	bitset<8>str("10010100");
      	cout << str << endl;
      	str.flip();//取反
      	cout << str << endl;
      	str.set(4);//指定位置设置为1
      	cout << str << endl;
      	str.set();//全设置为1
      	cout << str << endl;
      	str.reset(3); //全设置为0
      	cout << str << endl;
      	str.reset(); //全设置为0
      	cout << str << endl;
      
      }

  2. map容器

    1. map
      #include<iostream>
      #include <string>
      #include <map>
      using namespace std;
      
      class Student
      {
      public:
      	Student() = default;
      	Student(string name,int stuNum):name(name),stuNum(stuNum){}
      	bool operator<(const Student& object) const{
      		return this->stuNum < object.stuNum;
      	}
      	friend ostream& operator<<(ostream& out, const Student& object) {
      		out << object.name << " " << object.stuNum;
      		return out;
      	}
      	void print() const{
      		cout << name << " " << stuNum << endl;
      	}
      protected:
      	string name;
      	int stuNum;
      private:
      };
      
      class Score
      {
      public:
      	Score() = default;
      	Score(double Chinese,double math):Chinese(Chinese),math(math){}
      	friend ostream& operator<<(ostream& out, const Score& object) {
      		out << object.Chinese << " " << object.math;
      		return out;
      	}
      	void print() const{
      		cout << Chinese << " " << math << endl;
      	}
      protected:
      	double Chinese;
      	double math;
      private:
      };
      
      int main() {
      	/*
      	map
      	1.自带排序,默认是从小到大
      	2.数据唯一性
      	*/
      
      	//1.存储普通类型数据
      	map<int, string>mapData;//默认是从小到大
      	map<int, string, less<int>>mapData1;//从小到大
      	map<int, string, greater<int>>mapData2;//从大到小
      	mapData.insert(pair<int, string>(1, "1001")); //insert插入
      	mapData.insert(make_pair<int, string>(2, "1002"));//make_pair构建数对插入
      	mapData[3] = "1003";//单映射,可以直接采用数组下标方式进行插入,相比数组来说,这个下标是没有任何要求
      	mapData[1] = "1004";//相同键 采用的是覆盖方式
      	for (auto v : mapData)
      	{
      		cout << v.first << " " << v.second << endl;
      	}
      	cout << endl;
      	mapData.erase(1);//删除
      	for (map<int,string>::iterator iter=mapData.begin();iter!=mapData.end();iter++)
      	{
      		cout << iter->first << " " << iter->second << endl;
      	}
      	cout << endl;
      
      	//2.存储自定义类型的数据
      	map<Student, Score>stuMap;
      	stuMap.insert(pair<Student, Score>(Student("张三", 1001), Score(95, 98)));
      	stuMap[Student("李四", 1002)] = Score(94, 88);
      	stuMap[Student("王五", 1003)] = Score(89, 98);
      	for (auto v : stuMap) {
      		//v.first.print();
      		//v.second.print();
      		cout << v.first << " " << v.second << endl;
      	}
      	for (map<Student, Score>::iterator iter=stuMap.begin();iter!=stuMap.end();iter++)
      	{
      		cout << iter->first << " " << iter->second << endl;
      	}
      	return 0;
      }

    2. multimap
      #include <iostream>
      #include <string>
      #include <map>
      using namespace std;
      
      int main() {
      	//多重映射,没有什么限制,什么样对应关系都可以插入到映射中
          //因为存在相同的键,所以不能采用下标法
      	multimap<int, string>  mulData;
      	mulData.insert(pair<int, string>(1001, "张三"));
      	mulData.insert(pair<int, string>(1002, "李四"));
      	mulData.insert(pair<int, string>(1003, "王五"));
      	mulData.insert(pair<int, string>(1003, "周六"));
      	for (auto v : mulData)
      	{
      		cout << v.first << "\t" << v.second << endl;
      	}
      
      	return 0;
      }

  3. initializer_list容器

    #include <iostream>
    #include <string>
    #include<initializer_list>
    using namespace std;
    //输入任意个参数
    void print(initializer_list<int>list) {
    	for (auto iter=list.begin();iter!=list.end();iter++)
    	{
    		cout << *iter << " ";
    	}
    	cout << endl;
    }
    
    int main() {
    	print({ 1 });
    	print({ 1 ,2 });
    	print({ 1 ,2,3 });
    	print({ 1 ,2,3,4 });
    	print({ 1 ,2,3,4,5 });
    
    	return 0;
    }

  4. tuple容器

    #include <iostream>
    #include<string>
    #include <tuple>
    using namespace std;
    
    int main() {
    	//把任何类型的一系列数据当做一组处理
    	tuple<string, int, string, double>stuInfo1 = { "张三",1001,"男",89 };
    	tuple<string, int, string, double>stuInfo2 = make_tuple("李四", 1002, "男", 95);
    	tuple<string, int, string, double>stuInfo3 = forward_as_tuple("王五", 1003, "女", 92);
    	//get方法,不能用for循环
    	cout << get<0>(stuInfo1) << " ";
    	cout << get<1>(stuInfo1) << " ";
    	cout << get<2>(stuInfo1) << " ";
    	cout << get<3>(stuInfo1) << endl;
    	//tie的方式访问数据
    	string name;
    	int stuNum;
    	string sex;
    	double score;
    	tie(name, stuNum, sex, score) = stuInfo2;
    	cout << name << " " << stuNum << " " << sex << " " << score << endl;
    	//连接
    	auto result = tuple_cat(stuInfo2, stuInfo3);
    	cout << get<0>(result) << " ";
    	cout << get<1>(result) << " ";
    	cout << get<2>(result) << " ";
    	cout << get<3>(result) <<endl;
    	cout << get<4>(result) << " ";
    	cout << get<5>(result) << " ";
    	cout << get<6>(result) << " ";
    	cout << get<7>(result) << endl;
    	return 0;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值