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’.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist