cplusplus.com
C++ : Forum : Beginners : How do I Convert a Vector to a String Ar
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs


question How do I Convert a Vector to a String Array?

lazyrussian (7)
Hiya,

I was wondering how I would go about converting a vector to a string array?

Would the following be proper:

1
2
vector<string> screen;
string scr(screen.begin(), screen.end());

or do I have to allot a certain amount of space/memory for scr before I fill the array?

1
2
3
vector<string> screen;
string scr[vector.size()];
scr[] = scr(screen.begin(), screen.end());

or something like that?


(Pretend for a moment that my vector has already been initialized with values)

Thanks.
Mitsakos (343)
I think you can not do the first method because the vector is a container that contains string which is also a container... You can only do your first example between vectors.

You can do it by your second but you can not declare the size of scr[] array like that. You have to do it at runtime. So you have to use pointer to string.
1
2
3
4
5
6
7
vector<string> screen;
...//add data to screen here
string *scr;
scr = new string[screen.size()];
for(int i=0; i<screen.size(); i++){
   scr[i] = screen[i];//Copy the vector to the string
}


Hope this helps
Last edited on
firedraco (4744)
Lolz Mistsakos got it before me!

Btw, thanks for code tags actually.
Last edited on
Topic archived. No new replies allowed.