一、字符串处理 1.字符输入 (1) cin 用法一:接收一个字符串,无论是char a[10],还是string b 都一样,遇到”Space”,”Tab”,”Enter”都会结束
1 2 3 4 5 6 7 8 9 10 11 12 #include <bits/stdc++.h> using namespace std;int main () { char a[10 ]; string b; cin>>a; cin>>b; cout<<a<<endl; cout<<b<<endl; return 0 ; }
(2) cin.get() 用法一:用 ch = cin.get() 或 cin.get(ch) 来接收单个字符
1 2 3 4 5 6 7 8 9 #include <bits/stdc++.h> using namespace std;int main () { char ch; ch = cin.get (); cout<<ch<<endl; return 0 ; }
用法二:用cin.get(a, n),其中a是字符数组名,n是要输入的字符个数,可接收空格
注意1:不能用于string类型,只能输入字符数组
注意2:假如定义cin(a, 20),实际只能输入19个字符
1 2 3 4 5 6 7 8 9 #include <bits/stdc++.h> using namespace std;int main () { char a[20 ]; cin.get (a,10 ); cout<<a<<endl; return 0 ; }
用法三:无参数调用,主要用于舍弃输入流中不需要的字符,或用于吃掉回车,相当于getchar()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <iostream> using namespace std;int main (void ) { char arr[10 ]; cin.get (arr,10 ); cin.get (); cout<<arr<<endl; cin.get (arr,5 ); cout<<arr<<endl; system ("pause" ); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> using namespace std;int main (void ) { char arr[10 ]; cin.get (arr,10 ); cout<<arr<<endl; cin.get (arr,5 ); cout<<arr<<endl; system ("pause" ); return 0 ; }
(3) cin.getline() 用法一:调用、功能、注意点 和 cin.get() 的第二种用法一模一样
1 2 3 4 5 6 7 8 9 #include <bits/stdc++.h> using namespace std;int main () { char a[20 ]; cin.getline (a,10 ); cout<<a<<endl; return 0 ; }
用法二:cin.getline(m, n, ch),其中m表示字符数组位置,n表示接收字符个数,ch表示结束字符
在多维数组当中,m可以是a[i]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <iostream> #include <string> using namespace std;int main () { char m[3 ][20 ]; for (int i=0 ;i<3 ;i++) { cout<<"\n请输入第" <<i+1 <<"个字符串:" <<endl; cin.getline (m[i],20 ); } cout<<endl; for (int j=0 ;j<3 ;j++) cout<<"输出m[" <<j<<"]的值:" <<m[j]<<endl; system ("pause" ); return 0 ; }
(4) getline() 用法一:需要加头文件,调用 getline(cin, a)输入字符串或字符数组,弥补了前两种输入无法处理string类型的弊病
1 2 3 4 5 6 7 8 9 10 #include <iostream> #include <string> using namespace std;int main () { string s; getline (cin,s); cout << s << endl; return 0 ; }
(6) 总结
字符数组
字符串
能否输入空格
备注
cin
✔️
✔️
❌
调用简单
cin.get()
✔️
❌
✔️
可用于定量输入字符数组
cin.getline()
✔️
❌
✔️
可用于多维字符数组输入
getline()
❌
✔️
✔️
可用于字符串输入
getchar()
❌
❌
✔️
可用于吞空格,尽量少用
2.字符串string (1) string 初始化 1 2 3 string str ("123" ) ; string str (3 ,'1' ) ; string str = "123" ;
(2) string 首尾字符 1 2 3 4 5 str.front (); str.back (); str.size () str.length ()
(3) string 迭代器 1 2 3 string::iterator iter str.begin () str.end ()
(4) string 插入与删除 1 2 3 4 5 6 7 str.push_back ('a' ); str.insert (str.begin (), 'a' ); str.pop_back (); str.erase (str.begin ()); str.erase (str.begin ()+1 , str.end ()-2 ); str.erase (k, n);
(5) string 替换和拼接 1 2 3 4 5 6 7 str.replace (str.begin (), str.begin + 5 , "boy" ); str.replace (k, n, "girl" ); str1. append (str2); str1 = str1 + str2; str.substr (k, n);
(6) string 遍历 1 2 3 4 5 6 7 for (int i = 0 ; i < str.size (); i ++ ) for (string::iterator iter = str.begin (); iter != str.end (); iter ++ ) for (auto iter = str.begin (); iter != str.end (); iter ++ ) for (auto x : str) for (auto &x : str)
(7) string 排序和比较 1 2 3 4 sort (str.begin (), str.end ());str1 < str2 str1. compare (str2)
(8) string 查找 1 2 3 4 5 6 7 8 9 10 str.find ("123" ) str.rfind ("123" ) str.find_first_of ("123" ) str.find_first_not_of ("123" ) str.find_last_of ("123" ) str.find_last_not_of ("123" ) str.count ('a' )
(9) string 判空和清空 1 2 str.empty (); str.clear ();
二、STL容器库 1. vector (1) vector 初始化 1 2 3 4 5 6 7 8 9 10 11 vector<int > v (n) ; vector<int > v (10 ,1 ) ; vector<int > a (b) ; vector<int > a (b.begin(),b.begin+3 ) ; int b[7 ]={1 ,2 ,3 ,4 ,5 ,9 ,8 };vector<int > a (b,b+7 ) ; int rows = 3 , cols = 4 ;vector<vector<int >> arr (rows, vector <int >(cols, 0 )); arr[1 ][2 ] = 5 ;
(2) vector 调用元素 1 2 3 4 v.front () v.back () v.size () v[i]
(3) vector 迭代器 1 2 3 vector::iterator iter v.begin () v.end ()
(4) vector 插入和删除 1 2 3 4 5 6 7 8 v.push_back (a); v.insert (v.begin (), a); v.insert (v.begin (),k, a); v.pop_back (); v.erase (v.begin ()); v.erase (v.begin ()+1 , v.end ()-2 ); v.erase (k, n);
(5) vector 遍历 1 2 3 4 for (int i = 0 ; i < v.size (); i ++ ) for (vector::iterator iter = v.begin (); iter != v.end (); iter ++ ) for (auto iter = v.begin (); iter != v.end (); iter ++ )
(6) vector 判空和清空
(7) vector 一些算法 1 2 3 4 5 6 7 sort (v.begin (), v.end ()); reverse (v.begin (), v.end ()) a.swap (b); find (v.begin (),v.end (),k);
2.queue (1) queue 调用 1 2 3 q.push (2 ); q.pop (); q.empty ()
(2) priority_queue 调用 priority_queue容器相当于大根堆(或者小根堆)
大根堆每次堆顶是最大元素,小根堆每次堆顶是最小元素
1 2 3 4 5 6 7 8 priority_queue<int > q; priority_queue<int , vector<int >, greater<int >> q; q.push (2 ); q.pop (); q.top (); q.empty ();
(3) deque 调用 与vector相比,deque在头部增删元素仅需要 O(1)的时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 dq.front (); dq.back (); dq.begin (); dq.end (); dq.push_front (2 ); dq.push_back (2 ); dq.pop_front (); dq.pop_back (); dq.empty (); dq.clear ();
3.stack (1) stack 调用 1 2 3 4 sk.push (2 ); sk.pop (); sk.top (); sk.empty ()
4.set 集合分为去重元素集合set和可重元素集合multiset,用法相同
set为有序集合,unordered_set为无序集合(依赖哈希函数实现)
(1) set 插入和删除 1 2 3 4 5 6 7 st.insert (2 ); st.erase (st.begin ()); st.erase (2 ); st.empty (); st.clear ();
(2) set 一些算法 1 2 3 4 5 st.find (2 ) st.lower_bound (x) st.upper_bound (x) st.count (2 );
5.map 表分为去重元素表map和可重元素表multimap,用法相同
map为有序表,unordered_map为无序表(依赖哈希函数实现)
(1) map 调用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 map<key_type, value_type> mp; map<int , int > mp; map<int , int >::iterator iter mp.begin () mp.end () mp.begin ().first (*mp.begin ()).first mp.begin ()->first mp[2 ] = 5 ; mp.insert (pair <int , int >(2 , 5 )); mp.erase (iter); mp.find (2 ); mp.count (2 ); mp.empty (); mp.clear ();
6.bitset (1) bitset 调用 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 bitset<4> bt; bitset<8> bt (12 ) ; bitset<8> bt (str) ; bt1 |= bt2; bt1 &= bt2; bt1 ^= bt2; bt1 = ~bt1; bt1 <<= x; bt.test (x) bt.flip (); bt.flip (x); bt.set (); bt.set (x); bt.reset (); bt.reset (x); bt.size () bt.count (); string str = bt.to_string ();
三、二进制 1. 位处理 (1) 求不小于x的最小2次幂 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 unsigned long long up2pow (unsigned long long x) { if (x <= 1 ) return 1 ; x--; x |= x >> 1 ; x |= x >> 2 ; x |= x >> 4 ; x |= x >> 8 ; x |= x >> 16 ; x |= x >> 32 ; return x + 1 ; } unsigned long long down2pow (unsigned long long x) { if (x == 0 ) return 0 ; x |= x >> 1 ; x |= x >> 2 ; x |= x >> 4 ; x |= x >> 8 ; x |= x >> 16 ; x |= x >> 32 ; return (x + 1 ) >> 1 ; }
(2) 求x的二进制长度 1 2 3 4 5 6 7 8 int bitlen (unsigned long long x) { int len = 0 ; while (x){ len++; x >>= 1 ; } return len; }