vector initilization problem

Hi all

When we declare a vector we can at the same time initilize it.
such as
vector<double> vec(5);
this fills the vector with 5 zeros;

But if we dont, how can we "initilize" it later without using a for loop ?
i mean fill the vector with a size with some value easily?
i tried this and didnt work:
vector<double> vec;
vec(5);
vec.assign( 5, 0 );
alternatively,
vec.resize(5); // default constructs elements of value type
vec.resize(5, 0); // set all elements to 0
vec.resize(5, 25.5); // set all elements to 25.5
vec.insert(vec.begin(), 5, 25.5); // insert 5 elements at beginning each with value 25.5

Take a look at vector's page. There are many ways to accomplish this depending on how you want to initialize the values and whether or not there was previous content within the container.
http://cplusplus.com/reference/stl/vector/
Another way is:

1
2
3
vector<int> vec;
//puts 4 elements of value 20 in vec
fill_n (back_inserter(vec), 4, 20);
Topic archived. No new replies allowed.