0

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?

unseen_rider
  • 324
  • 5
  • 23

2 Answers2

2

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/

Andrey Belykh
  • 2,578
  • 4
  • 32
  • 46
0

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.

Christopher Pisz
  • 3,757
  • 4
  • 29
  • 65
  • https://stackoverflow.com/questions/6520052/whats-wrong-with-cplusplus-com – François Andrieux Nov 03 '17 at 19:47
  • @Francois Not getting your reference. It's a link to someone asking what others don't like about cplusplus.com. It's closed as off topic. Are you trying to say that you dislike links to cplusplus.com? If so, then suggest a better reference. – Christopher Pisz Nov 03 '17 at 19:48