[toc]
Shuffle
1 2 3 4 5 6
| static void shuffle(vector<int>* v) { srand(9); for (int n = v->size(); n >= 2; n--) { swap((*v)[n - 1], (*v)[static_cast<unsigned>(rand()) % n]); } }
|
class 成员初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| $ cat 010.cpp #include <iostream>
using namespace std;
class Plm { public: struct Arg { size_t net; size_t mem; size_t query; }; };
int main() { Plm::Arg arg { .net = 1, .mem = 3, .query = 2 }; cout << arg.net << " " << arg.mem << " " << arg.query << endl; return 0; } $ g++ 010.cpp && ./a.out 1 3 2
|
parameter pack
1 2 3 4 5 6 7
| template<typename... Arguments> void SampleFunction(Arguments... parameters);
template<typename T, typename U> void SampleFunction(T param1, U param2);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| bovenson@Dell:~/Git/notes/C++/Code/Learn$ cat 005.cpp #include <iostream>
using namespace std;
void fa(int a, int b) { cout << a << " " << b << endl; }
template<typename... Args> void f(Args... args) { fa((args)...); fa(args...); fa(std::forward<Args>(args)...); // fa(std::forward<Args>((args)...)); // ERROR }
int main() { f<int, int>(1, 2); // OK f(1, 2); // OK return 0; } bovenson@Dell:~/Git/notes/C++/Code/Learn$ g++ 005.cpp && ./a.out 1 2 1 2 1 2 1 2 1 2 1 2
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| MBP:Learn sunzhenkai$ cat 16.cpp #include <iostream> #include <vector>
using namespace std;
template <typename... T> void f(T... args) { std::vector<int> v = { args... }; for (auto item : v) { std::cout << item << std::endl; } }
int main() { f(1, 2, 3, 4, 5); return 0; } MBP:Learn sunzhenkai$ g++ 16.cpp -std=c++11 && ./a.out 1 2 3 4 5
|
完美转发
无论是T&&、左值引用、右值引用,std::forward都会依照原来的类型完美转发。
省略号语法
Amazing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| MBP:Learn sunzhenkai$ cat 15.cpp #include <iostream>
using namespace std;
class A { };
class B { public: B(A a) {} };
void fun(B b) { cout << "OK? OK!" << endl; }
int main() { A a; fun(a); return 0; } MBP:Learn sunzhenkai$ g++ 15.cpp && ./a.out OK? OK!
|