string.empty() 函数
1 2 3
| string s1, s2 = "abcd"; cout << s1.empty() << endl; cout << s2.empty() << endl;
|
输出:
1
0
string.empty()
函数表示检查字符串是否为空,若为空,则返回1,若不为空,则返回0
string 的输入
1 2 3
| string s; getline(cin,s); cin>>s;
|
substr(begin,lenth) 函数和 find(“”) 函数
1 2 3 4 5 6
| string s1="abcdefg"; string s2=s1.substr(2,3); string s3=s1.substr(s1.find("efg"),3); cout<<s2<<endl; cout<<s3<<endl;
|
输出
cde
efg
string.substr(起始下标,长度)
,返回为该起始下标开始,该长度的子串(不写长度默认到结尾)
string.find("某子串")
,返回第一次出现该子串的下标(若没有,则返回-1)
增强型 for 循环
两种形式:
for (char c : s) 效率低
for (char &c : s) 效率高
for (int i : a) 效率低
for (int &i : a) 效率高
意思是c从字符串/数组的第一位开始遍历
1 2 3 4 5 6 7 8 9 10 11
| string s="abcdefg"; for (char &c : s) { cout<<c; } cout<<endl; int a[6] = { 3,2,1,4,9,0 }; for (int i : a) { cout << i; }
|
输出:
abcdefg
3 2 1 4 9 0
Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Class Person { private: int age; public: string name; }
int mian() { Person a; a.age=18; a.name="shabi" }
|
STL 容器
vector
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include <iostream> #include <vector> using namespace std; int main() { vector<int> a={1,2,3,4,5,6}; vector<int> b=a; vector<double> c[3]={{1,2,3},{4,5,6},{7,8,9}}; cout<<a.front()<<' '<<a.back()<<endl; cout<<a[4]<<endl; cout<<b[3]<<endl; cout<<c[1][2]<<endl; cout<<b.size()<<endl; cout<<b.empty()<<endl; a.clear(); for(int x:b) { cout<<x<<' '; } cout<<endl; b.push_back(7); for(int i=0;i<b.size();i++) { cout<<b[i]<<' '; } b.pop_back(); for(int x:b) { cout<<x<<' '; } return 0; }
|
queue 队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include<iostream> #include<queue> using namespace std; int main() { queue<int> a; a.push(123); a.push(321); a.push(231); cout<<a.back()<<endl; a.pop(); cout<<a.front()<<' '<<a.back()<<endl; cout<<a.empty()<<endl; cout<<a.size()<<endl;
priority_queue<int> b; b.push(123); b.push(321); b.push(231); b.pop(); cout<<b.top()<<endl;
return 0; }
|
stack 栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include<iostream> #include<stack> using namespace std; int main() { stack<int> stk; stk.push(123); stk.push(456); stk.push(789);
stk.pop(); cout<<stk.top()<<endl; return 0; }
|
deque 双端队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include<iostream> #include<stack> using namespace std; int main() { deque<int> a; a.push_back(123); a.push_back(321); a.push_front(231); a.begin(),a.back(); cout<<*a.begin()<<' '<<*(a.end()-1)<<endl; cout<<a.front()<<' '<<a.back()<<endl; cout<<a[2]<<endl; a.pop_front(); a.pop_back(); a.clear(); cout<<a.empty()<<endl; return 0; }
|
set 集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include<iostream> #include<set> #include<cstring> using namespace std; int main() { set<int> a; set<string> str={{"abc"},{"def"}}; a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(1); a.insert(1); cout<<*a.begin()<<' '<<*a.end()<<endl; cout<<*a.find(2)<<endl; a.erase(a.find(2)); cout<<a.count(2)<<endl; cout<<str.count("abc")<<endl; a.clear(); cout<<a.empty()<<endl;
multiset<int> b={1,5,2,3,4,1,1,2}; for(int i:b) { cout<<i<<' '; } cout<<endl; return 0; }
|
map 映射
map<key_type, value_type> name;
类比二元关系<x,y>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include<iostream> #include<map> #include<cstring> using namespace std; int main() { map<string, int> a; a["00"]=0; a["01"]=1; a["10"]=2; a["11"]=3; a.insert({"111",7}); string key="111"; cout<<a[key]<<endl; cout<<a.find("11")->first<<endl; cout<<a.find("11")->second<<endl; return 0; }
|
algorithm 算法库
sort 排序
sort(a.begin(),a.end())
默认从小到大
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include<iostream> #include<algorithm> #include<vector> using namespace std;
bool cmp(int a,int b) { return a>b; }
int main() { int a[]={2,4,9,3,1,7,5,6,8,1,1}; sort(a,a+11); for(int &i:a) { cout<<i<<' '; } cout<<endl;
vector<int> b({2,4,9,3,1,7,5,6,8,1,1}); sort(b.begin(),b.end(),cmp); for(int &i:b) { cout<<i<<' '; } cout<<endl;
vector<int> c({2,4,9,3,1,7,5,6,8,1,1}); sort(c.begin(),c.end(),greater<int>()); for(int &i:c) { cout<<i<<' '; } cout<<endl; return 0; }
|
reverse 翻转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { int a[]={1,2,3,4,5,6,7,8,9}; reverse(a,a+9); for(int &i:a) { cout<<i<<' '; } cout<<endl; vector<int> b({1,2,3,4,5,6,7,8,9}); reverse(b.begin(),b.end()); for(int &i:b) { cout<<i<<' '; } cout<<endl; return 0; }
|
输出
9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
unique 去重
unique
是把不重复的元素提取,然后放到开头,再返回去重之后末尾元素的下一个位置的迭代器
例:1, 1, 2, 2, 3, 4, 4 → 1, 2, 3, 4, 未知, 未知, 未知
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| #include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { int a[]={1,3,2,1,3,4,5,5,4,2}; sort(a,a+10); int num=unique(a,a+10)-a; cout<<num<<endl; for(int &i:a) { cout<<i<<' '; } cout<<endl;
vector<int> b({1,3,2,1,3,4,5,5,4,2}); sort(b.begin(),b.end()); b.erase(unique(b.begin(),b.end()),b.end()); for(int &i:b) { cout<<i<<' '; } cout<<endl; return 0; }
|