C++2.0
0. C++ Language
This is a reference of the core C++ language constructs.
See also
C language
https://en.cppreference.com/w/c/language
C++ language
https://en.cppreference.com/w/cpp/language
C++ standard library header files
https://docs.microsoft.com/en-us/cpp/standard-library/cpp-standard-library-header-files?view=vs-2019
github
https://github.com/MicrosoftDocs/cpp-docs/tree/master/docs/standard-library
30-seconds-of-cpp
https://github.com/Bhupesh-V/30-seconds-of-cpp
I. gcc/g++
update online
sudo apt install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90
gcc --version
update offline
https://blog.csdn.net/xie1xiao1jun/article/details/51612679
g++ -o test test.cpp -std=c++11
g++ -o test test.cpp -std=c++14
g++ -o test test.cpp -std=c++17
g++ -o test test.cpp -std=c++2a
II. Templates
III. Containers library
1. array
C++11
template< class T, std::size_t N > struct array;
example
#include <string> #include <iterator> #include <iostream> #include <algorithm> #include <array> int main() { // construction uses aggregate initialization std::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 prior to the CWG 1270 revision // (not needed in C++11 after the revision and in C++14 and beyond) std::array<int, 3> a2 = {1, 2, 3}; // never required after = std::array<std::string, 2> a3 = { std::string("a"), "b" }; // container operations are supported std::sort(a1.begin(), a1.end()); std::reverse_copy(a2.begin(), a2.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; // ranged for loop is supported for(const auto& s: a3) std::cout << s << ' '; }
output:
3 2 1 a b
https://en.cppreference.com/w/cpp/container/array
C++17
template <class T, class... U> array(T, U...) -> array<T, 1 + sizeof...(U)>;
example
#include <array> int main() { int const x = 10; std::array a{1, 2, 3, 5, x}; // OK, creates std::array<int, 5> // std::array b{1, 2u}; // Error, all arguments must have same type }
https://en.cppreference.com/w/cpp/container/array/deduction_guides
2. vector
C++11
template< class T, class Allocator = std::allocator<T> > class vector;
example
#include <iostream> #include <vector> int main() { // Create a vector containing integers std::vector<int> v = {7, 5, 16, 8}; // Add two more integers to vector v.push_back(25); v.push_back(13); // Iterate and print values of vector for(int n : v) { std::cout << n << '\n'; } }
output:
7 5 16 8 25 13
https://en.cppreference.com/w/cpp/container/vector
C++17
template< class InputIt, class Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type>> vector(InputIt, InputIt, Alloc = Alloc()) -> vector<typename std::iterator_traits<InputIt>::value_type, Alloc>;
example
#include <vector> int main() { std::vector<int> v = {1, 2, 3, 4}; // uses explicit deduction guide to deduce std::vector<int> std::vector x(v.begin(), v.end()); // deduces std::vector<std::vector<int>::iterator> // first phase of overload resolution for list-initialization selects the candidate // synthesized from the initializer-list constructor; second phase is not performed and // deduction guide has no effect std::vector y{v.begin(), v.end()}; }
https://en.cppreference.com/w/cpp/container/vector/deduction_guides
iterator
#include <iostream> #include <vector> int main() { // Create a vector containing integers std::vector<int> v = {7, 5, 16, 8}; // Add two more integers to vector v.push_back(25); v.push_back(13); for(int i = 0; i < 5; i++) { std::cout << v[i] << std::endl; } std::vector<int>::iterator n = v.begin(); while( n != v.end()) { std::cout << *n << std::endl; n++; } // Iterate and print values of vector for(int n : v) { std::cout << n << std::endl; } for(const auto& n: v) std::cout << n << std::endl; }
vector class
https://docs.microsoft.com/en-us/cpp/standard-library/vector-class?view=vs-2019
3. deque
C++11
template< class T, class Allocator = std::allocator<T> > class deque;
example
#include <iostream> #include <deque> int main() { // Create a deque containing integers std::deque<int> d = {7, 5, 16, 8}; // Add an integer to the beginning and end of the deque d.push_front(13); d.push_back(25); // Iterate and print values of deque for(int n : d) { std::cout << n << '\n'; } }
output:
13 7 5 16 8 25
https://en.cppreference.com/w/cpp/container/deque
C++17
template< class InputIt, class Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type>> deque(InputIt, InputIt, Alloc = Alloc()) -> deque<typename std::iterator_traits<InputIt>::value_type, Alloc>;
example
#include <deque> #include <vector> int main() { std::vector<int> v = {1, 2, 3, 4}; // uses explicit deduction guide to deduce std::deque<int> std::deque x(v.begin(), v.end()); // deduces std::deque<std::vector<int>::iterator> // first phase of overload resolution for list-initialization selects the candidate // synthesized from the initializer-list constructor; second phase is not performed and // deduction guide has no effect std::deque y{v.begin(), v.end()}; }
https://en.cppreference.com/w/cpp/container/deque/deduction_guides
4. forward_list
C++11
template< class T, class Allocator = std::allocator<T> > class forward_list;
https://en.cppreference.com/w/cpp/container/forward_list
C++17
template< class InputIt, class Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type>> forward_list(InputIt, InputIt, Alloc = Alloc()) -> forward_list<typename std::iterator_traits<InputIt>::value_type, Alloc>;
example
#include <forward_list> #include <vector> int main() { std::vector<int> v = {1, 2, 3, 4}; // uses explicit deduction guide to deduce std::forward_list<int> std::forward_list x(v.begin(), v.end()); // deduces std::forward_list<std::vector<int>::iterator> // first phase of overload resolution for list-initialization selects the candidate // synthesized from the initializer-list constructor; second phase is not performed and // deduction guide has no effect std::forward_list y{v.begin(), v.end()}; }
https://en.cppreference.com/w/cpp/container/forward_list/deduction_guides
5. list
C++11
template< class T, class Allocator = std::allocator<T> > class list;
example
#include <algorithm> #include <iostream> #include <list> int main() { // Create a list containing integers std::list<int> l = { 7, 5, 16, 8 }; // Add an integer to the front of the list l.push_front(25); // Add an integer to the back of the list l.push_back(13); // Insert an integer before 16 by searching auto it = std::find(l.begin(), l.end(), 16); if (it != l.end()) { l.insert(it, 42); } // Iterate and print values of the list for (int n : l) { std::cout << n << '\n'; } }
output:
25 7 5 42 16 8 13
https://en.cppreference.com/w/cpp/container/list
C++17
template< class InputIt, class Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type>> list(InputIt, InputIt, Alloc = Alloc()) -> list<typename std::iterator_traits<InputIt>::value_type, Alloc>;
example
#include <list> #include <vector> int main() { std::vector<int> v = {1, 2, 3, 4}; // uses explicit deduction guide to deduce std::list<int> std::list x(v.begin(), v.end()); // deduces std::list<std::vector<int>::iterator> // first phase of overload resolution for list-initialization selects the candidate // synthesized from the initializer-list constructor; second phase is not performed and // deduction guide has no effect std::list y{v.begin(), v.end()}; }
https://en.cppreference.com/w/cpp/container/list/deduction_guides
list Class
https://docs.microsoft.com/en-us/cpp/standard-library/list-class?view=vs-2019
6. set
C++11
template< class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key> > class set;
https://en.cppreference.com/w/cpp/container/set
C++17
template<class Key, class Alloc> set(std::initializer_list<Key>, Alloc) -> set<Key, std::less<Key>, Alloc>;
example
#include <set> int main() { std::set s = {1,2,3,4}; // guide #2 deduces std::set<int> std::set s2(s.begin(), s.end()); // guide #1 deduces std::set<int> }
https://en.cppreference.com/w/cpp/container/set/deduction_guides
7. multiset
C++11
template< class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key> > class multiset;
https://en.cppreference.com/w/cpp/container/multiset
C++17
template<class Key, class Alloc> multiset(std::initializer_list<Key>, Alloc) -> multiset<Key, std::less<Key>, Alloc>;
example
#include <set> int main() { std::multiset s = {1,2,3,4}; // guide #2 deduces std::multiset<int> std::multiset s2(s.begin(), s.end()); // guide #1 deduces std::multiset<int> }
https://en.cppreference.com/w/cpp/container/multiset/deduction_guides
8. map
C++11
template< class Key, class T, class Compare = std::less<Key>, class Allocator = std::allocator<std::pair<const Key, T> > > class map;
https://en.cppreference.com/w/cpp/container/map
C++17
template<class Key, class T, class Allocator> map(std::initializer_list<std::pair<Key, T>>, Allocator) -> map<Key, T, std::less<Key>, Allocator>;
example
#include <map> int main() { // std::map m1 = {{"foo", 1}, {"bar", 2}}; // Error: braced-init-list has no type; // cannot deduce pair<Key, T> from // {"foo", 1} or {"bar", 2} std::map m1 = {std::pair{"foo", 2}, {"bar", 3}}; // guide #2 std::map m2(m1.begin(), m1.end()); // guide #1 }
https://en.cppreference.com/w/cpp/container/map/deduction_guides
map Class
https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=vs-2019
9. multimap
C++11
template< class Key, class T, class Compare = std::less<Key>, class Allocator = std::allocator<std::pair<const Key, T> > > class multimap;
https://en.cppreference.com/w/cpp/container/multimap
C++17
template<class Key, class T, class Allocator> multimap(std::initializer_list<std::pair<Key, T>>, Allocator) -> multimap<Key, T, std::less<Key>, Allocator>;
example
#include <map> int main() { // std::multimap m1 = {{"foo", 1}, {"bar", 2}}; // Error: braced-init-list has no type; // cannot deduce pair<Key, T> from // {"foo", 1} or {"bar", 2} std::multimap m1 = {std::pair{"foo", 2}, {"bar", 3}}; // guide #2 std::multimap m2(m1.begin(), m1.end()); // guide #1 }
https://en.cppreference.com/w/cpp/container/multimap/deduction_guides
10. unordered_set
C++11
template< class Key, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class Allocator = std::allocator<Key> > class unordered_set;
https://en.cppreference.com/w/cpp/container/unordered_set
C++17
template<class T, class Hash, class Alloc> unordered_set(std::initializer_list<T>, typename /*see below*/::size_type, Hash, Alloc) -> unordered_set<T, Hash, std::equal_to<T>, Alloc>;
example
#include <unordered_set> int main() { std::unordered_set s = {1,2,3,4}; // guide #2 deduces std::unordered_set<int> std::unordered_set s2(s.begin(), s.end()); // guide #1 deduces std::unordered_set<int> }
https://en.cppreference.com/w/cpp/container/unordered_set/deduction_guides
11. unordered_multiset
C++11
template< class Key, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class Allocator = std::allocator<Key> > class unordered_multiset;
https://en.cppreference.com/w/cpp/container/unordered_multiset
C++17
template<class T, class Hash, class Alloc> unordered_multiset(std::initializer_list<T>, typename /*see below*/::size_type, Hash, Alloc) -> unordered_multiset<T, Hash, std::equal_to<T>, Alloc>;
example
#include <unordered_set> int main() { std::unordered_multiset s = {1,2,3,4}; // guide #2 deduces std::unordered_multiset<int> std::unordered_multiset s2(s.begin(), s.end()); // guide #1 deduces std::unordered_multiset<int> }
https://en.cppreference.com/w/cpp/container/unordered_multiset/deduction_guides
12. unordered map
C++11
template< class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class Allocator = std::allocator< std::pair<const Key, T> > > class unordered_map;
example
#include <iostream> #include <string> #include <unordered_map> int main() { // Create an unordered_map of three strings (that map to strings) std::unordered_map<std::string, std::string> u = { {"RED","#FF0000"}, {"GREEN","#00FF00"}, {"BLUE","#0000FF"} }; // Iterate and print keys and values of unordered_map for( const auto& n : u ) { std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n"; } // Add two new entries to the unordered_map u["BLACK"] = "#000000"; u["WHITE"] = "#FFFFFF"; // Output values by key std::cout << "The HEX of color RED is:[" << u["RED"] << "]\n"; std::cout << "The HEX of color BLACK is:[" << u["BLACK"] << "]\n"; return 0; }
Output:
Key:[RED] Value:[#FF0000] Key:[BLUE] Value:[#0000FF] Key:[GREEN] Value:[#00FF00] The HEX of color RED is:[#FF0000] The HEX of color BLACK is:[#000000]
https://en.cppreference.com/w/cpp/container/unordered_map
C++17
template<class Key, class T, class Hash, class Alloc> unordered_map(std::initializer_list<std::pair<Key, T>>, typename /*see below*/::size_type, Hash, Alloc) -> unordered_map<Key, T, Hash, std::equal_to<Key>, Alloc>;
example
#include <unordered_map> int main() { // std::unordered_map m1 = {{"foo", 1}, {"bar", 2}}; // Error: braced-init-list has no type // cannot deduce pair<Key, T> from // {"foo", 1} or {"bar", 2} std::unordered_map m1 = {std::pair{"foo", 2}, {"bar", 3}}; // guide #2 std::unordered_map m2(m1.begin(), m1.end()); // guide #1 }
https://en.cppreference.com/w/cpp/container/unordered_map/deduction_guides
13. unordered_multimap
C++11
template< class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class Allocator = std::allocator< std::pair<const Key, T> > > class unordered_multimap;
https://en.cppreference.com/w/cpp/container/unordered_multimap
C++17
example
#include <unordered_map> int main() { // std::unordered_multimap m1 = {{"foo", 1}, {"bar", 2}}; // Error: braced-init-list has no type // cannot deduce pair<Key, T> from // {"foo", 1} or {"bar", 2} std::unordered_multimap m1 = {std::pair{"foo", 2}, {"bar", 3}}; // guide #2 std::unordered_multimap m2(m1.begin(), m1.end()); // guide #1 }
https://en.cppreference.com/w/cpp/container/unordered_multimap/deduction_guides
14. stack
C++11
template< class T, class Container = std::deque<T> > class stack;
https://en.cppreference.com/w/cpp/container/stack
C++17
example
#include <vector> #include <stack> int main() { std::vector<int> v = {1,2,3,4}; std::stack s{v}; // guide #1 deduces std::stack<int, vector<int>> }
https://en.cppreference.com/w/cpp/container/stack/deduction_guides
15. queue
C++11
template< class T, class Container = std::deque<T> > class queue;
https://en.cppreference.com/w/cpp/container/queue
C++17
example
#include <vector> #include <queue> int main() { std::vector<int> v = {1,2,3,4}; std::queue s{v}; // guide #1 deduces std::queue<int, vector<int>> }
https://en.cppreference.com/w/cpp/container/queue/deduction_guides
queue Class
https://docs.microsoft.com/en-us/cpp/standard-library/queue-class?view=vs-2019
16. priority_queue
C++11
template< class T, class Container = std::vector<T>, class Compare = std::less<typename Container::value_type> > class priority_queue;
example
#include <functional> #include <queue> #include <vector> #include <iostream> template<typename T> void print_queue(T& q) { while(!q.empty()) { std::cout << q.top() << " "; q.pop(); } std::cout << '\n'; } int main() { std::priority_queue<int> q; for(int n : {1,8,5,6,3,4,0,9,7,2}) q.push(n); print_queue(q); std::priority_queue<int, std::vector<int>, std::greater<int> > q2; for(int n : {1,8,5,6,3,4,0,9,7,2}) q2.push(n); print_queue(q2); // Using lambda to compare elements. auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1); }; std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp); for(int n : {1,8,5,6,3,4,0,9,7,2}) q3.push(n); print_queue(q3); }
output:
9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 8 9 6 7 4 5 2 3 0 1
https://en.cppreference.com/w/cpp/container/priority_queue
C++17
example
#include <vector> #include <queue> int main() { std::vector<int> v = {1,2,3,4}; std::priority_queue pq1{v}; // deduces std::priority_queue<int> std::priority_queue pq2{v.begin(), v.end()}; // deduces std::priority_queue<int> }
https://en.cppreference.com/w/cpp/container/priority_queue/deduction_guides
17. span
C++20
template< class T, std::size_t Extent = std::dynamic_extent > class span;
https://en.cppreference.com/w/cpp/container/span
III. Basic Concepts
std::atomic<int> cnt{0}; auto f = [&]{cnt++;}; std::thread t1{f}, t2{f}, t3{f}; // OK
2. Access to pointer passed to malloc/realloc
#include <iostream> #include <cstdlib> int main() { int *p = (int*)std::malloc(sizeof(int)); int *q = (int*)std::realloc(p, sizeof(int)); *p = 1; // UB access to a pointer that was passed to realloc *q = 2; if (p == q) // UB access to a pointer that was passed to realloc std::cout << *p << *q << '\n'; }
Possible output:
12
IV. Coroutines (since C++20)
V. Modules (since C++20)
Modules help divide large amounts of code into logical parts.
Modules are orthogonal to namespaces.
// helloworld.cpp export module helloworld; // module declaration import <iostream>; // import declaration export void hello() { // export declaration std::cout << "Hello world!\n"; }
// main.cpp import helloworld; // import declaration int main() { hello(); }
https://en.cppreference.com/w/cpp/language/modules
VI. Application
1. deque
#include <iostream> #include <deque> typedef struct{ const char * buf; int type; int size; } data_t; int main() { // Create a deque containing integers std::deque<data_t> x;
//std::deque<data_t> x(32); data_t d4; d4.type = 4; d4.buf = "hello4"; data_t d5; d5.type = 5; d5.buf = "hello5"; data_t d6; d6.type = 6; d6.buf = "hello6"; x.push_front(d4); x.push_back(d5); x.push_back(d6); // Iterate and print values of deque for(data_t n : x) { std::cout << n.type << '\n'; } }
2. deque + vector
#include <string> #include <iostream> #include <iterator> #include <deque> #include <vector> typedef struct{ const char * buf; int type; int size; } data_t; int main() { // uses explicit deduction guide to deduce std::deque<int> std::vector<data_t> v={{"hello1",1,6},{"hello2",2,6},{"hello3",3,6}}; std::deque x(v.begin(), v.end()); data_t d4; d4.type = 4; d4.buf = "hello4"; data_t d5; d5.type = 5; d5.buf = "hello5"; data_t d6; d6.type = 6; d6.buf = "hello6"; x.push_front(d4); x.push_back(d5); x.push_back(d6); for(data_t n : x) std::cout << n.type << n.buf << '\n'; return 0; }
https://github.com/clpsz/cpp11-new-features
Minimal and extensible websocket server library written in C++11 and using boost ASIO
https://github.com/nullseed/ws
simple C++11 ring buffer implementations for embedded targets, allocated and evaluated at compile time
https://github.com/jnk0le/Ring-Buffer
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步