Can you specify what ISN'T a delimiter in std::getline?

混江龙づ霸主 提交于 2019-12-01 07:21:14

问题


I want it to consider anything that isn't an alphabet character to be a delimiter. How can I do this?


回答1:


You can't. The default delimiter is \n:

while (std::getline (std::cin, str) // '\n' is implicit

For other delimiters, pass them:

while (std::getline (std::cin, str, ' ') // splits at a single whitespace

However, the delimiter is of type char, thus you can only use one "split-character", but not what not to match.

If your input already happens to be inside a container like std::string, you can use find_first_not_of or find_last_not_of.


In your other question, are you sure you have considered all answers? One uses istream::operator>>(std::istream&, <string>), which will match a sequence of non-whitespace characters.




回答2:


You don't. getline is a simple tool for a simple job. If you need something more complex, then you need to use a more complex tool, like RegEx's or something.




回答3:


You can't do what you want using std::getline(), but you can roll your own. Here's a getline variant that let's you specify a predicate (function, functor, lambda if it's C++11) to indicate if a character is a delimiter along with a couple overloads that let you pass in a string of delimiter characters (kind of like strtok()):

#include <functional>
#include <iostream>
#include <string>

using namespace std;

template <typename Predicate>
istream& getline_until( istream& is, string& str, Predicate pred)
{
    bool changed = false;
    istream::sentry k(is,true);

    if (bool(k)) {
        streambuf& rdbuf(*is.rdbuf());
        str.erase();

        istream::traits_type::int_type ch = rdbuf.sgetc(); // get next char, but don't move stream position
        for (;;ch = rdbuf.sgetc()) {
            if (istream::traits_type::eof() == ch) {
                is.setstate(ios_base::eofbit);
                break;
            }
            changed = true;
            rdbuf.sbumpc(); // move stream position to consume char
            if (pred(istream::traits_type::to_char_type(ch))) {
                break;
            }
            str.append(1,istream::traits_type::to_char_type(ch));
            if (str.size() == str.max_size()) {
                is.setstate(ios_base::failbit);
                break;
            }
        }

        if (!changed) {
            is.setstate(ios_base::failbit);
        }
    }            

    return is;
}

// a couple of overloads (along with a predicate) that allow you
// to pass in a string that contains a set of delimiter characters

struct in_delim_set : unary_function<char,bool>
{
    in_delim_set( char const* delim_set) : delims(delim_set) {};
    in_delim_set( string const& delim_set) : delims(delim_set) {};

    bool operator()(char ch) {
        return (delims.find(ch) != string::npos);
    };
private:
    string delims;

};

istream& getline_until( istream& is, string& str, char const* delim_set)
{
    return getline_until( is, str, in_delim_set(delim_set));
}

istream& getline_until( istream& is, string& str, string const& delim_set)
{
    return getline_until( is, str, in_delim_set(delim_set));
}

// a simple example predicate functor
struct is_digit : unary_function<char,bool>
{
    public:
        bool operator()(char c) const {
            return ('0' <= c) && (c <= '9');
        }
};


int main(int argc, char* argv[]) {
    string test;

    // treat anything that's not a digit as end-of-line
    while (getline_until( cin, test, not1(is_digit()))) {
        cout << test << endl;
    }

    return 0;
}


来源:https://stackoverflow.com/questions/9272276/can-you-specify-what-isnt-a-delimiter-in-stdgetline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!