STL容器和一些常用的函数
STL容器
迭代器vector< int >::iterator it=a.begin()
- erase()函数(STL容器可以用2,3条。先用二分查找找到要擦除元素的迭代器,然后擦除)
(1)string& erase ( size_t pos = 0, size_t n = npos );删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)iterator erase ( iterator position );删除position处的一个字符(position是个string类型的迭代器)
(3)iterator erase ( iterator first, iterator last );删除从first到last之间的字符(first和last都是迭代器)
string
string s;
getline(cin,s)注意这个会读取上一个的回车,会清空当前回车。所有如果前面有回车记得用getchar()函数清除
- s.length()/size()
- s.substr(pos,len)
- s.erase(begin,end)/§/(pos,len)/s.clear()
- s.find(‘x’)
stringstream
-
构造方法
注意:利用第一种构造函数创建对象时,输入字符串后直接进行字符串拼接,而第二种构造方式,在进行字符串拼接时,首先把原本的字符串覆盖掉,之后再进行拼接。 -
用str()来清空修改内容
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream ss("fghewoo");
cout << ss.str() << endl;
// 修改内容
ss.str("123456");
cout << ss.str() << endl;
// 清空内容
ss.str("");
cout << ss.str() << endl;
return 0;
}
/*
输出:
fghewoo
123456
*/
- 指定分隔符来分割字符串
默认空格
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream ss("2 dfjho 43");
cout << ss.str() << endl;
cout<< endl;
string str;
while (ss >> str)
{
cout << str << endl;
}
return 0;
}
/*
输出:
2 dfjho 43
2
dfjho
43
*/
这里’,'是分隔符
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string source = "abc,123,<!>";
stringstream ss(source);
cout << ss.str() << endl;
cout<< endl;
string str;
while (getline(ss, str, ','))
{
cout << str << endl;
}
return 0;
}
/*
输出:
abc,123,<!>
abc
123
<!>
*/
algorithm中常用函数
前置常识:A.begin(),A.end()是指从begin到end-1位置。因为下标是从0开始,end是最后一位数的后一位地址。
二分查找 O(lgn)
- it = lower_bound(begin,end,num)或者A.lower_bound(num)
找到第一个大于等于num数的地址。用*it输出这个数。
示例代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
vector<int> a;
for (int i = 1; i < 4; i++)
a.push_back(2 * i);//有序的2,4,6
vector<int>::iterator it = lower_bound(a.begin(), a.end(), 3);
//迭代器定位到4的位置
cout << *it << endl; //输出4
int x = lower_bound(a.begin(), a.end(), 3) - a.begin();
// int x=it-a.begin();//同上面一样
//在vector中也可以像数组那样定位下标
cout << x<<endl;//输出1 返回4的下标
int b[4]={1,3,5,7};
int * k = lower_bound(b,b+3,2);
//在数组b中的0~2下标里查找2,注意不是0~3下标
//b+3代表从b、b+1、b+2这三个数中查找
//就像a.end(),返回一个迭代器不指向实际的元素,而是表示末端元素的下一个元素
cout<<(k-b)<<endl;//输出1,即3的下标
cout<<*k<<endl;//输出3,在数组中也可以像容器中那样使用
//或者像下面这样用也可以,结果相同
/*
int k =* lower_bound(b,b+3,2);
cout<<k<<endl;
*/
if (binary_search(b, b + 4, 7)) cout << "查找成功!" << endl;
//输出“查找成功!”
}
2.upper_bound(begin,end,num)
表示在begin和end内返回第一个大于num数的地址(指针)
3.binary_search(begin,end,num)
表示在begin和end内是否存在num,是返回true,否返回false。类似count
find(begin,end,num)函数
和二分函数一样都是返回地址。
示例代码:
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int a[5] = {11,22,33,44,55}
int x=33;
int *p = find(a,a+5,x);//定义指针,指向查找完成后返回的地址,5为a2数组长度
if(((p-a) != 5) && (*p == x))//若同时满足这两个条件,则查找成功,输出
cout << (p-a+1);//输出所在位置(从1开始) 即输出3
return 0;
}
全排列函数 O(n)
-
next_permutation(begin,end)
求当前排列的下一个排列,如果没有下一个排列返回false,否则返回true。 -
prev_permutation(begin,end)
求当前排列的上一个排列,如果没有上一个排列返回false,否则返回true。 -
可以带cmp参数,自定义排序函数
示例代码:
#include<iostream> //poj 1256 Anagram
#include<string>
#include<algorithm>
using namespace std;
int cmp(char a,char b) //自定义字典序
{
if(tolower(a)!=tolower(b))//tolower 是将大写字母转化为小写字母.
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;
}
max()、min()、abs()、floor()、ceil()、round()函数
注意:fabs()是cmath中的函数,求浮点数的绝对值
sort(begin,end,cmp)函数
可以自己定义排序的优先级
swap(t[i],t[j])函数
reverse(begin,end)函数
fill(begin,end,num)函数
将begin到end之间的元素填充为num。
count(begin,end,num)函数
查找begin到end内num出现的次数。
__gcd(a,b)函数
注意:有两个_符号,求a,b的最大公因数