C ++ 11 seed_seq initialization

The header file <random> allows you to initialize the internal sequence of the seed sequence. An object of class seed_seq can be constructed in several ways. I am wondering which method is used in C ++.

I look at the site here: http://www.cplusplus.com/reference/std/random/seed_seq/seed_seq/

And in the example section, I see this line:

 std::seed_seq seed2 = {102,406,7892}; 

What exactly is going on here? It seems the class object is assigned to an array. I looked at the list-initializer-constructor, the copy destination constructor, and I'm still confused about what exactly is going on.

I understand std::seed_seq seed3 (foo.begin(),foo.end()); and std::seed_seq seed1; . The first code fragment ( seed3 ) calls the seed_seq constructor with the arguments foo.begin() and foo.end() , and the second code fragment ( seed1 ) is constructed using the default constructor.

+4
source share
1 answer

I'm not sure I fully understood your question, since you yourself answered. Using something like {102,406,7892} is a list of initializers. A constructor method (or virtually any method) with a signature of type MyClass::MyClass(std::initializer_list<int> args) can be used.

You can iterate over it using the usual begin() and end() iterator methods. Basically this is just a convenient way to pass a list of arbitrary length in the code without having to initialize the "normal" std::list or std::vector (and keep calling push_back() on it) or an array.

As a bonus, you can also create standard containers using initializer lists: std::vector<std::string> vec {"hello", "world"} . This allows you to use standard containers as argument types for functions that can still be called using initializer_list.

+5
source

All Articles