STL容器之——set(集合)

1.插入元素:

#include <iostream>  
#include <string>  
#include <set>  
using namespace std;  
void printSet(set<int> s)  
{  
 set<int>::iterator i;  
 for(i=s.begin();i!=s.end();i++)  
        printf("%d ",*i);  
 cout<<endl;  
}  
int main()  
{  
 //创建空的set对象,元素类型为int,  
 set<int> s1;  
 for (int i = 0; i <5 ; i++)  
  s1.insert(i*10);  
 printSet(s1);  
 cout<<"s1.insert(20).second = "<<endl;;  
 if (s1.insert(20).second)//再次插入20     
  cout<<"Insert OK!"<<endl;  
 else  
  cout<<"Insert Failed!"<<endl;  
 cout<<"s1.insert(50).second = "<<endl;  
 if (s1.insert(50).second)  
 {
cout<<"Insert OK!"<<endl; 
printSet(s1);
}  
 else  
  cout<<"Insert Failed!"<<endl;  
 pair<set<int>::iterator, bool> p;  
  p = s1.insert(60);  
 if (p.second)  
 {
cout<<"Insert OK!"<<endl; 
printSet(s1);
}  
 else  
  cout<<"Insert Failed!"<<endl;  
}  

2.删除元素:

#include <iostream>  
#include <set>  
using namespace std;  
int main ()  
{  
  set<int> myset;  
  myset.insert(20);  
  myset.insert(30);  
  myset.insert(10);  
  while (!myset.empty())  
  {  
     cout <<" "<< *myset.begin();  
     myset.erase(myset.begin());  
  }  
  cout << endl;  
  return 0;  
}  

 

#include <iostream>  
#include <set>  
using namespace std;  
  
int main ()  
{  
  set<int> myset;  
  set<int>::iterator it;  
  for (int i=1; i<=5; i++) myset.insert(i*10);    // set: 10 20 30 40 50  
  it=myset.find(20);  
  myset.erase (it);  
  myset.erase (myset.find(40));  
  myset.erase (30);  
  cout << "myset contains:";  
  for (it=myset.begin(); it!=myset.end(); it++)  
    cout << " " << *it;  
  cout << endl;  
  return 0;  
}  

3.并集,交集

#include<stdio.h>  
#include<string>  
#include<set>  
#include<iostream>  
#include <algorithm>//包含  
using namespace std;  
  
struct compare//自定义排序方式  
{  
    bool operator ()(string s1,string s2)  
    {  
        return s1>s2;  
    }///自定义一个仿函数  
};  
int main()  
{  
    typedef  set<string,compare>  SET;  
    SET s;//建立第一个集合  
    s.insert(string("sfdsfd"));  
    s.insert(string("apple"));  
    s.insert(string("english"));  
    s.insert(string("dstd"));  
    cout<<"第一个集合s1为:"<<endl;  
     set<string,compare>::iterator it = s.begin();  
    while(it!=s.end())  
        cout<<*it++<<"   ";  
  
    SET s2;//建立第二个集合  
    s2.insert(string("abc"));  
    s2.insert(string("apple"));  
    s2.insert(string("english"));  
    cout<<endl<<"第一个集合s2为:"<<endl;  
    it = s2.begin();  
    while(it!=s2.end())  
        cout<<*it++<<"   ";  
    cout<<endl<<endl;  
      
    string str[10];  
    string *end =set_intersection(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//求交集,返回值指向str最后一个元素的尾端  
    /*set_intersection包含于#include <algorithm>   头文件中  其中上面的不一定非要为set容器 也可以使数组 但是前提是要把2个数组都排好序才可以 
    返回值是一个指向交集序列末尾的迭代器 至于是什么迭代器与第5个参数有关 如果是数组 返回为int的迭代器 */  
    cout<<"s1,s2的交集为:"<<endl;  
    string *first = str;  
    while(first<end)  
        cout <<*first++<<" ";  
  
  
    cout<<endl<<endl<<"s1,s2的并集为:"<<endl;  
    end =set_union(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//并集  
    first = str;  
    while(first<end)  
        cout <<*first++<<" ";  
  
  
    cout<<endl<<endl<<"s2相对于s1的差集:"<<endl;   
    first = str;  
    end = std::set_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//s2相对于s1的差集  
    while(first<end)  
        cout <<*first++<<" ";  
  
  
    cout<<endl<<endl<<"s1相对于s2的差集:"<<endl;   
    first = str;  
    end = std::set_difference(s2.begin(),s2.end(),s.begin(),s.end(),str,compare());//s1相对于s2的差集  
      
    while(first<end)  
        cout <<*first++<<" ";  
    cout<<endl<<endl;  
    first = str;  
    end = std::set_symmetric_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//上面两个差集的并集  
    while(first<end)  
        cout <<*first++<<" ";  
    cout<<endl;   
}

 

 

posted @ 2023-02-12 16:21  金鳞踏雨  阅读(19)  评论(0编辑  收藏  举报  来源