仅读取字母文件

时间:2019-05-29 14:36:54

标签: c++ alphabetical read-write

我只能完成我要完成的任务的一小部分,并且必须修剪每个单词,以便当我从.txt文件中读取单词时,每个单词仅包含字母。这是AVL树的一部分,是的,但是我认为问题在于阅读文本文档。

我尝试过ise isalpha,但无法使其正常工作,并且用尽了所有想法,陷入了这种情况。我将不胜感激!

           cout << "Input a file name (dictionary.txt):" << endl;
            cin >> file;
            myfile.open(file);
            if (!myfile) {
                cout << "\nFile does not exist." << endl;
                return 0;
            }
            else cout << "\nDictionary has been loaded." << endl;
            while(!myfile.eof()) {
                myfile >> insert;
                DATA newItem;
                newItem.key = insert;
                tree.AVL_Insert(newItem);
                count++;
            }

1 个答案:

答案 0 :(得分:0)

算法库使此操作变得简单:

#include <iostream>
#include <string>
#include <algorithm>

int main ()
{
    std::string s = "A1B2C3";
    s.erase (std::remove_if (s.begin (), s.end (), [] (auto c) { return !std::isalpha ((unsigned char) c); }), s.end ());
    std::cout << s;
}

输出:

ABC

Live demo

另请参阅erase remove idiom

相关问题