万能头文件
# include <bits/stdc++.h>
ios:: sync_with_stdio ( false ) ;
cin. tie ( 0 ) ;
cout. tie ( 0 ) ;
C++常用函数
# include <algorithm>
sort ( myvector. begin ( ) , myvector. begin ( ) + 4 ) ;
merge ( a, a + 5 , b, b + 6 , c. begin ( ) ) ;
auto it = find ( myvector. begin ( ) , myvector. end ( ) , 30 ) ;
vector< int > A{ 1 , 2 , 3 , 4 , 8 , 12 , 18 , 1 , 2 , 3 } ;
int B[ ] = { 1 , 2 , 3 } ;
auto it = search ( A. begin ( ) , A. end ( ) , B, B + 3 ) ;
int a[ 5 ] = { 1 , 2 , 3 , 4 , 5 } ;
int * p = lower_bound ( a, a + 5 , 3 ) ;
int * p = upper_bound ( a, a + 5 , 3 ) ;
s[ 0 ] = 0 ;
partial_sum ( nums. begin ( ) , nums. end ( ) , s + 1 ) ;
s[ i] = toupper ( s[ i] ) ;
s[ i] = tolower ( s[ i] ) ;
# include <numeric>
int sum = accumulate ( nums. begin ( ) , nums. end ( ) , 0 ) ;
# include <numeric>
iota ( v. begin ( ) , v. end ( ) , 1 ) ;
# include <algorithm>
max_element ( nums. begin ( ) , nums. end ( ) ) ;
* max_element ( nums. begin ( ) , nums. end ( ) ) ;
int ans = ___gcd ( a, b)
string常用函数
# include <string>
string s1 ( ) ;
string s2 ( "Hello" ) ;
string s3 ( 4 , 'K' ) ;
s2. append ( s3) ;
int n = s1. compare ( s2) ;
string s4 = s2. substr ( 2 , 4 ) ;
s1. swap ( s2) ;
vector容器常用函数(动态数组)
# include <vector>
using namespace std;
vector< double > values;
vector< double > values ( 20 ) ;
vector< double > values ( 20 , 1.0 ) ;
vector< int > value1{ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 } ;
vector< int > value2 ( begin ( value1) , begin ( value1) + 3 ) ;
int n = values. size ( ) ;
bool is = values. empty ( ) ;
values. push_back ( x) ;
values. pop_back ( ) ;
values. emplace_back ( x) ;
values. erase ( values. begin ( ) + 2 ) ;
swap ( values[ 1 ] , values[ 4 ] ) ;
list容器常用函数(双向链表)
# include <list>
using namespace std;
list< int > values;
list< int > values ( 10 ) ;
list< int > values ( 10 , 5 ) ;
int & first = mylist. front ( ) ;
int & last = mylist. back ( ) ;
int n = values. size ( ) ;
bool is = values. empty ( ) ;
values. push_front ( x) ;
values. pop_front ( ) ;
values. push_back ( x) ;
values. pop_back ( ) ;
values. emplace_front ( x) ;
values. emplace_back ( x) ;
values. reverse ( ) ;
list< int > mylist1{ 1 , 2 , 3 , 4 } , mylist2{ 10 , 20 , 30 } ;
mylist2. splice ( mylist2. end ( ) , mylist1, mylist1. begin ( ) , mylist1. end ( ) ) ;
values. erase ( values. begin ( ) + 2 ) ;
values. remove ( 'c' ) ;
values. unique ( ) ;
for ( auto it = values. begin ( ) ; it != values. end ( ) ; ++ it) {
cout<< * it;
}
pair键值对
# include <utility>
# include <string>
using namespace std;
pair < string, string> pair1;
pair < string, int > pair1 ( "nums" , 20 ) ;
pair1. first = "name" ;
pair1. second = "cashmaker" ;
map容器常用函数
# include <map>
using namespace std;
map< int , int > mymap;
mymap. insert ( { key, value} ) ;
mymap. emplace ( key, value) ;
mymap. erase ( key) ;
mymap. count ( key) ;
mymap. find ( key) ;
if ( mymap. find ( x) != mymap. end ( ) )
auto it = maymap. find ( x) ;
int a = it-> first, b = it-> second, c = mymap[ x] ;
mymap. at ( key) ;
mymap. lower_bound ( key) ;
mymap. upper_bound ( key) ;
set容器常用函数
# include <set>
using namespace std;
set< string> myset;
myset. insert ( x) ;
myset. emplace ( x) ;
myset. erase ( x) ;
myset. count ( x) ;
myset. find ( x) ;
myset. lower_bound ( x) ;
myset. upper_bound ( x) ;
unordered_map 容器常用函数
# include <unordered_map>
using namespace std;
unordered_map< int , int > mymap;
mymap. insert ( { key, value} ) ;
mymap. emplace ( key, value) ;
mymap. erase ( key) ;
mymap. find ( key) ;
if ( mymap. find ( x) != mymap. end ( ) )
auto it = maymap. find ( x) ;
int a = it-> first, b = it-> second, c = mymap[ x] ;
mymap. at ( key) ;
unordered_set容器常用函数
# include <set>
using namespace std;
unordered_set< string> myset;
myset. insert ( x) ;
myset. emplace ( x) ;
myset. erase ( x) ;
myset. find ( x) ;
stack容器常用函数
# include <stack>
using namespace std;
stack< int > values;
values. empty ( ) ;
values. top ( ) ;
values. push ( x) ;
values. pop ( ) ;
values. emplace ( x) ;
queue容器常用函数
# include <queue>
using namespace std;
queue< int > values;
values. empty ( ) ;
values. front ( ) ;
values. back ( ) ;
values. push ( x) ;
values. pop ( ) ;
values. emplace ( x) ;
priority_queue容器常用函数
# include <queue>
using namespace std;
priority_queue< int > values;
priority_queue< int , vector< int > , greater< int >> heap;
priority_queue< int , vector< int > , less< int >> heap;
values. empty ( ) ;
values. push ( x) ;
values. top ( ) ;
values. pop ( ) ;
values. emplace ( x) ;