我想复制 vector
的内容到一长string
带有自定义分隔符。到目前为止,我已经尝试过:
// .h
string getLabeledPointsString(const string delimiter=",");
// .cpp
string Gesture::getLabeledPointsString(const string delimiter) {
vector<int> x = getLabeledPoints();
stringstream s;
copy(x.begin(),x.end(), ostream_iterator<int>(s,delimiter));
return s.str();
}
但我明白了
no matching function for call to ‘std::ostream_iterator<int, char, std::char_traits<char> >::ostream_iterator(std::stringstream&, const std::string&)’
我试过 charT*
但我明白了
error iso c++ forbids declaration of charT with no type
然后我尝试使用 char
和 ostream_iterator<int>(s,&delimiter)
但我在字符串中得到了奇怪的字符。
谁能帮我理解编译器在这里的期望吗?
最佳答案
Use delimiter.c_str()
as the delimiter :
copy(x.begin(),x.end(), ostream_iterator<int>(s,delimiter.c_str()));
这样,您将获得一个指向字符串的 const char*
,这正是 ostream_operator
对 std::string
的期望。
关于c++ - std::vector 到带有自定义分隔符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9277906/