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
commented: This is c++? -2

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

Member Avatar for iamthwee

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.