The string series--std:vector<char> and std:string in the actual combat C + + conversion (vector to StringStream)

Source: Internet
Author: User

Sometimes you will also encounter Std:vector and turn std:string each other in the case of conversion.

First look at vector<char> how to turn a string:

STD:: vector<char>*data = Response->getresponsedata ();STD::stringRes//Method one for(inti =0; I<data->size (); ++i) {res+= (*data) [i];} res+=' + ';STD:cout<< Res;//Method twoSTD:: vector<char>*data = Response->getresponsedata ();STD::stringRes;res.insert (Res.begin (), Data->begin (), Data->end ());STD::cout<< Res;//Method threeSTD:: vector<char>*data = Response->getresponsedata ();STD::stringResConst Char* s = & (*data->begin ()); res =STD::string(s, data->size ());STD::cout<< Res;//Method fourstringCH ="What a fucking day!"; vector <char>Ta;ta.resize (Ch.size ()); Ta.assign (Ch.begin (), Ch.end ());

String to vector is easier:

vector <char> ta = {‘a’, ‘b‘, ‘c‘};ch.clear();ch.assign(ta.begin(),ta.end());

================================================================
Vector to StringStream

#include <iostream>#include <sstream>#include <vector>#include <string>#include <iterator> //Dummy std::vector of Strings STD:: Vector<std::string>sentence; Sentence.push_back ("AA"); Sentence.push_back ("AB");//Required Std::stringstream Object STD::StringStreamss//Populate STD:: Copy (Sentence.begin (), Sentence.end (),STD::ostream_iterator<STD::string> (SS,"\ n"));//Display STD::cout<<ss.str () <<STD:: Endl;

The string series--std:vector<char> and std:string in the actual combat C + + conversion (vector to StringStream)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.