C++ 標準模板庫STL set 使用方法與應用介紹(一)
阿新 • • 發佈:2019-01-26
這次先看例子程式:
#include <iostream> #include <set> #include <algorithm> #include <iterator> using namespace std; int main() { /* type of the collection: * - no duplicates * - elements are integral values * - descending order */ typedef set<int,greater<int> > IntSet; IntSet coll1; // empty set container // insert elements in random order coll1.insert(4); coll1.insert(3); coll1.insert(5); coll1.insert(1); coll1.insert(6); coll1.insert(2); coll1.insert(5); cout<<"typedef set<int,greater<int> > IntSet 由大到小:"<<endl; // iterate over all elements and print them IntSet::iterator pos; for (pos = coll1.begin(); pos != coll1.end(); ++pos) { cout << *pos << ' '; } cout << endl; // insert 4 again and process return value pair<IntSet::iterator,bool> status = coll1.insert(4); if (status.second) { cout << "4 inserted as element " << distance(coll1.begin(),status.first) + 1 << endl; } else { cout << "4 already exists" << endl; } status = coll1.insert(512); if (status.second) { cout << "512 inserted as element " << distance(coll1.begin(),status.first) + 1 << endl; } else { cout << "512 already exists" << endl; } // print all elements of the copy cout<<"after pair<IntSet::iterator,bool> status = coll1.insert(4 and 512):"<<endl; copy (coll1.begin(), coll1.end(),ostream_iterator<int>(cout," ")); cout<<endl; // assign elements to another set with ascending order set<int> coll2(coll1.begin(),coll1.end()); cout<<"after set<int> coll2(coll1.begin(),coll1.end()) coll2: 由小到大:"<<endl; // print all elements of the copy copy (coll2.begin(), coll2.end(),ostream_iterator<int>(cout," ")); cout << endl; // remove all elements up to element with value 3 coll2.erase (coll2.begin(), coll2.find(3)); cout<<"after coll2.erase (coll2.begin(), coll2.find(3)) :"<<endl; // print all elements copy (coll2.begin(), coll2.end(),ostream_iterator<int>(cout," ")); // remove all elements with value 5 int num; num = coll2.erase (5); cout<<"after :num = coll2.erase (5) :"<<endl; cout <<endl<< num << " element(s) removed" << endl; // print all elements copy (coll2.begin(), coll2.end(),ostream_iterator<int>(cout," ")); cout << endl; } /******************* 程式執行結果如下: typedef set<int,greater<int> > IntSet 由大到小: 6 5 4 3 2 1 4 already exists 512 inserted as element 1 after pair<IntSet::iterator,bool> status = coll1.insert(4 and 512): 512 6 5 4 3 2 1 after set<int> coll2(coll1.begin(),coll1.end()) coll2: 由小到大: 1 2 3 4 5 6 512 after coll2.erase (coll2.begin(), coll2.find(3)) : 3 4 5 6 512 after :num = coll2.erase (5) : 1 element(s) removed 3 4 6 512 Process returned 0 (0x0) execution time : 0.496 s Press any key to continue. ********************/
集和多集(set 和multiset 容器類)
#include <set> 一個集合(set)是一個容器,它其中所包含的元素的值是唯一的。 集和多集的區別是:set支援唯一鍵值,set中的值都是特定的,而且只出現一次;而multiset中可以出現副本鍵,同一值可以出現多次。構造
explicit set(const Compare&=compare()); 如:set<int,less<int> > set1; less<int>是一個標準類,用於形成升序排列函式物件。降序排列是用greater<int>。 Template<class InputIterator> set(InputIterator, InputIterator,\ const Compare&=compare());方法:
begin() 返回指向第一個元素的迭代器 clear() 清除所有元素 count() 返回某個值元素的個數 empty() 如果集合為空,返回true(真) end() 返回指向最後一個元素之後的迭代器,不是最後一個元素集合操作:
std::set_intersection() :這個函式是求兩個集合的交集。 std::set_union() :求兩個集合的並集 std::set_symmetric_difference():得到的結果是第一個迭代器相對於第二個的差集並 上第二個相當於第一個的差集 struct compare{ bool operator ()(string s1,string s2)
{ return s1>s2; }///自定義一個仿函式 }; std::set<string,compare> s string str[10]; string *end = set_intersection(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//求交集,返回值指向str最後一個元素的尾端 end = std::set_union(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//並集 end = std::set_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//s2相對於s1的差集 end = std::set_difference(s2.begin(),s2.end(),s.begin(),s.end(),str,compare());//s1相對於s2的差集 end = std::set_symmetric_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//上面兩個差集的並集