我想使用自定义分隔符将 vector 的内容复制到一个长 string 中.到目前为止,我已经尝试过:
I would like to copy the contents of a vector to one long string with a custom delimiter. So far, I've tried:
// .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* 但我得到 p>
I've tried with charT* but I get
error iso c++ forbids declaration of charT with no type
然后我尝试使用 char 和 ostream_iterator但我在字符串中得到了奇怪的字符.
Then I tried using char and ostream_iterator<int>(s,&delimiter)
but I get strange characters in the string.
谁能帮我理解编译器在这里的期望?
Can anyone help me make sense of what the compiler is expecting here?
使用 delimiter.c_str() 作为分隔符:
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 得到的.
That way, you get a const char* pointing to the string, which is what ostream_operator expects from your std::string.
这篇关于std::vector 到带有自定义分隔符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
fpermissive 标志有什么作用?What does the fpermissive flag do?(fpermissive 标志有什么作用?)
如何在我不想编辑的第 3 方代码中禁用来自 gccHow do you disable the unused variable warnings coming out of gcc in 3rd party code I do not wish to edit?(如何在我不想编辑的第 3 方代码中禁
使用 GCC 预编译头文件Precompiled headers with GCC(使用 GCC 预编译头文件)
如何在 OS X 中包含 omp.h?How to include omp.h in OS X?(如何在 OS X 中包含 omp.h?)
如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文How can I make GCC compile the .text section as writable in an ELF binary?(如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文件中?)
GCC、字符串化和内联 GLSL?GCC, stringification, and inline GLSL?(GCC、字符串化和内联 GLSL?)