STL
vector:可变长数组
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int>v1;
vector<vector<int> >v2;
vector<int>v22[100];
vector<int>::iterator it;
}
set:内部自动有序且不含重复元素的容器
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int>st;
set<set<int> >st2;
set<int>st22[100];
set<int>::iterator it;
}
string:字符串类型
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "hello-world";
for (int i = 0; i < str.length(); i++)
{
printf("%c", str[i]);
}
cout << "\n";
string str1;
cin >> str1;
cout << str1<<endl;
string str2;
getline(cin, str2);
cout << str2<<endl;
printf("%s", str2.c_str());
char str3[100];
cin.getline(str3, 100);
cout << str3<<endl;
printf("%s\n", str3);
for (string::iterator it = str.begin(); it != str.end(); it++)
{
printf("%c", *it);
}
printf("\n");
string a = "hello";
string b = "world";
string c = a + b;
a += b;
cout << c << endl;
cout << a << endl;
b.insert(0, a);
cout << b << endl;
a.insert(a.begin(), b.begin(), b.end());
cout << a << endl;
a.erase(0, 3);
cout << a << endl;
cout<<a.substr(0, 3)<<endl;
string aa = "thank you very much!";
string bb = "you";
string cc = "weqw";
if (aa.find(bb) != string::npos)
cout << bb << endl;
if (aa.find(bb, 7) != string::npos)
cout << bb << endl;
if (aa.find(cc) != string::npos)
cout << cc << endl;
else cout << "no position" << endl;
string aaa = "maybe you will not turn arround.";
string bbb = "will not";
string ccc = "Surely";
cout << aaa.replace(10, 4, bbb) << endl;
cout << aaa.replace(aaa.begin(), aaa.begin() + 5, ccc);
system("pause");
return 0;
}
map:映射
#include <iostream>
#include <map>;
#include <set>
using namespace std;
int main()
{
map<int, char>ic;
map<string, int>si;
map<set<int>, string>sit;
ic[1] = 'c';
printf("%c", ic[1]);
si["nihao"] = 1;
si["weqwe"] = 2;
si["erre3"] = 0;
map<string, int>::iterator it = si.begin();
for (; it != si.end(); it++)
{
printf("%s %d", it->first, it->second);
}
}
队列
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<int>q;
}
stack
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int>s;
}
algorithm
函数 | 作用 |
---|
max(a,b) | 返回最大值 |
min(a,b) | 返回最小值 |
abs(int,int) | 求绝对值 |
swap(x,y) | 交换 |
reverse(a,a+4) | 对数组某范围进行反转 |
reverse(it1,it2) | 对string字符串进行反转 |
next_permutation(a,a+3) | 给出一个序列在全排列中的下一个序列 |
fill(a,a+5,233) | 给数组赋值 |
sort(a,a+3,cmp) | 排序 |
lower_bound(a,a+10,-1) | 在数组[a,a+10)范围内返回第一个大于-1的位置 |
upper_bound(a,a+10,-1) | 在数组[a,a+10)范围内返回第一个大于等于-1的位置 |
next_permutation()
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[10]={1,2,3};
do{
printf("%d%d%d\n",a[0],a[1],a[2]);
}while(next_permutation(a,a+3));
return 0;
}
lower_bound()&upper_bound()
int *lowerpos=lower_bound(a,a+10,-1);
int *upperpos=upper_bound(a,a+10,-1);
printf("%d,%d\n",lowerpos-a,upperpos-a);