常用的STL
常用 STL
2023-09-11 14:13:59 时间
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;
}
容器和数组一样,不过比较活用,相当于直接离散化数组
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;
}
相关文章
- Java虚拟机详解03----常用JVM配置参数
- Sublime Text 3 常用插件以及安装方法(转)
- Linux Ubuntu 22.04常用系统快捷键
- Android 常用网络图片url地址
- ML之FE:特征工程/数据预处理之构造特征之特征分箱/数据分桶的常用六大类方法—基于统计(等距/等频+分位数+标准差/f方差)、基于数据分布(自然断点+重尾分布)、基于评价指标的自适应(卡方/Bes
- mysql常用基础操作语法(九)~~外连接查询【命令行模式】
- Computer:敏捷开发(敏捷式开发)的价值观、原则、经验、常用开发方法(XP极限编程/Crystal水晶/Scrum)之详细攻略
- C/C++ Qt 常用数据结构
- isNotBlank()和isNotEmpty()的区别——java中,StringUtils类中的一些常用方法
- STL源代码剖析 容器 stl_list.h
- STL 源代码剖析 算法 stl_algo.h -- inplace_merge
- 神州数码无线设备常用配置
- C++ STL标准库常用泛型算法
- nltk常用函数
- 程序员常用快捷网站
- idea中常用的快捷键以及一些奇淫技巧 , 加快我们的开发效率
- 黑马C++笔记——STL常用算法
- C++,STL -- 函数对象、常用算法
- C++ , STL ---- 常用容器
- Linux常用快捷键