本文主要是介绍C++ string vector 转换,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.string转vector
#include#include#include#includeusing namespace std;int main(){
string str = "hello world!";
vector<uint8_t> vec;
vec.assign(str.begin(), str.end());
for(size_t i=0; i<vec.size(); i++){
cout<<vec[i]<<" ";
}
cout<<endl;
return 0;}
2.vector转string
#include#include#include#includeusing namespace std;int main(){
string str;
vector<char> vec={'a','a','a','b','a'};
str.assign(vec.begin(), vec.end());
cout<<str<<endl;
// vector转 string
string str2;
vector<string> vec2{"hhh", "zzz"};
str2 = accumulate(vec2.begin(), vec2.end(), str2);
cout<<"str2 = "<<str2<<endl;
return 0;}
这篇关于C++ string vector 转换的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!