0、C++中常用STL库函数
STL常用库函数
# include <algorithm>
sort ( s. begin ( ) , s. end ( ) , 自己定义) ;
stable_sort ( v1. begin ( ) , v1. end ( ) , comp) ;
binary_search ( v1. begin ( ) , v1. end ( ) , elem) ;
upper_bound ( a. begin ( ) , a. end ( ) , 3 ) - a. begin ( ) ;
lower_bound ( a. begin ( ) , a. end ( ) , 3 ) - a. begin ( ) ;
reverse ( s. begin ( ) , s. end ( ) ) ;
int m = unique ( s. begin ( ) , s. end ( ) ) - s. begin ( ) ;
s. erase ( unique ( s. begin ( ) , s. end ( ) ) , s. end ( ) ) ;
srand ( time ( 0 ) ) ; random_shuffle ( s. begin ( ) , s. end ( ) ) ;
count ( s. begin ( ) , s. end ( ) , '1' ) ;
iter = find ( v1. begin ( ) , v1. end ( ) , elem) ;
iter = search ( v1. begin ( ) , v1. end ( ) , v2. begin ( ) , v2. end ( ) ) ;
# include <string>
s. find ( p, position)
s. rfind ( p)
s. find_first_of ( p, position)
s. find_first_not_of ( p, position)
1、string字符串
c++
# include <string>
string s1 = "hello" ;
s1. clear ( ) ;
s1. size ( ) ;
s1. length ( ) ;
s1. empty ( ) ;
s1. c_str ( ) ;
s1. substr ( 0 , s1. size ( ) ) ;
s1. append ( s2, 0 , s2. size ( ) ) ;
to_string ( 100 ) ;
stoi ( "100" ) ;
stod ( "0.1" ) ;
python
str ( )
int ( )
2、vector和python列表
c++
vector中的函数
# include <vector>
vector< int > result;
vector< int > result ( n, 初始值) ;
vector< int > v1 = { 0 , 1 , 2 , 3 } ;
vector< int > v1 ( v2. begin ( ) , v2. end ( ) ) ;
vector< int > v1 ( r, vector < int > ( c, 0 ) )
typedef pair< int , int > PII;
vector< PII> v1;
v1. clear ( ) ;
v1. size ( ) ;
v1. empty ( ) ;
result. front ( ) ;
result. back ( ) ;
result. push_back ( input) ;
result. pop_back ( ) ;
for ( auto i: result) cout<< i<< ends;
iter = v1. insert ( v1. begin ( ) , 100 ) ;
python
python列表中的函数
result = [ ]
len ( result)
result. count( name)
result. append( input )
result. pop( )
result. extend( list )
result. sort( key= None , reverse= False )
result = "" . join( result)
3、unordered_map/map和字典
c++
unordered_map用法
# include <map>
map< int , string> m1 = { { 2015 , "Tom" } , { 2016 , "Jim" } } ;
m1. clear ( ) ;
m1. size ( ) ;
m1. empty ( ) ;
m1. at[ 2015 ] = "Tom" ;
m1[ 2015 ] = "Tom" ;
m1. count ( key) ;
iter = m1. find ( key) ;
c1. lower_bound ( key) ;
c1. upper_bound ( key) ;
m1. insert ( make_pair ( 2015 , "Tom" ) ) ;
m1. insert ( pair < int , string> ( 2015 , "Tom" ) ) ;
m1. insert ( { 2015 , "Tom" } ) ;
m1. erase ( key) ;
m1. erase ( iter) ;
m1. erase ( m1. begin ( ) + 2 , m1. begin ( ) + 5 ) ;
# include <unordered_map>
unordered_map< int , int > occ;
occ[ key] = value;
if ( auto temp = occ. find ( key) == occ. end ( ) )
if ( occ. count ( key) ) ;
iter. first, iter. second
python
字典具体函数
occ = dict ( )
occ[ key] = value
if key in occ:
4、unordered_set/set和集合
c++
unordered_set用法
# include <set>
# include <multiset>
set< int , less< int >> c1 ( c2) ;
set< int , less< int >> c1 ( c2. begin ( ) , c2. end ( ) ) ;
set< int , less< int >> c1 ( c2. begin ( ) , c2. end ( ) , comp) ;
c1. clear ( ) ;
c1. size ( ) ;
c1. empty ( ) ;
c1. count ( elem) ;
c1. find ( elem) ;
c1. lower_bound ( elem) ;
c1. upper_bound ( elem) ;
c1. insert ( elem) ;
c1. insert ( c2. begin ( ) , c2. end ( ) ) ;
c1. erase ( elem) ;
c1. erase ( iter) ;
c1. erase ( c1. begin ( ) + 2 , c1. begin ( ) + 5 ) ;
# include <unordered_set>
unordered_set< char > occ;
occ. insert ( name) ;
occ. erase ( name) ;
if ( auto temp = occ. find ( name) == occ. end ( ) )
occ. count ( name)
python
set用法
occ = set ( )
occ. add( name)
occ. remove( name)
if name in occ:
5、Stack
C++
# include <stack>
s1. clear ( ) ;
s1. size ( ) ;
s1. empty ( ) ;
s1. top ( ) ;
s1. push ( 100 ) ;
s1. pop ( ) ;
6、queue/deque
C++
# include <queue>
# include <deque>
q1. clear ( ) ;
q1. size ( ) ;
q1. empty ( ) ;
q1. front ( ) ;
q1. back ( ) ;
q1. push_front ( 100 ) ;
q1. push_back ( 100 ) ;
q1. pop_front ( ) ;
q1. pop_back ( ) ;
7、优先级队列
C++
# include <queue>
priority_queue< int , vector< int > , less< int >> maxQ;
priority_queue< int , vector< int > , greater< int >> minQ;
priority_queue< int > q1 ( less < int > ( ) , v1) ;
q1. clear ( ) ;
q1. size ( ) ;
q1. empty ( ) ;
q1. top ( ) ;
q1. push ( 100 ) ;
q1. pop ( ) ;
struct CompLess {
bool operator ( ) ( const int a, const int b) {
return a < b;
}
} ;
# include <algorithm>
std:: make_heap ( v. begin ( ) , v. end ( ) , comp) ;
v. push_back ( 6 ) ;
std:: push_heap ( v. begin ( ) , v. end ( ) ) ;
std:: pop_heap ( v. begin ( ) , v. end ( ) , comp) ;
v. pop_back ( )
8、List双链表
C++
list< int > l1 = l2;
list< int > l1 = { 0 , 1 , 2 , 3 } ;
list< int > l1 ( l2. begin ( ) , l2. end ( ) ) ;
list< int > l1 ( 7 ) ;
list< int > l1 ( 7 , 3 )
l1. clear ( ) ;
l1. size ( ) ;
l1. empty ( ) ;
l1. front ( ) ;
l1. back ( ) ;
l1. push_back ( 100 ) ;
l1. pop_back ( ) ;
iter = l1. insert ( l1. begin ( ) , 100 ) ;
iter = l1. insert ( l1. begin ( ) , l2. begin ( ) , l2. end ( ) ) ;
iter = l1. erase ( l1. begin ( ) + 2 ) ;
iter = l1. erase ( l1. begin ( ) + 2 , l1. begin ( ) + 5 ) ;
函数内定义函数(sort为例)
c++不允许函数内定义函数,实在想调用需要用lambda来匿名: lambda的c++描述 例1:sort(idx1.begin(),idx1.end(),[&](int i, int j){return nums1[i] < nums2[j];});//引用传递比值传递[=]快很多
例2:auto display = [&](int a,int b){return i < j}; sort(idx1.begin(),idx1.end(),display);
python直接def即可。
Last、c++导出错误
try{ } catch( exception e) { }