STL用法(转自吉吉)

(七)set类的主要成员
set<T>是一种集合容器。
☆void clear();//删除所有元素
☆bool empty() const; //返回是否为空
☆void erase(iterator position);//删除在位置position的元素      //貌似也可以直接删除值
☆iterator find(const key_type& x)const;//返回索引为x的元素的指针

☆pair<iterator,bool>insert(const value_type& x);//返回<指向元素x的迭代器,是否插入成功>
☆interator lower_bound(const key_type& x)const; //返指向键不小于x的第一个元素的迭代器
☆size_type size() const;//返回set的大小
☆void swap(set& X);//与setX交换内容
☆iterator upper_bound(const key_type& x)const;//返回指向键大于x的第一个元素的迭代器

STL-set用法



// 1.set::begin/end
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  int myints[] = {75,23,65,42,13,13};
  set<int> myset (myints,myints+6);
  set<int>::iterator it;
  cout << "myset contains:";
  for ( it=myset.begin() ; it != myset.end(); it++ )
    cout << " " << *it;
  cout << endl;
  return 0;


Output:
myset contains: 13 23 42 65 75

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

Output:myset contains: 10 20 30

// 3.set::size
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  set<int> myints;
  cout << "0. size: " << (int) myints.size() << endl;
  for (int i=0; i<10; i++) myints.insert(i);
  cout << "1. size: " << (int) myints.size() << endl;
  myints.insert (100);
  cout << "2. size: " << (int) myints.size() << endl;
  myints.erase(5);
  cout << "3. size: " << (int) myints.size() << endl;
  return 0;
}
Output:0. size: 0
1. size: 10
2. size: 11
3. size: 10

// 4.set::find
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  set<int> myset;
  set<int>::iterator it;
  // set some initial values:
  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));
  cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); it++)
    cout << " " << *it;
  cout << endl;
  return 0;
}

Output:
myset contains: 10 30 50

// 5.set::count
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  set<int> myset;
  int i;

  // set some initial values:

  for (i=1; i<5; i++) myset.insert(i*3);    // set: 3 6 9 12

  for (i=0;i<10; i++)
  {
    cout << i;
    if (myset.count(i)>0)
      cout << " is an element of myset.\n";
    else 
      cout << " is not an element of myset.\n";
  }
  return 0;
}

Output:
0 is not an element of myset.
1 is not an element of myset.
2 is not an element of myset.
3 is an element of myset.
4 is not an element of myset.
5 is not an element of myset.
6 is an element of myset.
7 is not an element of myset.
8 is not an element of myset.
9 is an element of myset.


 
// 6.set::lower_bound/upper_bound
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  set<int> myset;
  set<int>::iterator it,itlow,itup;
  for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
  itlow=myset.lower_bound (30);                //       ^
  itup=myset.upper_bound (60);                 //                   ^
  myset.erase(itlow,itup);                     // 10 20 70 80 90
  cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); it++)
    cout << " " << *it;
  cout << endl;
  return 0;
}
 
Notice that lower_bound(30) returns an iterator to 30, whereas upper_bound(60) returns an iterator to 70.
myset contains: 10 20 70 80 90


// 7.set::equal_elements

#include <iostream>

#include <set>
using namespace std;

int main ()
{
  set<int> myset;
  pair<set<int>::iterator,set<int>::iterator> ret;
  for (int i=1; i<=5; i++) myset.insert(i*10);   // set: 10 20 30 40 50
  ret = myset.equal_range(30);
  cout << "lower bound points to: " << *ret.first << endl;
  cout << "upper bound points to: " << *ret.second << endl;
  return 0;


lower bound points to: 30
upper bound points to: 40

你可能感兴趣的:(set,STL)