http://www.cplusplus.com/reference/
超级好的参考资料
vector
//清空vector
vector.clear();
myvector.push_back(v);
myvector.pop_back();
//initial.
int myints[] = {32,71,12,45,26,80,53,33};
std::vector<int> myvector (myints, myints+8);
//Output.
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
Element access:
-
operator[] Access element
(public member function)
-
at
Access element
(public member function)
-
front
Access first element
(public member function)
-
back
Access last element
(public member function)
-
Capacity:
size Return size (public member function)
-
max_size Return maximum size
(public member function)
-
resize
Change size
(public member function)
-
capacity Return size of allocated storage capacity
(public member function)
-
empty
Test whether vector is empty
(public member function)
-
begin Return iterator to beginning
(public member function)
-
end Return iterator to end
(public member function)
-
rbegin
Return reverse iterator to reverse beginning
(public member function)
-
rend
Return reverse iterator to reverse end
(public member function)
-
cbegin
Return const_iterator to beginning
(public member function)
-
cend
Return const_iterator to end
(public member function)
-
crbegin
Return const_reverse_iterator to reverse beginning
(public member function)
-
crend
Return const_reverse_iterator to reverse end
(public member function)
-
Modifiers:
push_back Add element at the end (public member function)
-
pop_back
Delete last element
(public member function)
-
insert
Insert elements
(public member function)
-
erase
Erase elements
(public member function)
-
swap
Swap content
(public member function)
-
clear
Clear
--------------------------------------
string
std::string s0 ("Initial string");
// constructors used in the same order as described above:
std::string s1;
std::string s2 (s0);
std::string s3 (s0, 8, 3);
std::string s4 ("A character sequence", 6);
std::string s5 ("Another character sequence");
std::string s6 (10, 'x');
std::string s7a (10, 42); // 42 is the ASCII code for '*'
std::string s7b (s0.begin(), s0.begin()+7);
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (12,12); // "generalities"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
s.substr(begin_index,numbers);
s.find(c)!=string::npos //find it
Convert from strings
-
stoi
Convert string to integer
(function template)
-
stol
Convert string to long int
(function template)
-
stoul
Convert string to unsigned integer
(function template)
-
stoll
Convert string to long long
(function template)
-
stoull
Convert string to unsigned long long
(function template)
-
stof
Convert string to float
(function template)
-
stod
Convert string to double
(function template)
-
stold
Convert string to long double
(function template)
Convert to strings
-
to_string
Convert numerical value to string
(function)
Range access
-
begin
Iterator to beginning
(function template)
-
end
Iterator to end
(function template)
-
begin
Return iterator to beginning
(public member function)
-
end
Return iterator to end
(public member function)
-
rbegin
Return reverse iterator to reverse beginning
(public member function)
-
rend
Return reverse iterator to reverse end
(public member function)
-
cbegin
Return const_iterator to beginning
(public member function)
-
cend
Return const_iterator to end
(public member function)
-
crbegin
Return const_reverse_iterator to reverse beginning
(public member function)
-
crend
Return const_reverse_iterator to reverse end
(public member function)
Capacity:
-
size
Return length of string
(public member function)
-
length
Return length of string
(public member function)
-
max_size
Return maximum size of string
(public member function)
-
resize
Resize string
(public member function)
-
capacity
Return size of allocated storage
(public member function)
-
reserve
Request a change in capacity
(public member function)
-
clear
Clear string
(public member function)
-
empty
Test if string is empty
(public member function)
-
shrink_to_fit
Shrink to fit
(public member function)
Element access:
-
operator[]
Get character of string
(public member function)
-
at
Get character in string
(public member function)
-
back
Access last character
(public member function)
-
front
Access first character
(public member function)
Modifiers:
-
operator+=
Append to string
(public member function)
-
append
Append to string
(public member function)
-
push_back
Append character to string
(public member function)
-
assign
Assign content to string
(public member function)
-
insert
Insert into string
(public member function)
-
erase
Erase characters from string
(public member function)
-
replace
Replace portion of string
(public member function)
-
swap
Swap string values
(public member function)
-
pop_back
Delete last character
(public member function)
String operations:
-
c_str
Get C string equivalent
(public member function)
-
data
Get string data
(public member function)
-
get_allocator
Get allocator
(public member function)
-
copyCopy sequence of characters from string
(public member function)
-
find
Find content in string
(public member function)
-
rfind
Find last occurrence of content in string
(public member function)
-
find_first_of
Find character in string
(public member function)
-
find_last_of
Find character in string from the end
(public member function)
-
find_first_not_of
Find absence of character in string
(public member function)
-
find_last_not_of
Find non-matching character in string from the end
(public member function)
-
substr
Generate substring
(public member function)
-
compare
Compare strings
(public member function)
Member constants
-
npos
Maximum value for size_t
(public static member constant)
------------------------------------------
stack
mystack.pop();
mystack.top();
mystack.push_back();
queue
-
empty
Test whether container is empty
(public member function)
-
size
Return size
(public member function)
-
front
Access next element
(public member function)
-
back
Access last element
(public member function)
-
push
Insert element
(public member function)
-
emplace
Construct and insert element
(public member function)
-
pop
Remove next element
(public member function)
-
priority_queue
empty Test whether container is empty (public member function)
-
size
Return size
(public member function)
-
top
Access top element
(public member function)
-
push
Insert element
(public member function)
-
emplace
Construct and insert element
(public member function)
-
pop
Remove top element
(public member function)
---------------------------------------
unordered_map
#include <tr1/unordered_map>
using namespace std;
using namespace std::tr1;
unordered_map<int,int> mapping;
mapping.find(gap)!=mapping.end()Element access
-
operator[]
Access element
(public member function)
-
at
Access element
(public member function)
Element lookup
-
find
Get iterator to element
(public member function)
-
count
Count elements with a specific key
(public member function)
-
equal_range
Get range of elements with specific key
(public member function)
Modifiers
-
emplace
Construct and insert element
(public member function)
-
emplace_hint
Construct and insert element with hint
(public member function)
-
insert
Insert elements
(public member function)
-
erase
Erase elements
(public member function)
-
clear
Clear content
(public member function)
-
swap
Swap content
(public member function)
----------------------------------------
unordered_set
-----------------------------------------
pair
make_pair(a,b);
pair<int,int> mypair[2]={{1,2},{3,3}} ;
pair<int,int> mypair={1,2};
-------------------------------------------
ListNode
struct ListNode{
int val;
ListNode *next;
ListNode(int x) : val(x),next(nullptr){}
};
--------------------------------------------
TreeNode
struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x),left(nullptr),right(nullptr){}
};
----------------------------------------
UndirectedGraphNode
struct UndirectedGraphNode{
int label;
vector<UndirectedGraphNode*> neighbors;
UndirectedGraphNode(int x) : label(x) {}
};
-----------------------------------------
transform(source_begin,source_end, target_begin, function_pointer);
::tolower
::isalnum;
::atoi;
::isspace;
::isdigit;
::strtod;
::atol;
::strtol;
::strtoul;
--------------------------------------------
#include <algorithm>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value)
void fill (ForwardIterator first, ForwardIterator last, const T& val)
OutputIterator fill_n (OutputIterator first, Size n, const T& val)
void swap ( T& a, T& b )
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
E.G.
--int myints[]={10,20,30,40,50,60,70};
std::vector<int> myvector (7);
std::copy ( myints, myints+7, myvector.begin() );
InputIterator find (InputIterator first, InputIterator last,
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)
const T& valFunction for_each(InputIterator first, InputIterator last, Function fn)
E.G.
--for_each (myvector.begin(), myvector.end(), myfunction);void myfunction (int i) { std::cout << ' ' << i;}
OutputIterator transform (InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperator op)
std::sort (myvector.begin()+4, myvector.end(), myfunction);
bool myfunction (int i,int j) { return (i<j); }
ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
void generate ( ForwardIterator first, ForwardIterator last, Generator gen )
E.G. --
int RandomNumber () { return (std::rand()%100); }
std::generate (myvector.begin(), myvector.end(), RandomNumber);
void generate_n ( OutputIterator first, Size n, Generator gen )
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
//heap
int main () {
int myints[] = {10,20,30,5,15};
std::vector<int> v(myints,myints+5);
std::make_heap (v.begin(),v.end());
std::cout << "initial max heap : " << v.front() << '\n';
std::pop_heap (v.begin(),v.end()); v.pop_back();
std::cout << "max heap after pop : " << v.front() << '\n';
v.push_back(99); std::push_heap (v.begin(),v.end());
std::cout << "max heap after push: " << v.front() << '\n';
std::sort_heap (v.begin(),v.end());
std::cout << "final sorted range :";
for (unsigned i=0; i<v.size(); i++)
std::cout << ' ' << v[i];
std::cout << '\n';
return 0;
}
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred) E.G. --
if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) ) std::cout << "All the elements are odd numbers.\n";
bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred)
bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred)
std::random_shuffle (myvector.begin(), myvector.end());
std::nth_element (myvector.begin(), myvector.begin()+5, myvector.end());
bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)
BidirectionalIterator partition (BidirectionalIterator first,
BidirectionalIterator last, UnaryPredicate pred)
next_permutation(iter_1,iter_2)
------------------------------------------------------
//No need
//#include <iteration>
distance (InputIterator first, InputIterator last);
auto begin (Container& cont)
ForwardIterator next (ForwardIterator it, typename iterator_traits<ForwardIterator>::difference_type n = 1)
BidirectionalIterator prev (BidirectionalIterator it, typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
void advance (InputIterator& it, Distance n);