如何从std :: vector中创建std :: string< char&gt ;? [英] How to construct a std::string from a std::vector<char>?

查看:155
本文介绍了如何从std :: vector中创建std :: string< char&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

缺少(显而易见的)首先构建一个C风格的字符串,然后使用它来创建一个std :: string,是否有更快/替代/更好的方式从chars的向量初始化一个字符串?

Short of (the obvious) building a C style string first then using that to create a std::string, is there a quicker/alternative/"better" way to initialize a string from a vector of chars?

推荐答案

好,最好的方法是使用下面的构造函数:

Well, the best way is to use the following constructor:

template<class InputIterator> string (InputIterator begin, InputIterator end);

这将导致类似:

std::vector<char> v;
std::string str(v.begin(),v.end());

我希望它有帮助。

这篇关于如何从std :: vector中创建std :: string&lt; char&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆