How do you initialize an STL list with the values when you declare it? That way I don't have to add the values later with a loop and push_back.

Thanks,
-Matt

std::vector<char> myVector(10, 'a'); Creates a character vector filled with 10 copies of the character 'a'.

a sequence container can also be initialized with a sequence identified by a pair of iterators.

char cstr[] = "abcdefghijkl" ;
  std::list<int> list2( cstr, cstr+sizeof(cstr) ) ;

in c++0x, the following would be another way:

std::list<int> list5 = { 23, 6, 57, 575, 7575, 75 }; // like aggreagate initializer in C
Comments
This is c++?

I'm sorry, I should have specified, I need to initialize it with numbers, but not all the numbers are the same, so I can't modify the "10 copies of 'a'" suggestion. Something like, initializing an integer vector (or list) with the numbers 14, 15, 17, 18, and 19.

Thanks,
-Matt

>That way I don't have to add the values later with a loop and push_back.

Well what's the harm in using a loop and push_back? Pray tell? You could also use Vj's second example. But I don't see the difference other than semantics.

> You could also use Vj's second example.
It is not C++. He was giving an example of how it would be in C++0X.

This article has been dead for over six months. Start a new discussion instead.