std::vector 到带有自定义分隔符的字符串

std::vector to string with custom delimiter(std::vector 到带有自定义分隔符的字符串)
本文介绍了std::vector 到带有自定义分隔符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想使用自定义分隔符将 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* 但我得到

I've tried with charT* but I get

error iso c++ forbids declaration of charT with no type

然后我尝试使用 charostream_iterator(s,&delimiter)但我在字符串中得到了奇怪的字符.

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 到带有自定义分隔符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)