c++:字符串流到向量

发布于 2024-10-20 21:28:34 字数 557 浏览 5 评论 0原文

我正在尝试将字符串流中的数据存储到向量中。我可以成功地这样做,但它会忽略字符串中的空格。如何才能将空格也推入向量中?

谢谢!

代码存根:

#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
    stringstream s;
    string line  = "HELLO HELLO\0";

    stringstream stream(line);

    unsigned char temp;
    vector<unsigned char> vec;
    while(stream >> temp) 
        vec.push_back(temp);


    for (int i = 0; i < vec.size(); i++)
         cout << vec[i];
    cout << endl;
    return 0;
}

I'm trying to store the data that is in a stringstream into a vector. I can succesfully do so but it ignores the spaces in the string. How do I make it so the spaces are also pushed into the vector?

Thanks!

Code stub:

#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
    stringstream s;
    string line  = "HELLO HELLO\0";

    stringstream stream(line);

    unsigned char temp;
    vector<unsigned char> vec;
    while(stream >> temp) 
        vec.push_back(temp);


    for (int i = 0; i < vec.size(); i++)
         cout << vec[i];
    cout << endl;
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-10-27 21:28:34

为什么使用stringstreamstring复制到vector?你可以这样做:

vector<unsigned char> vec(line.begin(), line.end());

这将直接进行复制。如果需要使用stringstream,则需要使用stream >>首先是 noskipws。

Why are you using a stringstream to copy from a string into a vector<unsigned char>? You can just do:

vector<unsigned char> vec(line.begin(), line.end());

and that will do the copy directly. If you need to use a stringstream, you need to use stream >> noskipws first.

冬天旳寂寞 2024-10-27 21:28:34

默认情况下,标准流都跳过空格。如果您希望禁用跳过空白,您可以使用 noskipws 操纵器在下一个流提取 (>>) 操作中明确执行此操作,如下所示:

stream >> std::noskipws >> var;

By default, the standard streams all skip whitespace. If you wish to disable the skipping of white space you can explicitly do so on the next stream extraction (>>) operation by using the noskipws manipulator like so:

stream >> std::noskipws >> var;
自在安然 2024-10-27 21:28:34

我倾向于建议只使用您真正想要的容器,但您也可以使用操纵器 noskipws

请参阅 this 有关 stringstream 的更多信息以及除提取运算符之外还可以使用的其他方法

编辑:

另请考虑 std::copystd::为了简单起见, basic_string

I'm inclined to suggest just using the container that you actually want but you could also use the manipulator noskipws

See this for more info on stringstream and other methods you could use besides the extraction operator

Edit:

Also consider std::copy or std::basic_string<unsigned char> for simplicity.

流心雨 2024-10-27 21:28:34

您需要 noskipws 操纵器。说 stream >> std::noskipws; 在从中提取内容之前。

[已编辑添加 std:: 前缀,我愚蠢地省略了它。您的代码不需要它,因为它的顶部有 using namespace std; ,但其他人可能选择不这样做。]

You want the noskipws manipulator. Say stream >> std::noskipws; before pulling stuff out of it.

[EDITED to add the std:: prefix, which I stupidly omitted. Your code doesn't need it because it has using namespace std; at the top, but others may choose not to do that.]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文