Reputation: 2079
We can initialize a vector
in C++11
in the following way
vector<int> v {1,2,3,4,5,6}
But what if I want to initialize a vector<int> v(1000)
with values {1,1,1,1....1}
?
In python it would be:
somelist [1] * 1000
I'm not sure if Python has a loop behind that line but my goal by omitting the loop is to do better in terms of performance and a simpler code.
Upvotes: 2
Views: 2753
Reputation: 227618
If you want a vector of 1000 elements, all set to 1
, then std::vector
has a constructor that does that for you:
vector<int> v(1000, 1);
The time complexity of this constructor is necessarily linear, but you can expect it to be very fast. You would be hard pressed to find a faster alternative, so if this is an issue, you might require a re-design.
Upvotes: 9