【发布时间】:2016-04-19 12:32:41
【问题描述】:
如何将向量转换为 const char* ?还要串? 这合法吗?
void Printer::Wrap(const CharVector& text, RangeVector& result) const
//if (TestLineWidth(text.begin(), text.end()))
int c = 0;
for (CharVector::const_iterator it = text.begin(); it != text.end(); ++it)
c++;
if (TestLineWidth(&text[0], &text[c]))
这里是 CharVector 和 TestLineWidth 的声明:
typedef std::vector<char> CharVector;
bool TestLineWidth(const char* from, const char* to) const;
【问题讨论】:
-
在c++11中有一个方法std::vector::data。对于旧版本,您可以使用
&text[0]。但请记住,c-string 是一个以 null 结尾的字符串,并且向量末尾可能没有 null -
为什么会涉及到vector
?为什么不使用 std::string?它就是为此而生的! -
@Klaus 可能存在一个字节数组,它可能不仅包含字符串,也可能不像 HTTP 客户端的某些实现那样仅包含单个字符串。
标签: c++ string iterator char stdvector