STL set multiset

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN___CSDN/article/details/88358421

STL容器大的方向分为两类,序列式容器和关联式容器。

这两者通过数据在容器内的排列来区分。关联容器和顺序容器的根本不同在于:关联容器中的元素是按关键字来保存和访问的,而顺序容器中的元素则是按它们在容器中的位置来顺序保存和访问的。

序列容器包括:vector、list、stack、queue、priority_queue 等

关联容器包括:set、multiset、map 等

SET && MULTISET

关于set,必须说明的是set关联式容器。set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。应该注意的是set中数元素的值不能直接被改变。
 

定义 set<int> s;

定义 multiset<int> ss; multiset 里面允许有重复的数字,且按照顺序排列

s.insert() 插入元素

s.begin() 返回set容器第一个元素的地址

s.end() 返回set容器的最后一个元素地址

s.size() 返回当前set容器中的元素个数

s.clear() 删除set容器中的所有的元素

s.empty() 判断set容器是否为空

s.rbegin() 返回的值和end()相同

s.rend() 返回的值和rbegin()相同

#include <iostream>
#include <set>

using namespace std;

int main ()
{
	//定义 
	set<int> s;
	
	//插入  
	int cnt = 10,x;
	while(cnt--)
	{
		cin >> x;
		s.insert(x); 
	}
	
	//迭代器 
	set<int>::iterator it;
	
	//输出 ,遍历 
	for(it=s.begin();it!=s.end();it++)
	{
		cout << *it << " "; 
	} 
	cout << endl; 	
	
	//s.size()返回容器中的元素个数 
	cout << "一共有" <<s.size() << "个元素\n" ;//10
	
	s.clear();
	
	if(s.empty()==1)
	{
		cout << "set 为空 !\n";
	}
	
	cout << "s.clear()之后,一共有" <<s.size() << "个元素\n" ;//0
	
	return 0;
}

     

s.count() :这个是看在set种出现的次数,但是在set里没有重复的元素,所以只可能是0和1,所以用来判断是不是set中的元素

s.find():用来查找set中某个元素出现的位置。如果找到,就返回这个元素的迭代器,如果这个元素不存在,则返回 s.end() 。 (最后一个元素的下一个位置,s为set的变量名)

#include <iostream>
#include <set>

using namespace std;

int main ()
{
	set<int> s;
	//插入 
	s.insert(10);
	s.insert(10);
	s.insert(20);
	s.insert(30);
	//遍历
	set<int>::iterator it;
	cout << "10出现的次数:" << s.count(10) << endl; 
	cout << "20出现的次数:" << s.count(20) << endl; 
	return 0;
} 
#include <iostream>
#include <set>

using namespace std;

int main ()
{
	set<int> s;
	//插入 
	s.insert(10);
	s.insert(10);
	s.insert(20);
	s.insert(30);
	//遍历
	set<int>::iterator it;
	it = s.find(30);
	if(it==s.end())
		cout << "没找到!";
	else
		cout << "找到" << *it; 
	return 0;
} 

erase(iterator)  ,删除定位器iterator指向的值

erase(first,second),删除定位器first和second之间的值

erase(key_value),删除键值key_value的值

小结:set中的删除操作是不进行任何的错误检查的,比如定位器的是否合法等等,所以用的时候自己一定要注意。

#include <iostream>
#include <set>

using namespace std;

int main ()
{
	set<int> s;
	set<int>::iterator it;
	set<int>::iterator first,second; 
	
	for(int i=1;i<=20;i++)
		s.insert(i);
	
	//删除 1 
	s.erase(s.begin());
	
	//删除2-4之间的数 (不包括4) 
	first=s.begin();//2 
	second=s.begin();
	second++;
	second++; 
	s.erase(first,second);
	
	s.erase(10);//10
	
	for(it=s.begin();it!=s.end();it++)
	{
		cout << *it <<" ";
	}
	//输出:4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20 
	return 0;
} 

lower_bound(key_value) ,返回第一个大于等于key_value的定位器

upper_bound(key_value),返回最后一个大于等于key_value的定位器

#include <iostream>
#include <set>  
  
using namespace std;  
  
int main()  
{  
    set<int> s;  
    s.insert(1);  
    s.insert(3);  
    s.insert(4);  
    s.insert(5);
    cout<<*s.lower_bound(2)<<endl;  //3
    cout<<*s.lower_bound(3)<<endl;  //3
    cout<<*s.upper_bound(3)<<endl;  //4
    return 0;  
}  

SET 自定义排序

set 里默认为less 函数,从小到大

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

using namespace std;

struct cmp
{
	bool operator() (const int &a ,const int &b) const
	{
		return a>b;
	}
};

int main()
{
	set<int,cmp> s;
	for(int i=1;i<=20;i++)
	{
		s.insert(i);
	}
	set<int,cmp>::iterator it;
	for(it=s.begin();it!=s.end();it++)
	{
		cout << *it << " ";
	}
	return 0;
}
//输出:20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
#include <iostream>
#include <set>
using namespace std;


int main()
{
	set<int,greater<int> > s;
	for(int i=1;i<=20;i++)
	{
		s.insert(i);
	}
	set<int,greater<int> >::iterator it;
	for(it=s.begin();it!=s.end();it++)
	{
		cout << *it << " ";
	}
	return 0;
}

结构体+set+排序:

#include<iostream>
#include<set>
#include<string>
using namespace std;
struct Info
{
    string name;
    double score;
};

struct cmp
{
	bool operator () (const Info &a,const Info &b) const // 重载“<”操作符,自定义排序规则
	{
	    //按score由大到小排序。
	    //如果是由小到大排序则是 <  
	    return a.score > b.score;
	}	
};


int main()
{
    set<Info,cmp> s;
    Info info;

    //插入三个元素
    info.name = "Jack";
    info.score = 80;
    s.insert(info);
    info.name = "Tom";
    info.score = 99;
    s.insert(info);
    info.name = "Steaven";
    info.score = 60;
    s.insert(info);

    set<Info,cmp>::iterator it;
    for(it = s.begin(); it != s.end(); it++)
        cout << (*it).name << " : " << (*it).score << endl; 
    return 0;
}
/*
运行结果:
Tom : 99
Jack : 80
Steaven : 60
*/
struct Info
{
    string name;
    double score;
	friend bool operator < (Info a,Info b) // 重载“<”操作符,自定义排序规则
	{
	    //按score由大到小排序。
	    //如果是由小到大排序则是 <  
	    return a.score > b.score;
	}
};
struct Info
{
    string name;
    double score;
	bool operator < (const Info & a) const
	{
		return score > a.score;
	}
};

猜你喜欢

转载自blog.csdn.net/CSDN___CSDN/article/details/88358421