C++ 用C++中的空格分割字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2275135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:49:55  来源:igfitidea点击:

Splitting a string by whitespace in c++

c++string

提问by Brian

Possible Duplicates:
C++: How to split a string?
Splitting a string

可能的重复项:
C++:如何拆分字符串?
拆分字符串

What is the best way to go about splitting a string up by whitespace in c++?

在 C++ 中用空格拆分字符串的最佳方法是什么?

I'd like to be able to split it based on tab, space, etc. and of course ignore multiple tabs/spaces/etc. in a row as well as not have issues with having those things at the end.

我希望能够根据制表符、空格等进行拆分,当然可以忽略多个制表符/空格/等。连续,并且在最后拥有这些东西时没有问题。

Ultimately, I am going to end up storing this in a vector, but I can easily convert between data types if there is some easy built-in standard library way of splitting.

最终,我最终将把它存储在一个向量中,但如果有一些简单的内置标准库分割方式,我可以轻松地在数据类型之间进行转换。

I am building this on a UNIX machine with g++, notusing Microsoft Visual C++

我正在使用 g++ 的 UNIX 机器上构建它,而不是使用 Microsoft Visual C++

回答by Jerry Coffin

It may be open to question whether it's best, but one really easyway to do this is to put your string into a stringstream, then read the data back out:

是否最好可能还有待商榷,但一种非常简单的方法是将您的字符串放入字符串流中,然后将数据读回:

// warning: untested code.
std::vector<std::string> split(std::string const &input) { 
    std::istringstream buffer(input);
    std::vector<std::string> ret;

    std::copy(std::istream_iterator<std::string>(buffer), 
              std::istream_iterator<std::string>(),
              std::back_inserter(ret));
    return ret;
}

If you prefer, you can initialize the vectordirectly from the iterators:

如果您愿意,可以vector直接从迭代器初始化:

std::vector<std::string> split(std::string const &input) { 
    std::istringstream buffer(input);
    std::vector<std::string> ret((std::istream_iterator<std::string>(buffer)), 
                                 std::istream_iterator<std::string>());
    return ret;
}

Either should work with any reasonable C++ compiler. With C++11, you can clean up the second version a little bit by using brace-initialization instead:

两者都应该与任何合理的 C++ 编译器一起使用。使用 C++11,您可以通过使用大括号初始化来稍微清理第二个版本:

    std::vector<std::string> ret{std::istream_iterator<std::string>(buffer), 
                                 std::istream_iterator<std::string>()};

回答by Brian

This is what I use:

这是我使用的:

/* Tokenizing a string */
    std::vector<std::string> Parser::tokenizer( const std::string& p_pcstStr, char delim )  {
        std::vector<std::string> tokens;
        std::stringstream   mySstream( p_pcstStr );
        std::string         temp;

        while( getline( mySstream, temp, delim ) ) {
            tokens.push_back( temp );
        }

        return tokens;
    } 

Your delim would be a whitespace, p_pcstStr would be the string to tokenize and the return would be a vector with all strings which have a whitespace in between.

您的 delim 将是一个空格, p_pcstStr 将是要标记的字符串,返回将是一个向量,其中所有字符串之间都有一个空格。