Data structures and Algorithm Analysis in c++ 4th edition p16 #include <iostream> #include <algorithm> #include <functional> #include <vector> #include <random> #include <ctime> #include <map> #include <fstream> class IntCell{ public: explicit IntCell(int initialValue = 0); int read() const; void write(int x); private: int storedValue; };
IntCell::IntCell(int initialValue) : storedValue{initialValue}{ }
int IntCell::read() const{ return storedValue; }
void IntCell::write(int x){ storedValue = x; }
void fillVector(std::vector<int>& vec, const std::function<int()>& generator) { generate(begin(vec), end(vec), generator); }
template<class IntType = int> class uniform_int_distribution uniform_int_distribution(IntType a = 0, IntType b = std::numeric_limits<IntType>::std::max()); template<class RealType = double> class uniform_real_distribution uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);
template<class RealType = double> class normal_distribution normal_distribution(RealType mean = 0.0, RealType stddev = 1.0); template<class RealType = double> class lognormal_distribution lognormal_distribution(RealType m = 0.0, RealType s = 1.0); template<class RealType = double> class chi_squared_distribution chi_squared_distribution(RealType n = 1); template<class RealType = double> class cauchy_distribution cauchy_distribution(RealType a = 0.0, RealType b = 1.0); template<class RealType = double> class fisher_f_distribution fisher_f_distribution(RealType m = 1, RealType n = 1); template<class RealType = double> class student_t_distribution student_t_distribution(RealType n = 1); //c13.cpp:761:10: 错误:expected unqualified-id before ‘template’ //template<class IntType = int> class discrete_distribution // discrete_distribution(std::initializer_list<double> wl); //template<class RealType = double> class piecewise_constant_distribution // template<class UnaryOperation> // piecewise_constant_distribution (std::initializer_list<RealType>b1, // UnaryOperation fw); //template<class RealType = double> class piecewise_linear_distribution // template<class UnaryOperation> // piecewise_linear_distribution (std::initializer_list<RealType>b1, // UnaryOperation fw); int main(){ std::vector<int> squares(100); for(int i = 0; i < squares.size(); ++i) squares[i] = i * i; for(int i = 0; i < squares.size(); ++i) std::cout << "i: = " << i << ' ' << "squares = " << squares[i] << std::endl; int sum = 0; // for(int i = 0; i < squares.size(); ++i) // (1) // sum += squares[i] ; // range for 只允许对项目的查看 // for (int x : squares) // (2) for(auto x1 :squares) // (3) sum += x1 ; std::cout << "sum = " << sum << std::endl; IntCell *m1; m1 = new IntCell{0}; m1->write(5);// 类成员write() 被 对象m 通过 -> 操作符 访问. std::cout << "Cell contents: " << m1->read() << std::endl; delete m1; std::vector<std::string> arr(3); const int x2 = 2; int y ; int z = x2 + y; std::string str = "foo"; std::vector<std::string> *ptr = &arr; // arr, str, arr[x], &x, y, z, ptr, *ptr, (*ptr)[x] 都是左值. // 2, "foo", x2 + y, str.substr(0, 1) 都是右值. p20 2020年06月30日 22时39分43秒
std::random_device rnd; std::cout << "Entropy: " << rnd.entropy() << std::endl; std::cout << "Min value: " << rnd.min() << ", Max value: " << rnd.max() << std::endl; std::cout << "Random number: " << rnd() << std::endl; //professional c++ 4th edition p473 std::random_device seeder; const auto seed1 = seeder.entropy() ? seeder() : time(nullptr); std::mt19937 eng1(static_cast<std::mt19937::result_type>(seed1));
std::uniform_int_distribution<int> dist1(1, 99); std::cout << dist1(eng1) << std::endl; auto gen1 = std::bind(dist1, eng1); std::vector<int> vec1(10); generate(begin(vec1), end(vec1), gen1); for (auto i : vec1) { std::cout << i << " "; } std::cout << std::endl; auto gen2 = std::bind(dist1, eng1);
std::vector<int> vec2(10); fillVector(vec2, gen2);
for (auto i : vec2) { std::cout << i << " "; } std::cout << std::endl; //professional c++ 4th edition p475 const unsigned int kStart = 1; const unsigned int kEnd = 99; const unsigned int kIterations = 1'000'000;
// Uniform Mersenne Twister std::random_device seeder2; const auto seed2 = seeder2.entropy() ? seeder2() : time(nullptr); std::mt19937 eng2(static_cast<std::mt19937::result_type>(seed2)); std::uniform_int_distribution<int> dist2(kStart, kEnd); auto gen3 = bind(dist2, eng2); std::map<int, int> m2; for (unsigned int i = 0; i < kIterations; ++i) { int rnd = gen3(); // Search map for a key = rnd. If found, add 1 to the value associated // with that key. If not found, add the key to the map with value 1. ++(m2[rnd]); } // Write to a CSV file std::ofstream of1("res.csv"); for (unsigned int i = kStart; i <= kEnd; ++i) { of1 << i << "," << m2[i] << std::endl; } std::normal_distribution<double> dist3(50, 10); auto gen = bind(dist3, eng2); std::map<int, int> m3; for (unsigned int i = 0; i < kIterations; ++i) { int rnd = static_cast<int>(gen()); // Search map for a key = rnd. If found, add 1 to the value associated // with that key. If not found, add the key to the map with value 1. ++(m3[rnd]); } // Write to a CSV file std::ofstream of2("res.csv"); for (unsigned int i = kStart; i <= kEnd; ++i) { of2 << i << "," << m3[i] << std::endl; } return 0; } /* wannian07@wannian07-PC:~$ g++ -std=c++17 -o c13 c13.cpp wannian07@wannian07-PC:~$ ./c13 i: = 0 squares = 0 i: = 1 squares = 1 i: = 2 squares = 4 : : i: = 98 squares = 9604 i: = 99 squares = 9801
sum = 328350
Cell contents: 5
Entropy: 32 Min value: 0, Max value: 4294967295 Random number: 4147688544
22
95 9 19 11 94 11 35 22 73 14
95 9 19 11 94 11 35 22 73 14
*/