最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++ STL入門(mén)教程(6) set(集合)的使用方法

 更新時(shí)間:2017年08月18日 17:19:12   作者:synapse7  
這篇文章主要為大家詳細(xì)介紹了C++ STL入門(mén)教程的第六篇,set集合的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、簡(jiǎn)介

集合(Set)是一種包含已排序?qū)ο蟮年P(guān)聯(lián)容器,不允許有重復(fù)元素。

二、完整程序代碼

/*請(qǐng)務(wù)必運(yùn)行以下程序后對(duì)照閱讀*/ 
 
#include <set> 
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  ///1. 初始化 
  set<int> num; 
  set<int>::iterator iter; 
  cout << num.max_size() << endl;///set容納上限 
  cout << endl; 
 
  ///2. 添加元素 
  for (int i = 0; i < 10; i++) 
    num.insert(i); 
  cout << num.size() << endl; 
  cout << endl; 
 
  ///3. 遍歷 
  ///不同于map,set容器不提供下標(biāo)操作符 
  for (iter = num.begin(); iter != num.end(); iter++) 
    cout << *iter << " " ; 
  cout << endl; 
  cout << endl; 
 
  ///4. 查詢(xún) 
  iter = num.find(1); 
  if (iter != num.end()) 
    cout << *iter << endl; 
  else 
    cout << -1 << endl; 
  iter = num.find(99); 
  if (iter != num.end()) 
    cout << *iter << endl; 
  else 
    cout << -1 << endl; 
  cout << endl; 
 
  ///5. 刪除 
  iter = num.find(1); 
  num.erase(iter); 
  cout << num.size() << endl; 
  for (iter = num.begin(); iter != num.end(); iter++) 
    cout << *iter << " " ; 
  cout << endl; 
  cout << endl; 
 
  ///6. 判空與清空 
  if (!num.empty()) 
    num.clear(); 
} 

三、補(bǔ)充

map容器是鍵-值對(duì)的集合,好比以人名為鍵的地址和電話(huà)號(hào)碼。相反地,set容器只是單純的鍵的集合。當(dāng)我們想知道某位用戶(hù)是否存在時(shí),使用set容器是最合適的。

參考網(wǎng)址:http://www.cplusplus.com/reference/set/set/

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

色达县| 安化县| 河北省| 潢川县| 达日县| 廉江市| 衡阳县| 西和县| 荆门市| 镇江市| 万山特区| 沙湾县| 松溪县| 宁明县| 千阳县| 博爱县| 甘德县| 平邑县| 中卫市| 安阳县| 运城市| 曲靖市| 万源市| 龙海市| 沾益县| 广丰县| 邛崃市| 隆林| 上高县| 乌兰浩特市| 绥滨县| 九龙坡区| 涟源市| 桐庐县| 宜昌市| 科技| 吉林省| 靖江市| 山阳县| 砚山县| 罗山县|