C++容器--set_string

#include <iostream>
#include <set>
#include <string>


using namespace std;


void printS(set<int> &s)
{
set<int>::iterator it = s.begin();
while(it != s.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}


void printS(set<int,greater<int>> &s)
{
set<int,greater<int>>::iterator it = s.begin();
while(it != s.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}
//插入 ,删除数据
void func1()
{
//set集合 内部数据默认是从小到大排序
//set 内部数据时不允许重复的
set<int,less<int>> s;
//set<int,greater<int>> s;


s.insert(1);
s.insert(3);
s.insert(4);
s.insert(9);
s.insert(8);


printS(s);


s.erase(s.begin());
}


class Student
{
public:
Student(int id,const string &name)
{
this->id = id;
this->name = name;
}
void print()const
{
printf("id = %d,name = %s\n",id,name.c_str());
}
int getId()const
{
return id;
}


string GetName()const
{
return name;
}
private:
int id;
string name;
};


//比较的类:比较两个学生的大小
class MyCompare
{
public:
bool operator()(const Student &left,const Student &right)
{
return left.GetName() > right.GetName();
//return left.getId() < right.getId();
}
};


void func2()
{
Student stus[] = {
Student(1,"小明"),
Student(10,"小芳"),
Student(6,"小红"),
Student(3,"小张"),
};
//set 集合是一个排号序的集合,插入 不知道怎么对学生数据进行排序
//比较大小 ===> 交换
//自己写比较大小的函数
set<Student,MyCompare> s;
s.insert(stus[0]);
s.insert(stus[1]);
s.insert(stus[2]);
s.insert(stus[3]);


set<Student,MyCompare>::iterator it = s.begin();
while(it != s.end())
{
//集合内部的数据是不允许修改的
it->print();
it++;
}
}


//返回值
void func3()
{
set<int> s;
s.insert(1);
s.insert(3);
s.insert(4);


//pair对组,将一组数据整合到一块,当一个数据使用
//first:对组中的第一个元素,迭代器,插入成功的那个元素的迭代器
//second:对组中的第二个元素,bool类型,表示插入成功还是没成功
pair<set<int>::iterator,bool> ret = s.insert(6);
if(ret.second)
cout << "插入成功" << endl;
else
cout << "插入失败" << endl;


ret = s.insert(4);
if(ret.second)
cout << "插入成功" << endl;
else
cout << "插入失败" << endl;
}
//查找
void func4()
{
set<int> s;
s.insert(1);
s.insert(3);
s.insert(5);
//找到返回找到的元素的迭代器,没找到返回 s.end()
set<int>::iterator it = s.find(3);
if(it == s.end())
cout << "没找到" << endl;
else
cout << "找到" <<  *it << endl;
//返回第一个大于要查找的元素的迭代器
it = s.upper_bound(3);
if(it == s.end())
cout << "没找到" << endl;
else
cout << "找到" << *it << endl;
//返回第一个大于等于要查找的元素的迭代器
it = s.lower_bound(3);
if(it == s.end())
cout << "没找到" << endl;
else
cout << "找到" << *it << endl;


//第一个:等价于lower_bound
//第二个:等价于uooer_bound
pair<set<int>::iterator,set<int>::iterator> ret = s.equal_range(3);
if(ret.first == s.end())
cout << "没找到" << endl;
else
cout << "找到" << *it << endl;


if(ret.second == s.end())
cout << "没找到" << endl;
else
cout << "找到" << *it << endl;
}
//multiset
void func5()
{
multiset<int> s;
s.insert(1);
s.insert(10);
s.insert(3);
s.insert(4);
s.insert(5);


multiset<int>::iterator it = s.begin();
while(it != s.end())
{
cout << *it << " ";
it++;
}
cout << endl;
cout << s.count(3) << endl;
}
int main()
{
func5();
return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值