STL初步——集合Set
生活随笔
收集整理的這篇文章主要介紹了
STL初步——集合Set
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
『寫在前面的一些基礎語法』
?
1.定義?和?賦值
- set<int> a={1,2};
- set<char> b={"1","2"};
- set<struct> c;
- set<vector<int>> d={a};?
2.Set的一些特性
- Set就是數學上的集合,所以每個元素是不出重復的
- 自動排序(根據元素類型不同排序規則不同)
3.一些基礎的操作
- a.insert(x) //插入一個元素x,若重則忽略
- 迭代器(iterator):
? ?? ?a.end();//返回的迭代器指向最后一個元素的后一個位置;
? ? ? a.begin();//¨返回的迭代器指向 set 中的最小值;
? ? ? a.rend;//返回的迭代器指向第一個元素的前一個位置;
? ? ? a.rbegin();//返回的迭代器指向 set 中的最大值;
- a.size();//成員的個數
- a.empty();//檢查集合是否為空
- a.clear();//清空集合
- a.erase(x);//刪除元素x
- a.find(x);//¨返回 x 元素的迭代器,如果找不到 x 就返回 end() 的 迭代器。
- a.count(x);//統計元素x在集合出現的次數(不常用)
- a.lower_bound(x) //返回 set 中大于等于 x 的最小元素的迭代器
- a.upper_bound(x) //返回 set 中大于 x 的最小元素的迭代器。 如果找不到也會返回 end() 的迭代器
4.來個雙胞胎
Multiset:最大的區別在于multiset中的元素可以重復
『上題上題』
?
安迪的第一個字典(UVa 10815)
【問題描述】
? ?輸入一個文本,找出所有不同的單詞(連續的字母序列),按字典序從小到大輸出。單詞不區分大小寫。
【樣例輸入】
?Adventures in Disneyland
?Two blondes were going to Disneyland when they came to a fork in the
?road. The sign read: "Disneyland Left."
?So they went home.
【樣例輸出】
?a
?adventures
?blondes
?came
?disneyland
?fork
?going
?home
?in
?left
?read
?road
?sign
?so
?the
?they
?to
?two
?went
?were
?when
『代碼代碼』
?
#include <iostream> #include <sstream> #include <bits/stdc++.h> #include <set> using namespace std; set<string> dict; int main(){string s,buf;while(cin>>s){for(int i = 0;i < s.length(); i++)if(isalpha(s[i])) s[i] = tolower(s[i]);else s[i] = ' ';//將每個不是英文字符直接變成空格stringstream ss(s);//從字符串s中提取每個單詞到ss中while(ss >> buf) //將ss中的每個單詞賦值給bufdict.insert(buf); //將每個buf插入到集合dict中}for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it)cout<<*it<<"\n";return 0; }『最后的一點點感悟』
?
- STL的確是神通廣大,集合多半用于去重和排序(排序時 元素要有規則可用重載了'<'運算符之類)
- 處理字符串時要將一些"不合規則的元素"變得統一起來
? ? ?1.大小寫的統一轉換
? ? ?2.多余元素的統一的轉化,例如本題中就將非英文字符轉換為了空格
- 牛羊的電腦壞了,五百軟妹幣又么得了,233333
總結
以上是生活随笔為你收集整理的STL初步——集合Set的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “POW'ER 2020 DEFI 创新
- 下一篇: [BZFZ友谊赛]火山喷发