Array of strings

How do I build an array of strings?

It needs to be dynamic obviously so I'm thinking I should use a vector but I'm not sure how to do that? It will be a 2 dimensional array.

Any help appreciated
The Reference section of this website can give you a quick start on using std::vector. If not, I recommend that you study a C++ tutorial on dynamic memory allocation and pointers. Those would be the 2 most obvious alternatives, I would say.
Use vectors from the standard template library to do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Vector<string> v;
v.push_back("string1");
v.push_back("string2");
v.push_back("string3");

cout << v[2] << endl; //this will print out string3

for (int i = 0; i < 3; i++) {
    cout << v[i] << endl; //this will print out:
/*
string1
string2
string3
*/
}
Last edited on
Topic archived. No new replies allowed.