日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

常用的STL

發布時間:2025/6/17 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 常用的STL 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
map?
? ? 容器和數組一樣,不過比較活用,相當于直接離散化數組
map<int ,int>mp 一維int
map<string ,string>mp 一維 str
map<int,map<int ,int> >mp 二維,經常大數hash時候用
map<int ,multiset<int> >mp 一對多的關系,相當于一個離散化并且隨時可以刪除指定元素,獲得元素個數的靈活二維容器....
??
set
? ? 是個容器,有集合的性質,不弄存重復的數,如下


函數
.size()//當前容器中有多少個元素
.insert()//插入元素
.clear()//清空容器
.erase()//刪除元素
.count()//返回該元素個數 ,(0 ,1)
.empty()//判空
*.lower_bound()//返回大于等于參數的數 ?***//


常用
? ?
?
multiset


.size()//當前容器中有多少個元素
.insert()//插入元素 ?//可以存重復的元素
.clear()//清空容器
.erase()//刪除元素 ?//刪除一個或多個還有該值的


元素
.count()//返回該元素個數?
.empty()//判空
*.lower_bound()//返回大于等于參數的數?




迭代器?
map<string,string>::iterator iter;


map<int ,int>::iterator iter;?


multiset<int>::iterator iter;


for(iter=mp1.begin();iter!=mp1.end(); iter++)






全排列


這是一個求一個排序的下一個排列的函數,可以遍歷


全排列,要包含頭文件<algorithm>
下面是以前的筆記 ? ?與之完全相反的函數還有


prev_permutation
?
?
(1) int 類型的next_permutation
?
int main()
{
?int a[3];
?a[0]=1;a[1]=2;a[2]=3;
?do
{
? cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;?
} ?
while (next_permutation(a,a+3)); //參數3指的


是要進行排列的長度
?
//如果存在a之后的排列,就返回true。如果a是最后


一個排列沒有后繼,返回false,每執行一次,a就變


成它的后繼
?
}
?
輸出:
?
?1 2 3
?1 3 2
?2 1 3
?2 3 1
?3 1 2
?3 2 1
?
?
如果改成 while(next_permutation(a,a+2));
則輸出:
?1 2 3
?2 1 3
?
只對前兩個元素進行字典排序
顯然,如果改成 while(next_permutation(a,a


+1)); 則只輸出:1 2 3
?
?
?
若排列本來就是最大的了沒有后繼,則


next_permutation執行后,會對排列進行字典升序排


序,相當于循環
?
int list[3]={3,2,1};
next_permutation(list,list+3);
cout<<list[0]<<" "<<list[1]<<" "<<list[2]


<<endl;
?
//輸出: 1 2 3


?
?
?
?
(2) char 類型的next_permutation
?
int main()
{
?char ch[205];
?cin >> ch;
?
sort(ch, ch + strlen(ch) );
//該語句對輸入的數組進行字典升序排序。如輸入


9874563102 cout<<ch; 將輸出0123456789,這樣就


能輸出全排列了
?
?char *first = ch;
?char *last = ch + strlen(ch);
?
?do {
? ? ?cout<< ch << endl;
? ? }
? ? while(next_permutation(first, last));
? ? return 0;
}
?
//這樣就不必事先知道ch的大小了,是把整個ch字符


串全都進行排序
//若采用 while(next_permutation(ch,ch+5)); 如


果只輸入1562,就會產生錯誤,因為ch中第五個元素


指向未知
//若要整個字符串進行排序,參數5指的是數組的長


度,不含結束符


?
?
?
?
?
(3) string 類型的next_permutation
?
int main()
{
? string line;
? while(cin>>line&&line!="#")
?{
? ?if(next_permutation(line.begin(),line.end


())) //從當前輸入位置開始
? ?cout<<line<<endl;
? ?else cout<<"Nosuccesor\n";
? }
}
?
?
?
int main()
{
? string line;
?while(cin>>line&&line!="#")
?{
? ?sort(line.begin(),line.end());//全排列
? ?cout<<line<<endl;
? ?while(next_permutation(line.begin


(),line.end()))
? ?cout<<line<<endl;
? }
}
?
?
next_permutation 自定義比較函數
?
#include<iostream> //poj 1256 Anagram
#include<string>
#include<algorithm>
using namespace std;
int cmp(char a,char b)?


//'A'<'a'<'B'<'b'<...<'Z'<'z'.
{
?if(tolower(a)!=tolower(b))
?return tolower(a)<tolower(b);
?else
?return a<b;
}
int main()
{
?char ch[20];
?int n;
?cin>>n;
?while(n--)
{
scanf("%s",ch);
sort(ch,ch+strlen(ch),cmp);
?do
{
printf("%s\n",ch);
}while(next_permutation(ch,ch+strlen


(ch),cmp));
}
?return 0;
}?




? ? ? ?






?

總結

以上是生活随笔為你收集整理的常用的STL的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。