简体   繁体   English

按空格分割一串正/负整数

[英]Split a string of positive/negative integers by space

I have the following string: 我有以下字符串:

1 -2 -8 4 51

I would like to get a vector with 5 elements, each of them corresponding to the 5 numbers in the string. 我想得到一个包含5个元素的向量,每个元素对应于字符串中的5个数字。 Basically, I'd like to split the above string by space as a delimiter. 基本上,我想用空格将上述字符串分隔为定界符。

I've found a lot of questions like this on Stack Overflow but so far any of them is able to get neither the first "1" nor the last one (from 51 ). 我在Stack Overflow上发现了很多类似的问题,但是到目前为止,它们中的任何一个都无法获得第一个“ 1”或最后一个(来自51 )。 My code is the following: 我的代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

std::vector<std::string> split(std::string str)
{
    std::string buf; 
    std::stringstream ss(str); 
    vector<std::string> tokens; 

    while (ss >> buf)
        tokens.push_back(buf);
    return tokens;
}

int main()
{

    std::string temps = "1 -2 -8 4 51";

    std::vector<std::string> x = split(temps);
    for (int j = 0; j < x.size(); j++){
        cout << x[j] << endl;    
    }
}

My output is the following: 我的输出如下:

-2
-8
4
5

As you can see, the first and the last 1 are skipped. 如您所见,第一个和最后一个1被跳过。 I'm very new to C++ (I've been maybe too much used to the built-in functions .split() of other languages) but I can't see anything wrong on my above code. 我对C ++还是很陌生(我可能已经习惯了其他语言的内置函数.split() ,但是我在上面的代码中看不到任何错误。 Can anyone please help me understanding what I'm doing wrong? 谁能帮我了解我在做什么错?

The code you showed works fine in general. 您显示的代码通常可以正常工作。 The output is as expected, so the problem has to be outside of the code you showed. 输出符合预期,因此问题必须出在显示的代码之外。

However, note that your vector will contain std::string values, not int values. 但是,请注意, vector将包含std::string值,而不是int值。 Also, you should consider using std::istringstream for input instead of using std::stringstream . 另外,您应该考虑使用std::istringstream作为输入,而不是使用std::stringstream

If you want a vector of integers, try this instead: 如果您想要一个整数向量,请尝试以下方法:

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

std::vector<int> split(const std::string &str)
{
    int num; 
    std::istringstream iss(str); 
    std::vector<int> tokens; 

    while (iss >> num)
        tokens.push_back(num);

    return tokens;
}

int main()
{
    std::string temps = "1 -2 -8 4 51";

    std::vector<int> x = split(temps);
    for (int j = 0; j < x.size(); j++) {
        std::cout << x[j] << std::endl;
    }

    return 0;
}

You could then make split() use std::istream_iterator and std::back_inserter instead of a manual loop, eg: 然后,您可以使split()使用std::istream_iteratorstd::back_inserter代替手动循环,例如:

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
#include <algorithm>

std::vector<int> split(const std::string &str)
{
    std::istringstream iss(str); 
    std::vector<int> tokens; 

    std::copy(
        std::istream_iterator<int>(iss),
        std::istream_iterator<int>(),
        std::back_inserter(tokens)
    );

    return tokens;
}

int main()
{
    std::string temps = "1 -2 -8 4 51";

    std::vector<int> x = split(temps);
    for (int j = 0; j < x.size(); j++) {
        std::cout << x[j] << std::endl;
    }

    return 0;
}

And then you could make `split() be a template function so it can return a vector of different types depending on what you want from the input string, eg: 然后您可以将`split()作为模板函数,以便它可以根据输入字符串的需要返回不同类型的向量,例如:

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
#include <algorithm>

template<typename T>
std::vector<T> split(const std::string &str)
{
    std::istringstream iss(str); 
    std::vector<T> tokens; 

    std::copy(
        std::istream_iterator<T>(iss),
        std::istream_iterator<T>(),
        std::back_inserter(tokens)
    );

    return tokens;
}

int main()
{
    std::string temps = "1 -2 -8 4 51";

    std::vector<int> x = split<int>(temps);
    for (int j = 0; j < x.size(); j++) {
        std::cout << x[j] << std::endl;
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM