set用法

一、set和multiset基础

  set和multiset会根据特定的排序准则,自动将元素进行排序。不同的是后者允许元素重复而前者不允许。

STL --> set用法-LMLPHP

需要包含头文件:

#include <set>

二、创建元素

set<int> s1;                    //创建空的set对象,元素类型为int,
set<const char*, strLess> s2( strLess); //创建空的set对象,元素类型char*,比较函数对象(即排序准则)为自定义strLess
set<int> s3(s1);               //利用set对象s1,拷贝生成set对象s2
int iArray[] = {, , };
set<int> s4(iArray, iArray + );      //用迭代区间[&first, &last)所指的元素,创建一个set对象
const char* szArray[] = {"hello", "dog", "bird" };
set<const char*, strLess> s5(szArray, szArray + , strLess() ); //用迭代区间[&first, &last)所指的元素,及比较函数对象strLess,创建一个set对象

三、插入元素

set<string> set1;    //empty set
set1.insert("the"); //set1 now has one element
set1.insert("and"); //set1 now has two elements
set<int> set2; //empty set
set2.insert(iset.begin(), iset.end()); //set2 now has 10 elements

例如:

// set::insert (C++98)
#include <iostream>
#include <set> int main ()
{
std::set<int> myset;
std::set<int>::iterator it;
std::pair<std::set<int>::iterator,bool> ret; // set some initial values:
for (int i=; i<=; ++i) myset.insert(i*); // set: 10 20 30 40 50 ret = myset.insert(); // no new element inserted if (ret.second==false) it=ret.first; // "it" now points to element 20 myset.insert (it,); // max efficiency inserting
myset.insert (it,); // max efficiency inserting
myset.insert (it,); // no max efficiency inserting int myints[]= {,,}; // 10 already in set, not inserted
myset.insert (myints,myints+); std::cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return ;
}

结果:

myset contains:          

 四、删除元素

// erasing from set
#include <iostream>
#include <set> int main ()
{
std::set<int> myset;
std::set<int>::iterator it; // insert some values:
for (int i=; i<; i++) myset.insert(i*); // 10 20 30 40 50 60 70 80 90 it = myset.begin();
++it; // "it" points now to 20 myset.erase (it);
myset.erase (); it = myset.find ();
myset.erase (it, myset.end()); std::cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return ;
}

结果:

myset contains:   

五、查找元素

iset.find();     //returns iterator that refers to the element with key==1
iset.find(); //returns iterator == iset.end()
iset.count(); //returns 1;
iset.count(); //returns 0; //set_it refers to the element with key==1
set<int>::iterator set_it = iset.find();
*set_it=; //error: keys in a set are read-only
cout<<*set_it<<endl; //ok: can read the key

例如:

// set::find
#include <iostream>
#include <set> int main ()
{
std::set<int> myset;
std::set<int>::iterator it; // set some initial values:
for (int i=; i<=; i++) myset.insert(i*); // set: 10 20 30 40 50 it=myset.find();
myset.erase (it);
myset.erase (myset.find()); std::cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return ;
}

结果:

myset contains:   

 六、其他

    #include <iostream>
#include <set>
using namespace std; int main()
{
typedef set<int,greater<int> > IntSet;
IntSet s1; s1.insert();
s1.insert();
s1.insert();
s1.insert();
s1.insert();
s1.insert();
s1.insert();
//the inserted element that has the same value with a element existed is emitted copy(s1.begin(),s1.end(),ostream_iterator<int>(cout," "));
cout << endl << endl; pair<IntSet::iterator,bool> status = s1.insert();
if(status.second)
cout << "4 is inserted as element "
<< distance(s1.begin(),status.first) + << endl;
else
cout << "4 already exists in s1" << endl;
copy(s1.begin(),s1.end(),ostream_iterator<int>(cout," "));
cout << endl << endl; set<int> s2(s1.begin(),s1.end());//default sort criterion is less<
copy(s2.begin(),s2.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
}

上述程序最后新产生一个set:s2,默认排序准则是less。以s1的元素作为初值。

注意:s1和s2有不同的排序准则,所以他们的型别不同,不能直接进行相互赋值或比较。

运行结果:


 already exist in s1
04-24 00:32