unseen_rider
unseen_rider

Reputation: 324

C++ std::vector arguments in brackets - explanation?

I am inexperienced at c++. I have come across the below code

std::vector<char> is_prime(sqrt + 1, 1);

Update Where sqrt is a positive integer.

I believe it is defining a vector filled with characters, naming the vector is_prime, but I don't understand what the purpose of the two arguments are.

I have had a look at the documentation for std::vector, however this wasn't clear to me. I have also searched SO but no question I found helped.

Example: say sqrt is 4, then this would be in effect:

is_prime(5, 1);

Does this mean it is a vector with elements 5 and 1? A vector with size 5 and first element 1?

Upvotes: 0

Views: 5662

Answers (2)

Christopher Pisz
Christopher Pisz

Reputation: 4000

Probably the second form of vector's constructor http://www.cplusplus.com/reference/vector/vector/vector/

vector (size_type n, const value_type& val = value_type(),
             const allocator_type& alloc = allocator_type());

Which would fill a vector of characters with sqrt + 1 elements, all set to the whatever 1 is as a character.

Upvotes: 0

Andrey Belykh
Andrey Belykh

Reputation: 2654

first argument is "Initial container size", second argument is "Value to fill the container with. Each of the n elements in the container will be initialized to a copy of this value."

http://www.cplusplus.com/reference/vector/vector/vector/

Upvotes: 2

Related Questions