vector assignment question

I am trying to assign a list of values to a vector:

vector<string> words;
words[] = {"one", "two", "three"};

This does not work. How can I accomplish it?

If you have C++11:

vector<string> words = {"one", "two", "three"};

Should work.

Otherwise you'll have to push them all in yourself or use a proxy array.
I need to assign the values after the declaration.

words = {"one", "two", "three"};

does not work.
(use some high-level programming language which supports what humans need to express algorithms. Sorry ;-))
This will do it serjo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <vector>


int main()
{
std::vector<std::string> Vec;
std::string text;
for (int i = 0; i <= 2; ++i)
{
    std::cout << "\nEnter word: " << i+1 << " ";
    std::cin >> text;
    Vec.push_back(text);
}
for (int i = 0; i <= 2; ++i)
{
    std::cout << '\n' << Vec[i];
}
}
I need to assign the values after the declaration.

words = {"one", "two", "three"};

does not work.


It works -- with a compiler which supports C++11 initializer lists.
Hint, Visual Studio 2010 and 2012 don't use such a compiler.
Topic archived. No new replies allowed.