STL set容器 入门

1.头文件 : #include<set>

2.定义 : set<string>name;

3.存入 : name.insert(key);

4.输出 : 需要定义一个迭选器 方法如下

4.1 set<string>::iterator it;(定义it为迭选器)

4.2 for(it=name.begin();i!=name.end();ii++)

{

cout<<*it<<endl;

}

5.set容器默认从小到大排序 若需要自己定义的排序 需要进行重载

5.1 比较函数的重载

struct cmp{

bool operator()(string a,string b)    / /对"()"进行重载

{

return a>b;

}

}

6.下面函数包括输入学生姓名 对学生姓名进行字典序排序的功能

#include <stdio.h>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
struct cmp{
	bool operator()(string a,string b)
	{
		return a>b;
	} 
};
int main(int argc, char *argv[])
{
	int n;
	set<string,cmp>name;
	set<string,cmp>::iterator it;
	while(scanf("%d",&n)!=EOF)
	{
		int i;
		for(i=0;i<n;i++)
		{
			string c;
			cin>>c;
			name.insert(c);
		}
		for(it=name.begin();it!=name.end();it++)
		{
			cout<<*it<<endl;
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值