devxlogo

Copying a String Object to a Vector

Copying a String Object to a Vector

Although the string class is very convenient for most string manipulation operations, it may be necessary sometimes to copy the contents of a string to a different container, e.g., in order to transmit the string to a remote host. You can copy the contents of a string to a vector of chars like this:

 #include #include using std::string;using std::vector;int main(){  string musical = "Cats";  vector vc (musical.begin(), musical.end());  //1}

In the line numbered (1), a vector of char is initialized with the contents of the string ‘musical’.

devx-admin

Share the Post: