閱讀273 返回首頁    go 阿裏雲 go 技術社區[雲棲]


STL之五:set/multiset用法詳解

集合

使用set或multiset之前,必須加入頭文件<set>

Set、multiset都是集合類,差別在與set中不允許有重複元素,multiset中允許有重複元素。


sets和multiset內部以平衡二叉樹實現



1.   常用函數

1)        構造函數和析構函數

set c:創建空集合,不包含任何元素

set c(op):以op為排序準則,產生一個空的set

set c1(c2):複製c2中的元素到c1中

set c(const value_type *first, const value_type* last):複製[first, last)之間元素構成新集合

set c(const value_type *first, const value_type* last,op):以op為排序準則,複製[first, last)之間元素構成新集合。

c.~set()銷毀所有元素,釋放內存

multiset mc:創建空集合,不包含任何元素

multiset mc(op):以op為排序準則,產生一個空的set

multiset c1(c2):複製c2中的元素到c1中

multiset c(const value_type *first, const value_type* last):複製[first, last)之間元素構成新集合

multiset c(const value_type *first, const value_type* last,op):以op為排序準則,複製[first, last)之間元素構成新集合。

c.~set()銷毀所有元素,釋放內存

// constructing sets
#include <iostream>
#include <set>

bool fncomp (int lhs, int rhs) {return lhs<rhs;}

struct classcomp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs<rhs;}
};

int main ()
{
  std::set<int> first;                           // empty set of ints

  int myints[]= {10,20,30,40,50};
  std::set<int> second (myints,myints+5);        // range

  std::set<int> third (second);                  // a copy of second

  std::set<int> fourth (second.begin(), second.end());  // iterator ctor.

  std::set<int,classcomp> fifth;                 // class as Compare

  bool(*fn_pt)(int,int) = fncomp;
  std::set<int,bool(*)(int,int)> sixth (fn_pt);  // function pointer as Compare

  return 0;
}


2)        大小、判斷空函數

    int size() const:返回容器元素個數
    bool empty() const:判斷容器是否為空,若返回true,表明容器已空


3)        增加、刪除函數

      pair<iterator,bool> insert( x):插入元素x

    iterator insert(iterator it,x):在迭代器it處插入元素x

    void insert(const value_type *first,const value_type *last):插入[first, last)之間元素

    iterator erase(iterator it):刪除迭代器指針it處元素

    iterator erase(iterator first,iterator last):刪除[first, last)之間元素

    size_type erase(const Key& key):刪除元素值等於key的元素

#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=1; i<=5; ++i) myset.insert(i*10);    // set: 10 20 30 40 50

  ret = myset.insert(20);               // no new element inserted

  if (ret.second==false) it=ret.first;  // "it" now points to element 20

  myset.insert (it,25);                 // max efficiency inserting
  myset.insert (it,24);                 // max efficiency inserting
  myset.insert (it,26);                 // no max efficiency inserting

  int myints[]= {5,10,15};              // 10 already in set, not inserted
  myset.insert (myints,myints+3);

  std::cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;
  std::set<int>::iterator it;

  // insert some values:
  for (int i=1; i<10; i++) myset.insert(i*10);  // 10 20 30 40 50 60 70 80 90

  it = myset.begin();
  ++it;                                         // "it" points now to 20

  myset.erase (it);

  myset.erase (40);

  it = myset.find (60);
  myset.erase (it, myset.end());

  std::cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}


4)        遍曆函數

     iterator begin():返回首元素的迭代器指針

    iterator end():返回尾元素的迭代器指針
    reverse_iterator rbegin():返回尾元素的逆向迭代器指針
    reverse_iterator rend():返回首元素前一個位置的迭代器指針

     

#include <iostream>
#include <set>

int main ()
{
  int myints[] = {75,23,65,42,13};
  std::set<int> myset (myints,myints+5);

  std::cout << "myset contains:";
  for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '\n';

  return 0;
}

5)        操作函數

       const_iterator lower_bound(const Key& key):返回容器中大於等於key的迭代器指針

    const_iterator upper_bound(const Key& key):返回容器中大於key的迭代器指針

    int count(const Key& key) const:返回容器中元素等於key的元素的個數
    pair<const_iterator,const_iterator> equal_range(const Key& key) const:返回容器中元素值等於key的迭代指針[first, last)
    const_iterator find(const Key& key) const:查找功能,返回元素值等於key的迭代器指針
    void swap(set& s):交換集合元素
    void swap(multiset& s):交換多集合元素  

#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;
  std::set<int>::iterator 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

  std::cout << "myset contains:";
  for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}
#include "stdafx.h"
#include <iostream>
#include <set>

using namespace std;

int main ()
{
	set<int> myset;

	for (int i=1; i<=5; i++) myset.insert(i*10);   // myset: 10 20 30 40 50

	pair<set<int>::const_iterator,set<int>::const_iterator> ret;
	ret = myset.equal_range(30);

	cout << "the lower bound points to: " << *ret.first << '\n';
	cout << "the upper bound points to: " << *ret.second << '\n';

	return 0;
}

#include "stdafx.h"
#include <iostream>
#include <set>

using namespace std;

int main ()
{
	int myints[]={12,75,10,32,20,25};
	set<int> first (myints,myints+3);     // 10,12,75
	set<int> second (myints+3,myints+6);  // 20,25,32

	first.swap(second);

	cout << "first contains:";
	for (set<int>::iterator it=first.begin(); it!=first.end(); ++it)
		cout << ' ' << *it;
	cout << '\n';

	cout << "second contains:";
	for (set<int>::iterator it=second.begin(); it!=second.end(); ++it)
		cout << ' ' << *it;
	cout << '\n';

	return 0;
}



最後更新:2017-04-04 07:03:42

  上一篇:go iOS開發那些事--nib、xib與故事板的關係
  下一篇:go 圖片文字絕對居中,並排顯示