小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

STL set

 啟蒙彩魂 2011-02-20

百科名片

STL 對這個序列可以進(jìn)行查找,插入刪除序列中的任意一個元素,而完成這些操作的時間同這個序列中元素個數(shù)的對數(shù)成比例關(guān)系,并且當(dāng)游標(biāo)指向一個已刪除的元素時,刪除操作無效。而一個經(jīng)過更正的和更加實際的定義應(yīng)該是:一個集合(set)是一個容器,它其中所包含的元素的值是唯一的。這在收集一個數(shù)據(jù)的具體值的時候是有用的。集合中的元素按一定的順序排列,并被作為集合中的實例。一個集合通過一個鏈表來組織,在插入操作和刪除操作上比向量(vector)快,但查找或添加末尾的元素時會有些慢。具體實現(xiàn)采用了紅黑樹的平衡二叉樹的數(shù)據(jù)結(jié)構(gòu)。

目錄

集和多集(set 和multiset 容器類)
構(gòu)造
方法:
集合操作:
例子:

編輯本段集和多集(set 和multiset 容器類)

  #include <set>
  一個集合(set)是一個容器,它其中所包含的元素的值是唯一的。
  集和多集的區(qū)別是:set支持唯一鍵值,set中的值都是特定的,而且只出現(xiàn)一次;而multiset中可以出現(xiàn)副本鍵,同一值可以出現(xiàn)多次。

編輯本段構(gòu)造

  explicit set(const Compare&=compare());
  如:set<int,less<int> > set1;
  less<int>是一個標(biāo)準(zhǔn)類,用于形成降序排列函數(shù)對象。升序排列是用greater<int>。
  Template<class InputIterator> set(InputIterator, InputIterator,\ const Compare&=compare());
  如:set<int ,less<int> >set2(vector1.begin(),vector1.end());
  通過指定某一預(yù)先定義的區(qū)間來初始化set對象的構(gòu)造函數(shù)。
  set(const set<Key,Compare&>);
  如:set<int ,less<int> >set3(set2);
  復(fù)制構(gòu)造函數(shù)。

編輯本段方法:

  begin() 返回指向第一個元素的迭代器
  clear() 清除所有元素
  count() 返回某個值元素的個數(shù)
  empty() 如果集合為空,返回true
  end() 返回指向最后一個元素的迭代器
  equal_range() 返回集合中與給定值相等的上下限的兩個迭代器
  erase() 刪除集合中的元素
  find() 返回一個指向被查找到元素的迭代器
  get_allocator() 返回集合的分配器
  insert() 在集合中插入元素
  lower_bound() 返回指向大于(或等于)某值的第一個元素的迭代器
  key_comp() 返回一個用于元素間值比較的函數(shù)
  max_size() 返回集合能容納的元素的最大限值
  rbegin() 返回指向集合中最后一個元素的反向迭代器
  rend() 返回指向集合中第一個元素的反向迭代器
  size() 集合中元素的數(shù)目
  swap() 交換兩個集合變量
  upper_bound() 返回大于某個值元素的迭代器
  value_comp() 返回一個用于比較元素間的值的函數(shù)

編輯本段集合操作:

  std::set_intersection() :這個函數(shù)是求兩個集合的交集。
  std::set_union() :求兩個集合的并集
  std::set_difference():差集
  std::set_symmetric_difference():得到的結(jié)果是第一個迭代器相對于第二個的差集并 上第二個相當(dāng)于第一個的差集
  struct compare{
  bool operator ()(string s1,string s2){
  return s1>s2;
  }///自定義一個仿函數(shù)
  };
  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());//上面兩個差集的并集

編輯本段例子:

  1///////////////////////////////////////////////////////////////////
  #include <iostream>
  #include <set>
  using namespace std;
  int main(void)
  {
  set<int> set1;
  for(int i=0; i<10; ++i)
  set1.insert(i);
  for(set<int>::iterator p=set1.begin();p!=set1.end();++p)
  cout<<*p<<"";
  if(set1.insert(3).second)//把3插入到set1中
  //插入成功則set1.insert(3).second返回1,否則返回0
  //此例中,集中已經(jīng)有3這個元素了,所以插入將失敗
  cout<<"set insert success";
  else
  cout<<"set insert failed";
  int a[] = {4, 1, 1, 1, 1, 1, 0, 5, 1, 0};
  multiset<int> A;
  A.insert(set1.begin(),set1.end());
  A.insert(a,a+10);
  cout<<endl;
  for(multiset<int>::iterator p=A.begin();p!=A.end();++p)
  cout<<*p<<" ";
  cin.get();
  return 0;
  }
  2////////////////////////////////////////
  struct compare
  {
  bool operator ()(string s1,string s2)
  {
  return s1>s2;
  }///自定義一個仿函數(shù)
  };
  int main()
  {
  typedef std::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;
  std::set<string,compare>::iterator it = s.begin();
  while(it!=s.end())
  cout<<*it++<<" ";
  cout<<endl<<"s2:"<<endl;
  _SET s2;
  s2.insert(string("abc"));
  s2.insert(string("apple"));
  s2.insert(string("english"));
  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最后一個元素的尾端cout<<"result of set_intersection s1,s2:"<<endl;
  string *first = str;
  while(first<end)
  cout <<*first++<<" ";
  cout<<endl<<endl<<"result of set_union of s1,s2"<<endl;
  end = std::set_union(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//并集 first = str;
  while(first<end)
  cout <<*first++<<" ";
  cout<<endl<<endl<<"result of set_difference of s2 relative to 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<<"result of set_difference of s1 relative to 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;
  }

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多