开发者

Splitting a string by whitespace in c++ [duplicate]

This question already has answers here: 开发者_JS百科 Closed 12 years ago.

Possible Duplicates:

C++: How to split a string?

Splitting a string

What is the best way to go about splitting a string up by whitespace in 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++, not using Microsoft Visual C++


It may be open to question whether it's best, but one really easy way 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 vector directly from the iterators:

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:

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜