国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

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

2020-01-26 13:57:19
字體:
來源:轉載
供稿:網友

一、簡介

集合(Set)是一種包含已排序對象的關聯容器,不允許有重復元素。

二、完整程序代碼

/*請務必運行以下程序后對照閱讀*/  #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容器不提供下標操作符   for (iter = num.begin(); iter != num.end(); iter++)     cout << *iter << " " ;   cout << endl;   cout << endl;    ///4. 查詢   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(); } 

三、補充

map容器是鍵-值對的集合,好比以人名為鍵的地址和電話號碼。相反地,set容器只是單純的鍵的集合。當我們想知道某位用戶是否存在時,使用set容器是最合適的。

參考網址:http://www.cplusplus.com/reference/set/set/

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白玉县| 安乡县| 平原县| 英山县| 澎湖县| 高陵县| 泉州市| 南澳县| 通城县| 巴里| 库伦旗| 随州市| 天镇县| 木里| 石棉县| 钦州市| 会昌县| 太仓市| 定州市| 泰安市| 泰兴市| 白朗县| 和田市| 达尔| 达日县| 阳泉市| 台北市| 布尔津县| 阿克| 阿克苏市| 楚雄市| 崇信县| 扎囊县| 旺苍县| 莱西市| 米脂县| 德江县| 宽甸| 荥经县| 德安县| 池州市|