игнорировать пунктуацию с помощью манипулятора - PullRequest
0 голосов
/ 14 октября 2010

Можно ли игнорировать пунктуацию, используя стандартный манипулятор на cin?Например, предположим, что у вас есть входной поток (в данном случае файл), например: «один, два, три».Я хочу иметь возможность:

f >> ignore_punct >> a;
f >> ignore_punct >> b;
f >> ignore_punct >> c;

в конце a=="one", b=="two", c=="three".

Ответы [ 2 ]

1 голос
/ 14 октября 2010

Попробуйте это:

Он использует локальный для фильтрации знаков препинания.
Это позволяет оставшемуся коду остаться неизменным.

#include <locale>
#include <string>
#include <iostream>
#include <fstream>
#include <cctype>

class PunctRemove: public std::codecvt<char,char,std::char_traits<char>::state_type>
{
    bool do_always_noconv() const throw()  { return false;}
    int do_encoding()       const throw()  { return true; }

    typedef std::codecvt<char,char,std::char_traits<char>::state_type> MyType;
    typedef MyType::state_type          state_type;
    typedef MyType::result              result;


    virtual result  do_in(state_type& s,
            const char* from,const char* from_end,const char*& from_next,
                  char* to,        char* to_limit,      char*& to_next  ) const
    {
        /*
         * This function is used to filter the input
         */
        for(from_next = from, to_next = to;from_next != from_end;++from_next)
        {
            if (!std::ispunct(*from_next))
            {
                *to_next = *from_from;
                ++to_next;
            }
        }
        return ok;
    }

    /*
     * This function is used to filter the output
     */
    virtual result do_out(state_type& state,
            const char* from, const char* from_end, const char*& from_next,
                  char* to,         char* to_limit,       char*& to_next  ) const
    { /* I think you can guess this */ }
};


int main()
{
    // stream must be imbued before they are opened.
    // Otherwise the imbing is ignored.
    //
    std::ifstream   data;
    data.imbue(std::locale(std::locale(), new PunctRemove));
    data.open("plop");
    if (!data)
    {
        std::cout << "Failed to open plop\n";
        return 1;
    }

    std::string         line;
    std::getline(data, line);
    std::cout << "L(" << line << ")\n";
}
1 голос
/ 14 октября 2010

Нет стандартного способа сделать это, но это довольно легко сделать, если я вас правильно понимаю. Если вы хотите прочитать строку с до с некоторыми пунктуациями, как если бы это была новая строка, вы можете использовать версию getline, которая принимает предикат вместо одного разделителя:

template<class F>
std::istream& getline(std::istream& stream, std::string& string, F delim) {

    string.clear();

    // Get characters while the stream is valid and the next character is not the terminating delimiter.
    while (stream && !delim(stream.peek()))
        string += stream.get();

    // Discard delimiter.
    stream.ignore(1);

    return stream;

};

Пример использования:

#include <iostream>
#include <cctype>

int main(int argc, char** argv) {

    std::string s;
    getline(std::cin, s, ::ispunct);

    std::cout << s << '\n';
    return 0;

}

Если вы также хотите разбить на новые строки, вы можете написать функтор:

struct punct_or_newline {
    bool operator()(char c) const { return ::ispunct(c) || c == '\n'; }
};

И вместо этого вызывать как getline(std::cin, my_string, punct_or_newline()). Надеюсь, это поможет!

...