初始化
C++ 复制 #include <string>
#include <iostream>
#include <vector>
int main (){
int a1; std ::string s1; std ::vector<int> v1; // 默认初始化
int a2 (1 ); std ::string s2 ("abcde" ); std ::vector<int> v2 (3 ,3 ); // 直接初始化
std ::cout<< a2<< " " << s2<< " " ; for (auto i:v2){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
int a3= 1 ; std ::string s3= "abcde" ; std ::vector<int> v3= {3 ,3 ,3 }; // 拷贝初始化
std ::cout<< a3<< " " << s3<< " " ; for (auto i:v3){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
int a4{1 }; std ::string s4{"abcde" }; std ::vector<int> v4{3 ,3 ,3 }; // 列表初始化
std ::cout<< a4<< " " << s4<< " " ; for (auto i:v4){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
}
数学
C++ 复制 #include <cmath>
#include <iostream>
#include <algorithm>
int main () {
std ::cout<< std ::abs (- 10 )<< std ::endl;
std ::cout<< std ::fabs (- 3.14 )<< std ::endl;
std ::cout<< std ::round (2.5 )<< std ::endl;
std ::cout<< std ::llround (3000000000.5 )<< std ::endl;
std ::cout<< std ::ceil (2.1 )<< std ::endl;
std ::cout<< std ::floor (2.9 )<< std ::endl;
std ::cout<< std ::sqrt (25.0 )<< std ::endl;
double pi= std ::acos (- 1.0 );
std ::cout<< std ::sin (pi/ 2 )<< std ::endl;
std ::cout<< std ::cos (0.0 )<< std ::endl;
std ::cout<< std ::tan (pi/ 4 )<< std ::endl;
std ::cout<< std ::asin (- 1.0 )<< std ::endl;
std ::cout<< std ::acos (- 1.0 )<< std ::endl;
std ::cout<< std ::atan (- 1.0 )<< std ::endl;
std ::cout<< std ::log (10.0 )<< std ::endl;
std ::cout<< std ::log2 (16.0 )<< std ::endl;
std ::cout<< std ::log10 (100.0 )<< std ::endl;
std ::cout<< std ::exp (1.0 )<< std ::endl;
std ::cout<< std ::exp2 (3.0 )<< std ::endl;
std ::cout<< std ::pow (3.0 ,3.0 )<< std ::endl;
std ::cout<< std ::__lg (8 )<< std ::endl;
std ::cout<< std ::__gcd (12 ,18 )<< std ::endl;
}
随机数
C++ 复制 #include <vector>
#include <iostream>
#include <algorithm>
#include <chrono>
#include <random>
int main (){
std ::mt19937 rnd (std ::random_device{}()); // 类型 unsigned int
std ::mt19937_64 rndll (std ::chrono ::steady_clock ::now ().time_since_epoch ().count ());
// 类型 unsigned long long
std ::cout<< rnd ()<< std ::endl;
std ::cout<< rndll ()<< std ::endl;
std ::vector<unsigned int> v;
v.push_back (rnd ());
v.push_back (rnd ());
v.push_back (rnd ());
v.push_back (rnd ());
v.push_back (rnd ());
for (unsigned int i:v){std ::cout<< i<< std ::endl;} std ::cout<< std ::endl;
std ::shuffle (v.begin (),v.end (),rnd); // 随机排列
for (unsigned int i:v){std ::cout<< i<< std ::endl;} std ::cout<< std ::endl;
}
文件读写
C++ 复制 #include <cstdio>
#include <fstream>
int main (){
freopen ("example1.in" ,"r" ,stdin);
freopen ("example1.out" ,"w" ,stdout);
// ...
fclose (stdin);
fclose (stdout);
std ::ifstream fin ("example2.in" );
std ::ofstream fout ("example2.out" );
// ...
fin.close ();
fout.close ();
fin.open ("example3.in" );
fout.open ("example3.out" );
// ...
fin.close ();
fout.close ();
}
Vector \text{Vector} Vector
构造函数
C++ 复制 std ::vector< T> v1; // 创建一个类型为 T 的空的 vector,时间复杂度常数
std ::vector <T > v2 (n ); // 创建一个初始空间为 n 且类型为 T 的 vector,时间复杂度线性
std ::vector <T > v3 (n ,m ); // 创建一个初始空间为 n、初始空间内部元素初始值为 m
// 且类型为 T 的 vector,时间复杂度线性
成员函数
C++ 复制 // push_back()
// at()
// operator []
// front()
// back()
// size()
// empty()
// pop_back()
// clear()
#include <iostream>
#include <vector> // 头文件
int main (){
std ::vector<int> v;
v.push_back (3 ); // 在末尾插入元素,均摊时间复杂度常数
v.push_back (2 ); // vector 空间不足时,需要扩展空间,时间复杂度线性
v.push_back (1 );
std ::cout<< v.at (1 )<< std ::endl; // 返回下标为`x`的元素,越界检查
// 越界抛出`std::out_of_range`类型
// RE,Received signal 6: Aborted / IOT trap.
std ::cout<< v[1 ]<< std ::endl; // 返回下标为`x`的元素,不越界检查
std ::cout<< v.front ()<< std ::endl; // 返回容器的首元素
std ::cout<< v.back ()<< std ::endl; // 返回容器的尾元素
v.at (1 )= 0 ; // 可以赋值
for (int i:v){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
v[1 ]= 1 ; // 可以赋值
for (int i:v){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
v.front ()= 2 ; // 可以赋值
for (int i:v){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
v.back ()= 3 ; // 可以赋值
for (int i:v){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
std ::cout<< v.size ()<< std ::endl; // 返回容器长度,时间复杂度常数
// 等价于 std::distance(v.begin(),v.end())
std ::cout<< v.empty ()<< std ::endl; // 容器为空返回 true,容器不为空返回 false
// 时间复杂度常数,等价于 v.begin()==v.end()
v.pop_back (); // 在末尾删除元素,时间复杂度常数
for (int i:v){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
v.clear (); // 清空容器,时间复杂度线性
std ::cout<< v.empty ()<< std ::endl;
}
Deque \text{Deque} Deque
构造函数
C++ 复制 std ::deque< T> v1; // 创建一个类型为 T 的空的 deque,时间复杂度常数
std ::deque <T > v2 (n ); // 创建一个初始空间为 n 且类型为 T 的 deque,时间复杂度线性
std ::deque <T > v3 (n ,m ); // 创建一个初始空间为 n、初始空间内部元素初始值为 m
// 且类型为 T 的 deque,时间复杂度线性
成员函数
C++ 复制 // push_front()
// push_back()
// operator []
// pop_front()
// pop_back()
// at()
// front()
// back()
// swap()
// clear()
// empty()
// size()
#include <deque> // 头文件
#include <iostream>
int main (){
std ::deque<int> d1;
std ::deque<int> d2;
d1.push_front (1 ); // 头部插入元素
d1.push_back (2 ); // 尾部插入元素
d1.push_front (3 );
d1.push_back (4 );
std ::cout<< d1[0 ]<< " " << d1[1 ]<< " " ; // 引用元素
std ::cout<< d1[2 ]<< " " << d1[3 ]<< std ::endl;
d1.pop_front (); // 头部删除元素
d1.pop_back (); // 尾部删除元素
std ::cout<< d1.at (0 )<< " " << d1.at (1 )<< std ::endl; // 引用元素
d1.front ()= 2 ; // 引用元素
d1.back ()= 1 ; // 引用元素
for (int i:d1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
d2.swap (d1); // 交换容器,时间复杂度常数
d2.clear (); // 清空容器
std ::cout<< d1.empty ()<< " " << d2.empty ()<< std ::endl; // 检查容器是否为空
std ::cout<< d1.size ()<< " " << d2.size ()<< std ::endl; // 返回容器大小
}
Iterator \text{Iterator} Iterator
迭代器
C++ 复制 #include <iostream>
#include <iterator> // 头文件
#include <vector> // vector,list 等容器已经包含了迭代器的定义
int main (){
std ::vector<int> v;
v.push_back (1 ); v.push_back (2 ); v.push_back (3 );
for (std ::vector <int >::iterator i= v.begin ();i!= v.end ();i++ ){
std ::cout<<* i<< " " ;
}
std ::cout<< std ::endl;
// begin() 返回指向首元素的迭代器,cbegin() 为相同作用的只读迭代器
// end() 返回指向尾元素后一位置的迭代器,cend() 为相同作用的只读迭代器
for (std ::vector <int >::reverse_iterator i= v.rbegin ();i!= v.rend ();i++ ){
std ::cout<<* i<< " " ;
}
std ::cout<< std ::endl;
// rbegin() 返回指向逆向数组首元素的逆向迭代器,即正向数组的末尾元素
// crbegin() 为相同作用的只读迭代器
// rend() 返回指向逆向数组尾元素后一位置的迭代器,crend() 为相同作用的只读迭代器
for (std ::vector <int >::const_iterator i= v.cbegin ();i!= v.cend ();i++ ){
std ::cout<<* i<< " " ;
}
std ::cout<< std ::endl;
for (std ::vector <int >::const_reverse_iterator i= v.crbegin ();i!= v.crend ();i++ ){
std ::cout<<* i<< " " ;
}
std ::cout<< std ::endl;
}
Bitset \text{Bitset} Bitset
构造函数
C++ 复制 std ::bitset< 16 > b1; // 创建一个大小为 16 的 bitset
std ::bitset <16 > b2 (0b 10101010 ); // 创建一个大小为 16 的 bitset
// 整数二进制的最低位对应 bitset 的下标 0
std ::bitset <16 > b3 (std ::string ("11110000" )); // 创建一个大小为 16 的 bitset
// 字符串二进制的最低位对应 bitset 的下标 0
成员函数
C++ 复制 // operator []
// operator ==
// operator !=
// operator &
// operator &=
// operator |
// operator |=
// operator ^
// operator ^=
// operator ~
// operator <<
// operator >>
// operator <<=
// operator >>=
// cin
// cout
// count()
// size()
// test()
// to_string()
// to_ulong()
// to_ullong()
// flip()
// any()
// all()
// none()
// set()
// reset()
#include <bitset> // 头文件
#include <iostream>
int main (){
std ::bitset< 8 > b1 (0b 11001100 );
std ::bitset< 8 > b2 (0b 10101010 );
for (int i= 0 ;i< 8 ;i++ ){
std ::cout<< b1[i]<< " " ; // 访问下标,不越界检查
}
std ::cout<< std ::endl;
for (int i= 0 ;i< 8 ;i++ ){
std ::cout<< b2[i]<< " " ; // 访问下标,不越界检查
}
std ::cout<< std ::endl;
std ::cout<< (b1== b2)<< std ::endl; // 两个 bitset 相同返回 true,否则返回 false
std ::cout<< (b1!= b2)<< std ::endl; // 两个 bitset 不同返回 true,否则返回 false
std ::bitset< 8 > b3;
b3= b1& b2; // 支持位运算
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
b3= b1; b3&= b2; // 支持位运算
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
b3= b1| b2; // 支持位运算
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
b3= b1; b3|= b2; // 支持位运算
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
b3= b1^ b2; // 支持位运算
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
b3= b1; b3^= b2; // 支持位运算
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
b3=~ b1; // 支持位运算
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
b3= b1<< 1 ; // 支持位运算
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
b3= b1>> 1 ; // 支持位运算
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
b3= b1; b3<<= 1 ; // 支持位运算
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
b3= b1; b3>>= 1 ; // 支持位运算
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
std ::cin>> b3; // 例如 10100101,支持流式 IO,输入只有 0 和 1
// 输入二进制的最低位对应 bitset 的下标 0
std ::cout<< b3<< std ::endl; // 支持流式 IO,效果同 to_string()
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
std ::cout<< b3.count ()<< std ::endl; // 返回 true 的个数
std ::cout<< b3.size ()<< std ::endl; // 返回 bitset 的大小
for (int i= 0 ;i< 8 ;i++ ){
std ::cout<< b3.test (i)<< " " ; // 访问下标,越界检查
}
std ::cout<< std ::endl;
std ::cout<< b3.to_string ()<< std ::endl; // 转字符串
std ::cout<< b3.to_ulong ()<< std ::endl; // 转 unsigned long 类型的整数
std ::cout<< b3.to_ullong ()<< std ::endl; // 转 unsinged long long 类型的整数
b3.flip (); // 翻转每一位
b3.flip (0 ); // 翻转某一位
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
std ::cout<< b3.any ()<< std ::endl; // bitset 中有 true 返回 true,否则返回 false
std ::cout<< b3.all ()<< std ::endl; // bitset 中全 true 返回 true,否则返回 false
std ::cout<< b3.none ()<< std ::endl; // bitset 中全 false 返回 true,否则返回 false
b3.set (); // 每一位设置为 true
std ::cout<< b3.all ()<< std ::endl;
b3.reset (); // 每一位设置为 false
std ::cout<< b3.none ()<< std ::endl;
b3.set (1 ); // 某一位设置为 true
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
b3.set (1 ,false ); // 某一位设置为 true 或 false
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
b3.set (1 );
b3.reset (1 ); // 某一位设置为 false
for (int i= 0 ;i< 8 ;i++ ){std ::cout<< b3[i]<< " " ;} std ::cout<< std ::endl;
}
Tuple \text{Tuple} Tuple
C++ 复制 // std::make_tuple
// std::get
// swap()
// std::swap
// operator ==
// operator <=
// operator >=
// operator !=
// operator <
// operator >
// std::tie
// std::ignore
// std::tuple_cat
// operator =
#include <tuple> // 头文件
#include <iostream>
int main (){
std ::tuple<int ,int ,int> t1,t2; // 定义
t1= std ::make_tuple (3 ,2 ,1 ); // 创建 tuple
t2= std ::make_tuple (1 ,2 ,3 ); // 创建 tuple
std ::cout<< std ::get <0 >(t1)<< " " ; // 访问指定位置的元素
std ::cout<< std ::get <1 >(t1)<< " " ; // 访问指定位置的元素
std ::cout<< std ::get <2 >(t1)<< " " ; // 访问指定位置的元素
std ::cout<< std ::endl;
std ::cout<< std ::get <0 >(t2)<< " " ; std ::cout<< std ::get <1 >(t2)<< " " ;
std ::cout<< std ::get <2 >(t2)<< " " ; std ::cout<< std ::endl;
t1.swap (t2); // 替换
std ::cout<< std ::get <0 >(t1)<< " " ; std ::cout<< std ::get <1 >(t1)<< " " ;
std ::cout<< std ::get <2 >(t1)<< " " ; std ::cout<< std ::endl;
std ::cout<< std ::get <0 >(t2)<< " " ; std ::cout<< std ::get <1 >(t2)<< " " ;
std ::cout<< std ::get <2 >(t2)<< " " ; std ::cout<< std ::endl;
std ::swap (t1,t2); // 替换
std ::cout<< std ::get <0 >(t1)<< " " ; std ::cout<< std ::get <1 >(t1)<< " " ;
std ::cout<< std ::get <2 >(t1)<< " " ; std ::cout<< std ::endl;
std ::cout<< std ::get <0 >(t2)<< " " ; std ::cout<< std ::get <1 >(t2)<< " " ;
std ::cout<< std ::get <2 >(t2)<< " " ; std ::cout<< std ::endl;
std ::cout<< (t1== t2)<< std ::endl; // 运算符
std ::cout<< (t1<= t2)<< std ::endl; // 运算符,按字典序比较
std ::cout<< (t1>= t2)<< std ::endl; // 运算符,按字典序比较
std ::cout<< (t1!= t2)<< std ::endl; // 运算符
std ::cout<< (t1< t2)<< std ::endl; // 运算符,按字典序比较
std ::cout<< (t1> t2)<< std ::endl; // 运算符,按字典序比较
std ::get <0 >(t1)= 4 ; // 可以赋值
std ::get <1 >(t1)= 5 ;
std ::get <2 >(t1)= 6 ;
std ::cout<< std ::get <0 >(t1)<< " " ; std ::cout<< std ::get <1 >(t1)<< " " ;
std ::cout<< std ::get <2 >(t1)<< " " ; std ::cout<< std ::endl;
int x,y,z;
std ::tie (x,y,z)= t1; // 元组中的元素赋值给变量
std ::cout<< x<< " " << y<< " " << z<< std ::endl;
std ::tie (x,std ::ignore,y)= t2; // 元组中的元素赋值给变量,std::ignore 起到跳过元素作用
std ::cout<< x<< " " << y<< std ::endl;
std ::tuple<int ,int ,int ,int ,int ,int> t3= std ::tuple_cat (t1,t2); // 拼接元组
std ::cout<< std ::get <0 >(t3)<< " " ; std ::cout<< std ::get <1 >(t3)<< " " ;
std ::cout<< std ::get <2 >(t3)<< " " ; std ::cout<< std ::get <3 >(t3)<< " " ;
std ::cout<< std ::get <4 >(t3)<< " " ; std ::cout<< std ::get <5 >(t3)<< " " ;
std ::cout<< std ::endl;
t1= t2; // 元组可以赋值给元组
std ::cout<< std ::get <0 >(t1)<< " " ; std ::cout<< std ::get <1 >(t1)<< " " ;
std ::cout<< std ::get <2 >(t1)<< " " ; std ::cout<< std ::endl;
std ::cout<< std ::get <0 >(t2)<< " " ; std ::cout<< std ::get <1 >(t2)<< " " ;
std ::cout<< std ::get <2 >(t2)<< " " ; std ::cout<< std ::endl;
}
Set \text{Set} Set
C++ 复制 // std::greater
// std::multiset
// insert()
// emplace()
// find()
// count()
// lower_bound()
// upper_bound()
// erase()
// empty()
// size()
#include <set> // 头文件
#include <iostream>
int main (){
std ::set<int> s1; // 元素升序排列,不允许重复
std ::set<int ,std ::greater<int> > s2; // 元素降序排列,不允许重复
// 可以重载运算符 <
std ::multiset<int> s3; // 元素升序排列,允许重复
s1.insert (1 ); s2.insert (1 ); s3.insert (1 ); // 插入
s1.emplace (2 ); s2.emplace (2 ); s3.emplace (2 ); // 插入
bool b;
b= s1.insert (1 ).second; std ::cout<< b<< std ::endl; // insert 返回 std::pair<iterator,bool>
b= s2.insert (1 ).second; std ::cout<< b<< std ::endl; // insert 返回 std::pair<iterator,bool>
s3.insert (1 ); // insert 返回 iterator
for (int i:s1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
for (int i:s2){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
for (int i:s3){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
std ::set <int >::iterator it1= s1.find (1 ); // 返回元素的迭代器,否则返回 end()
std ::set <int , std ::greater <int >>::iterator it2= s2.find (1 ); // 返回元素的迭代器
// 否则返回 end()
std ::multiset <int >::iterator it3= s3.find (1 ); // 返回元素的迭代器,多个元素返回第一个
// 否则返回 end()
std ::cout<<* it1<< std ::endl;
std ::cout<<* it2<< std ::endl;
std ::cout<<* it3<< std ::endl;
it3++ ; std ::cout<<* it3<< std ::endl;
it3++ ; std ::cout<<* it3<< std ::endl;
std ::cout<< s1.count (1 )<< std ::endl; // 返回元素数量
std ::cout<< s2.count (1 )<< std ::endl; // 返回元素数量
std ::cout<< s3.count (1 )<< std ::endl; // 返回元素数量
it1= s1.lower_bound (1 ); // 返回第一个大于等于元素的迭代器
std ::cout<<* it1<< std ::endl;
it1= s1.upper_bound (1 ); // 返回第一个大于元素的迭代器
std ::cout<<* it1<< std ::endl;
s1.erase (1 ); // 清除元素
s2.erase (1 ); // 清除元素
s3.erase (1 ); // 清除元素,多个元素均清除
for (int i:s1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
for (int i:s2){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
for (int i:s3){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
s1.erase (s1.begin ()); // 按迭代器清除元素
std ::cout<< s1.empty ()<< std ::endl; // 检查容器是否为空
std ::cout<< s1.size ()<< std ::endl; // 返回容器大小
}
Map \text{Map} Map
C++ 复制 // operator []
// insert()
// emplace()
// at()
// find()
// erase()
// clear()
// empty()
// size()
// lower_bound()
// upper_bound()
#include <map> // 头文件
#include <iostream>
int main (){
std ::map<int ,int> m1;
std ::map<int ,int ,std ::greater<int> > m2;
std ::multimap<int ,int> m3;
m1[1 ]= 4 ; m1[2 ]= 3 ; // 插入,注意 multimap 没有 operator []
m2.insert ({1 ,4 }); // 插入
m2.insert ({2 ,3 });
m3.emplace (1 ,4 ); // 构造
m3.emplace (1 ,3 );
m3.emplace (2 ,3 );
m3.emplace (2 ,4 );
for (std ::pair<int ,int> i:m1){std ::cout<< i.first<< " " << i.second<< std ::endl;}
for (std ::pair<int ,int> i:m2){std ::cout<< i.first<< " " << i.second<< std ::endl;}
for (std ::pair<int ,int> i:m3){std ::cout<< i.first<< " " << i.second<< std ::endl;}
std ::cout<< m1[1 ]<< " " << m1[2 ]<< " " << m1[3 ]<< std ::endl; // 访问元素
// 如果没有对应的键,map 会新建键和值
// 对于 int 类型,新建的值为 0
for (std ::pair<int ,int> i:m1){std ::cout<< i.first<< " " << i.second<< std ::endl;}
std ::cout<< m2.at (1 )<< " " << m2.at (2 )<< std ::endl; // 访问元素,越界检查
// 注意 multimap 没有 at()
std ::multimap <int ,int >::iterator it;
it= m3.find (1 ); // 返回对应键和值的迭代器,多个键返回第一个键和值
// 如果没有,返回 end()
std ::cout<< it->second<< std ::endl;
it++ ;
std ::cout<< it->second<< std ::endl;
it= m3.find (2 );
std ::cout<< it->second<< std ::endl;
it++ ;
std ::cout<< it->second<< std ::endl;
m3.erase (it); // 按迭代器清除
m3.erase (1 ); // 按键清除,全部清除
for (std ::pair<int ,int> i:m3){std ::cout<< i.first<< " " << i.second<< std ::endl;}
m3.clear (); // 清空
std ::cout<< m3.empty ()<< std ::endl; // 检查容器是否为空
std ::cout<< m3.size ()<< std ::endl; // 返回容器大小
m3.emplace (1 ,4 );
m3.emplace (1 ,3 );
m3.emplace (2 ,3 );
m3.emplace (2 ,4 );
it= m3.lower_bound (1 ); // 返回大于等于键和值的迭代器,多个键返回第一个键和值
std ::cout<< it->second<< std ::endl;
it= m3.upper_bound (1 ); // 返回大于键和值的迭代器,多个键返回第一个键和值
std ::cout<< it->second<< std ::endl;
}
Unordered \text{Unordered} Unordered
C++ 复制 // rehash()
// insert()
// emplace()
// operator []
// find()
// count()
// load_factor()
// erase()
#include <unordered_set> // 头文件
#include <unordered_map> // 头文件
#include <iostream>
int main (){
std ::unordered_set<int> us1;
std ::unordered_multiset<int> us2;
std ::unordered_map<int ,int> um1;
std ::unordered_multimap<int ,int> um2;
// 开散列法实现
us1.rehash (10 ); // 重哈希,强制设置桶数,所有迭代器失效
us2.rehash (10 );
um1.rehash (10 );
um2.rehash (10 );
us1.insert (1 ); us1.emplace (1 ); us1.insert (2 ); us1.emplace (2 ); // 插入
us2.insert (1 ); us2.emplace (1 ); us2.insert (2 ); us2.emplace (2 ); // 插入
um1.insert ({1 ,1 }); um1.emplace (1 ,2 ); um1.insert ({2 ,1 }); um1.emplace (2 ,2 ); // 插入
um2.insert ({1 ,1 }); um2.emplace (1 ,2 ); um2.insert ({2 ,1 }); um2.emplace (2 ,2 ); // 插入
for (int i:us1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
for (int i:us2){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
for (std ::pair<const int ,int> i:um1){std ::cout<< i.second<< " " ;} std ::cout<< std ::endl;
for (std ::pair<const int ,int> i:um2){std ::cout<< i.second<< " " ;} std ::cout<< std ::endl;
std ::cout<< um1[1 ]<< " " << um1[2 ]<< std ::endl; // 返回值
std ::cout<< um2.find (1 )->second<< " " << um2.find (2 )->second<< std ::endl; // 返回迭代器
std ::unordered_set <int >::iterator it1,it2;
it1= us1.find (1 ); std ::cout<< (it1!= us1.end ())<< std ::endl;
it1= us1.find (2 ); std ::cout<< (it1!= us1.end ())<< std ::endl;
it1= us1.find (3 ); std ::cout<< (it1!= us1.end ())<< std ::endl;
std ::cout<< us2.count (1 )<< " " << us2.count (2 )<< " " << us2.count (3 )<< std ::endl; // 返回键数
std ::cout<< um1.load_factor ()<< " " << um2.load_factor ()<< std ::endl; // 返回负载因子
it1= us1.find (1 ); it2= us1.find (2 );
us1.erase (it1); std ::cout<<* it2<< std ::endl; // 删除元素不影响其他迭代器
}
Stack \text{Stack} Stack
C++ 复制 // push()
// top()
// pop()
// size()
// empty()
#include <stack> // 头文件
#include <iostream>
int main (){
std ::stack<int> s;
s.push (1 ); // 栈顶放入元素
s.push (2 );
s.push (3 );
std ::cout<< s.top ()<< std ::endl; // 返回栈顶元素
s.pop (); // 栈顶删除元素
std ::cout<< s.top ()<< std ::endl;
std ::cout<< s.size ()<< std ::endl; // 返回容器大小
std ::cout<< s.empty ()<< std ::endl; // 检查容器是否为空
s.pop (); s.pop (); std ::cout<< s.empty ()<< std ::endl;
}
Queue \text{Queue} Queue
C++ 复制 // push()
// front()
// back()
// pop()
// size()
// empty()
#include <queue> // 头文件
#include <iostream>
int main (){
std ::queue<int> q;
q.push (1 ); // 队尾放入元素
q.push (2 );
q.push (3 );
std ::cout<< q.front ()<< " " << q.back ()<< std ::endl; // 返回队首和队尾元素
q.pop (); // 队首删除元素
std ::cout<< q.front ()<< std ::endl;
std ::cout<< q.size ()<< std ::endl; // 返回容器大小
std ::cout<< q.empty ()<< std ::endl; // 检查容器是否为空
q.pop (); q.pop (); std ::cout<< q.empty ()<< std ::endl;
}
Priority Queue \text{Priority Queue} Priority Queue
构造函数
C++ 复制 std ::priority_queue< T> pq1; // 类型为 T
std ::priority_queue< T,C1> pq2; // 类型为 T,底层容器为 C1
std ::priority_queue< T,C1,C2> pq3; // 类型为 T,底层容器为 C1,比较类型为 C2
// C1 不可省略
成员函数
C++ 复制 // push()
// top()
// pop()
// size()
// empty()
#include <queue> // 头文件
#include <iostream>
int main (){
std ::priority_queue<int> pq1; // 默认大根堆
std ::priority_queue<int ,std ::vector<int> ,std ::greater<int> > pq2; // 小根堆
pq1.push (1 ); // 放入元素
pq1.push (2 );
pq1.push (3 );
pq2.push (1 );
pq2.push (2 );
pq2.push (3 );
std ::cout<< pq1.top ()<< " " << pq2.top ()<< std ::endl; // 返回堆顶元素
pq1.pop (); // 堆顶删除元素
std ::cout<< pq1.top ()<< std ::endl;
std ::cout<< pq1.size ()<< std ::endl; // 返回容器大小
std ::cout<< pq1.empty ()<< std ::endl; // 检查容器是否为空
pq1.pop (); pq1.pop (); std ::cout<< pq1.empty ()<< std ::endl;
}
Algorithm \text{Algorithm} Algorithm
C++ 复制 // std::reverse()
// std::sort()
// std::nth_element()
// std::lower_bound()
// std::upper_bound()
// std::unique()
// std::next_permutation()
// std::prev_permutation()
#include <algorithm> // 头文件
#include <vector>
#include <iostream>
int main (){
std ::vector<int> v{1 ,2 ,3 ,1 ,2 ,3 ,4 ,5 ,6 ,4 ,5 ,6 };
for (int i:v){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
std ::reverse (v.begin (),v.end ()); // 翻转数组
// 不加 std:: 也行,原因是有 ADL(参数依赖查找)
for (int i:v){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
std ::nth_element (v.begin ()+ 3 ,v.begin ()+ 6 ,v.end ()- 3 );
// 将小于第 n 大的元素的元素移到左边,将大于第 n 大的元素的元素移到右边
// 这里的左边指 [v.begin()+3,v.begin()+6),这里的右边指 (v.begin()+6,v.end()-3)
for (int i:v){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
std ::sort (v.begin (),v.end (),std ::greater <int >());
// 降序排序
// std::greater 需要头文件 functional
// 许多编译器的 algorithm 会间接包含 functional
for (int i:v){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
std ::cout<< std ::lower_bound (v.begin (),v.end (),2 ,std ::greater <int >())- v.begin ()+ 1 ;
// 返回第一个小于等于元素的迭代器
std ::cout<< std ::endl;
std ::cout<< std ::upper_bound (v.begin (),v.end (),2 ,std ::greater <int >())- v.begin ()+ 1 ;
// 返回第一个小于元素的迭代器
std ::cout<< std ::endl;
std ::sort (v.begin (),v.end ()); // 默认升序排序
for (int i:v){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
std ::cout<< std ::lower_bound (v.begin (),v.end (),2 )- v.begin ()+ 1 << std ::endl;
// 返回第一个大于等于元素的迭代器
std ::cout<< std ::upper_bound (v.begin (),v.end (),2 )- v.begin ()+ 1 << std ::endl;
// 返回第一个大于元素的迭代器
int cnt= std ::unique (v.begin (),v.end ())- v.begin (); // 去重,重复元素移到末尾
// 容器大小不变
// 返回去重之后的尾指针
std ::cout<< cnt<< std ::endl;
std ::cout<< v.end ()- v.begin ()<< std ::endl; // 容器的尾指针位置不变
std ::next_permutation (v.begin (),v.begin ()+ cnt); // 生成字典序下一个排列
for (int i= 0 ;i< cnt;i++ ){std ::cout<< v[i]<< " " ;} std ::cout<< std ::endl;
std ::prev_permutation (v.begin (),v.begin ()+ cnt); // 生成字典序上一个排列
for (int i= 0 ;i< cnt;i++ ){std ::cout<< v[i]<< " " ;} std ::cout<< std ::endl;
}
Builtin \text{Builtin} Builtin
C++ 复制 #include <iostream>
// 不属于标准库,没有头文件
int main (){
unsigned int ui=0b 10000000 ;
unsigned long long ull=0b 1000000010000000 ;
std ::cout<< __builtin_popcount (ui)<< std ::endl; // 返回 1 的个数
std ::cout<< __builtin_clz (ui)<< std ::endl; // 返回前导零的个数,全零未定义
std ::cout<< __builtin_ctz (ui)<< std ::endl; // 返回末尾零的个数,全零未定义
std ::cout<< __builtin_popcountll (ull)<< std ::endl;
std ::cout<< __builtin_clzll (ull)<< std ::endl;
std ::cout<< __builtin_ctzll (ull)<< std ::endl;
}
Char \text{Char} Char
C++ 复制 // strcpy
// strlen
// strcmp
// strncmp
// strncpy
// strcat
// strncat
// strchr
// strrchr
// strstr
// memcpy
#include <cstdio>
#include <iostream>
#include <cstring>
int main (){
char s[100 ],t[100 ]; // 最多存储 99 个字符,最后是 '\0'
strcpy (s,"abcde" ); strcpy (t,"abdef" ); // 复制
std ::cout<< strlen (s)<< " " << strlen (t)<< std ::endl; // 返回长度
std ::cout<< strcmp (s,t)<< " " << strcmp (t,s)<< std ::endl; // 字典序比较
// < 返回小于 0,= 返回 0
// > 返回大于 0
std ::cout<< strncmp (s,t,2 )<< " " << strncmp (t,s,2 )<< std ::endl; // 字典序比较前 n 个字符
strncpy (s,t,4 ); // 复制前 n 个字符
printf (" %s\n " ,s);
strcat (s,t); // 拼接
strncat (s,t,4 ); // 拼接前 n 个字符
printf (" %s\n " ,s);
std ::cout<< strchr (s,'e' )- s+ 1 << std ::endl; // 查找字符首次出现位置,返回指针
std ::cout<< strrchr (s,'e' )- s+ 1 << std ::endl; // 查找字符最后一次出现位置,返回指针
std ::cout<< strstr (s,t)- s+ 1 << std ::endl; // 查找子串首次出现位置,返回指针
memcpy (s,t,100 ); // 复制
printf (" %s\n " ,s);
}
String \text{String} String
C++ 复制 // cin
// cout
// size()
// empty()
// operator []
// at()
// front()
// back()
// clear()
// operator +=
// append()
// push_back()
// pop_back()
// erase()
// find()
// substr()
// replace()
// operator <
// operator >
// operator ==
// operator !=
// operator +
#include <iostream>
#include <string> // 头文件
int main (){
std ::string s= "abcdefg" ;
std ::cout<< s<< std ::endl; // 支持流输入和流输出
std ::cout<< s.size ()<< std ::endl; // 返回字符个数
std ::cout<< s.empty ()<< std ::endl; // 判断是否为空
s[1 ]= 'h' ; // 访问元素
s.at (2 )= 'i' ; // 访问元素,越界抛出 out_of_range
s.front ()= 'j' ; // 访问首元素
s.back ()= 'a' ; // 访问尾元素
std ::cout<< s<< std ::endl;
s.clear (); // 清空
s+= 'q' ; // 末尾追加字符
std ::cout<< s<< std ::endl;
s+= "we" ; // 末尾追加字符串,时间复杂度为追加字符串长度
std ::cout<< s<< std ::endl;
s.append ("rt" ); // 末尾追加字符串
std ::cout<< s<< std ::endl;
s.push_back ('y' ); // 末尾追加字符
std ::cout<< s<< std ::endl;
s.pop_back (); s.pop_back (); s.pop_back (); // 末尾删除字符
std ::cout<< s<< std ::endl;
s.erase (2 ); // 删除从下标 2 开始到末尾的字符
s.erase (0 ,1 ); // 删除从下标 0 开始的 1 个字符
std ::cout<< s<< std ::endl;
s+= "asdfghjkl" ;
std ::cout<< s.find ('w' )<< std ::endl; // 查询字符第一次出现的下标,查询失败返回 string::npos
std ::cout<< s.find ('j' ,5 )<< std ::endl; // 从下标 5 开始,查询字符第一次出现的下标
std ::cout<< s.substr (5 )<< std ::endl; // 截取从下标 5 开始到末尾的字符串
std ::cout<< s.substr (3 ,5 )<< std ::endl; // 截取从下标 3 开始的 5 个字符构成字符串
std ::cout<< s<< std ::endl;
s.replace (3 ,3 ,"bc" ); // 将从下标 3 开始的 3 个字符替换为指定字符串
// 时间复杂度为两个字符串长度之和
std ::cout<< s<< std ::endl;
std ::string s1= "qaz" ,s2= "wsx" ;
bool b;
b= (s1< s2); // 字典序比较
std ::cout<< b<< std ::endl;
b= (s1> s2); // 字典序比较
std ::cout<< b<< std ::endl;
b= (s1== s2); // 判断两个字符串是否相同
std ::cout<< b<< std ::endl;
b= (s1!= s2); // 判断两个字符串是否不同
std ::cout<< b<< std ::endl;
s= s1+ s2; // 拼接字符串,时间复杂度为两个字符串长度之和
std ::cout<< s<< std ::endl;
}
List \text{List} List
C++ 复制 // iterator
// push_front()
// push_back()
// pop_front()
// pop_back()
// insert()
// erase()
// std::find()
// size()
// clear()
// sort()
// merge()
// splice()
// unique()
// remove()
// reverse()
#include <list> // 头文件
#include <algorithm>
#include <iostream>
int main (){
std ::list<int> l1,l2;
std ::list <int >::iterator it1,it2,it3;
// 注意,迭代器不支持 operator + operator - operator < operator > operator []
l1.push_front (1 ); l1.push_front (1 ); l1.push_front (1 ); // 头部插入
l1.push_back (2 ); l1.push_back (2 ); l1.push_back (2 ); // 尾部插入
l1.pop_front (); // 头部删除
l1.pop_back (); // 尾部删除
for (int i:l1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
it1= l1.begin ();
it1++ ; l1.insert (it1,3 ); // 在当前位置前插入
it1++ ; l1.insert (it1,3 );
for (int i:l1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
it1= l1.erase (it1); // 删除元素,返回下一个元素的迭代器
for (int i:l1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
std ::cout<< std ::distance (l1.begin (),std ::find (l1.begin (),l1.end (),3 ))+ 1 << std ::endl;
// 查找元素,时间复杂度线性
std ::cout<< l1.size ()<< std ::endl; // 返回容器大小
l1.clear (); // 清空
l1.push_back (5 ); l1.push_back (3 ); l1.push_back (1 ); l1.sort (); // 排序,时间复杂度线性对数
for (int i:l1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
l2.push_back (6 ); l2.push_back (4 ); l2.push_back (2 ); l2.sort ();
l2.merge (l1); // 合并有序链表
for (int i:l2){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
l1.push_back (5 ); l1.push_back (3 ); l1.push_back (1 );
l2.clear (); l2.push_back (6 ); l2.push_back (4 ); l2.push_back (2 );
it1= l1.begin (); l1.splice (it1,l2); // 所有元素剪贴
for (int i:l1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
std ::cout<< l2.size ()<< std ::endl;
l2.push_back (6 ); l2.push_back (4 ); l2.push_back (2 );
it1= l1.begin (); it2= l2.begin ();
l1.splice (it1,l2,it2); // 一个元素剪贴
for (int i:l1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
it1= l1.begin (); it2= it3= l2.begin (); it3++ ; it3++ ;
l1.splice (it1,l2,it2,it3); // 一个区间剪贴
for (int i:l1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
l1.unique (); // 相邻元素去重,时间复杂度线性
for (int i:l1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
l1.remove (2 ); // 删除所有相等元素 ,时间复杂度线性
for (int i:l1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
l1.reverse (); // 翻转链表,时间复杂度线性
for (int i:l1){std ::cout<< i<< " " ;} std ::cout<< std ::endl;
}
Pair \text{Pair} Pair
C++ 复制 // std::make_pair
// first
// second
// operator ==
// operator !=
// operator <=
// operator <
// operator >=
// operator >
#include <utility> // 头文件
#include <iostream>
int main (){
std ::pair<int ,int> p1,p2;
p1= std ::make_pair (123 ,456 );
std ::cout<< p1.first<< std ::endl;
std ::cout<< p1.second<< std ::endl;
p2.first= 987 ;
p2.second= 100 ;
std ::cout<< p2.first<< std ::endl;
std ::cout<< p2.second<< std ::endl;
std ::cout<< (p1== p2)<< std ::endl;
std ::cout<< (p1!= p2)<< std ::endl;
std ::cout<< (p1<= p2)<< std ::endl; // 先比较 first,first 相等时再比较 second
std ::cout<< (p1< p2)<< std ::endl;
std ::cout<< (p1>= p2)<< std ::endl;
std ::cout<< (p1> p2)<< std ::endl;
}