C++
This document summarizes snippets and useful C++ features categorized by C++ version.
C++11
Lambda functions
std::for_each(array.begin(), array.end(), [](int x) {
std::cout << x << std::endl;
});
Recursive lambda functions:
std::function<int(int)> fibonacci = [&fibonacci](int n) {
return n < 2 ? 1 : fibonacci(n-1) + fibonacci(n-2);
};
foreach-style loops
for (const auto v : {1, 2, 3}) {
std::cout << v << std::endl;
}
std::map<std::string, int> map;
for (const auto & v : map) {
std::cout << v.first << " = " << v.second << std::endl;
}
std::begin() and std::end()
int array[] = {1, 2, 3};
std::for_each(std::begin(array), std::end(array), [](int i) {
// ...
});
auto
With auto
, the compiler has to guess the type of a variable.
std::map<std::string, int> map;
for (auto it = map.begin(); it != map.end(); ++it) {
// ...
}
auto
can be used as a return type of a function in this case:
template<typename T1, typename T2>
auto add(T1 t1, T2 t2) -> decltype(t1 + t2) {
return t1 + t2;
}
auto value = add(1, 2.3);
List initialization
std::map<std::string, int> = {
{"a", 1},
{"b", 2}
};
nullptr
The keyword nullptr
represents a null pointer.
int * p = nullptr;
if (p == nullptr) {
// ...
}
Move and copy
A class has 5 default operations:
- copy constructor
- move constructor
- destructor
- copy assigment
- move assignment
\
class A {
A(const A &); // copy constructor
A(A &&); // move constructor
~A(); // destructor
A & operator=(const A &); // copy assignment
A & operator=(A &&); // move assigment
};
override and final
To explicitly inform that a method overrides a virtual method. Makes the code more understandable.
The keyword final
makes a method impossible to override.
class A {
public:
virtual void f(int) {}
};
class B : public A {
public:
virtual void f(int) override {}
};
class C : public B {
public:
virtual void f(int) override final {}
};
default and delete
You can specify a default copy behavior using default
.
class A {
A& operator=(const A &) = default;
A(const A &) = default;
};
You can forbid copying an object using delete
.
class A {
A& operator=(const A &) = delete;
A(const A &) = delete;
};
delete
can be used for any function, for example to forbid an argument type conversion.
class A {
void f(long long);
void f(long) = delete;
};
Initialize a vector of indices from 0 to n-1 and sort it according to another vector
#include <algorithm> // std::sort
#include <numeric> // std::iota
#include <vector> // std::vector
std::vector<std::size_t> indices(n);
// indices = [0, 0, 0, ..., 0]
std::iota(index.begin(), index.end(), 0);
// indices = [0, 1, 2, ..., n-1]
std::sort(index.begin(), index.end(), [&](auto i, auto j) { return values[i] < values[j]; });
// indices are sorted by increasing values
C++14
auto lambda function arguments
Lambda functions can be declared without explicit argument types.
auto lambda = [](auto x, auto y) { return x + y; };
auto return type
auto f() {
return 1.;
}
If a function has multiple return
, all of them have to be of the same type.