-3

Basically I just need to fill the string with all the letters from the vector. The vector is of type char, but it shouldn't matter right? When I debugged it, it said the size of string was still 0? Here's a snippet of code.

NOTE: Vector size is 7 (tested in output) so the problem doesn't seem to lie in the vector.

vector<char> final; //note this gets filled before reaching the loop
// fills vector in here, size is now 7

string* complete;
complete = new string[final.size()]; //set size of string to vector size
//debugger says size of complete is 0????
for (int i = 0; i < final.size(); i++) {
    complete[i] = final[i]; //should fill string
}

cout << "COMPLETE:" << *complete << endl; //one letter output
Derek H
  • 31
  • 7
  • 2
    You're initializing `complete` to an array of `string`s. – bejado Feb 24 '17 at 23:56
  • `complete = new string[final.size()]` you create an array of strings here. What you want to do is: instead of using pointers, just go with simple `std::string`. Pretty sure that `std::string complete(final.data())` will do the trick – Fureeish Feb 24 '17 at 23:58
  • Ohh! You're totally right! Thanks for pointing that out! – Derek H Feb 24 '17 at 23:58
  • Possible duplicate of [Converting a vector to string](http://stackoverflow.com/questions/8581832/converting-a-vector-to-string) – HDJEMAI Feb 25 '17 at 00:41

1 Answers1

2

Here's a one-liner to do it:

string complete(final.begin(), final.end());

or:

string complete(final.data(), final.size());
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • There is an exact answer for the same question here: http://stackoverflow.com/questions/8581832/converting-a-vector-to-string – HDJEMAI Feb 25 '17 at 20:28