国产探花免费观看_亚洲丰满少妇自慰呻吟_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/

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泰顺县| 多伦县| 尼木县| 太保市| 正镶白旗| 江阴市| 永寿县| 同心县| 金湖县| 岚皋县| 黎城县| 黑河市| 抚顺市| 临江市| 剑阁县| 仁布县| 乌拉特中旗| 互助| 布尔津县| 巧家县| 襄城县| 达拉特旗| 吉木乃县| 奉化市| 小金县| 沙洋县| 黎城县| 邢台市| 常山县| 舞钢市| 揭阳市| 扶沟县| 靖边县| 剑阁县| 图们市| 吐鲁番市| 合山市| 乌拉特中旗| 武陟县| 泸定县| 广河县|