0

これが要件です。新しい単語が検出されるたびに、プログラムは動的メモリからノードのインスタンスを割り当てて、単語とそのカウントを格納し、それをリンク リストに挿入して、リストが常にソートされるようにする必要があります。見つかった単語がリストに既に存在する場合、その単語のカウントをインクリメントする必要があります。

私はどこでも検索しましたが、適切な解決策は std::map を使用することですが、これまで学習していないため、使用したくありません。List や Vector を使って構造体やクラスを作って各ノードを操作してもよろしいでしょうか?

これは私の適切なコードです

class Node {
string word;
int count;

public:
    Node() {
        word = "";
        count = 1;
    }
    Node(const Node &other) : word(other.word), count(other.count) {
        // copy constructor 
    }
    ~Node() {} // Destructor

    void printWord() const {
        cout << count << " " << word << endl;
    }
    void loadWord(ifstream &fin) { 
        fin >> word;
    }
    void setWord(const string &word) {
        this->word = word;
    }
    const string& getWord() const {
        return word;
    }
    void incrementCount() {
        count++;
    }
};

void load(list<Node> &nodes, const char *file);
void print(const list<Node> &nodes);
bool isExist(const list<Node> &nodes, const string &word, Node &node);
void error(const string &message, const char *file);
const Node& getNode(const list<Node> &nodes, const string &word);

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

    list<Node> nodes;

    if (argc != 2) {
        cout << "Error syntax : require an input file\n";
        return 0;
    }
    load(nodes, argv[1]);
    print(nodes);

    return 0;   
}

void print(const list<Node> &nodes) {

    list<Node>::const_iterator itr;

    for (itr = nodes.begin(); itr != nodes.end(); itr++) {
        itr->printWord();
    }
cout << '\n';
}

void load(list<Node> &nodes, const char *file) { 

    ifstream fin;
    Node node;
    string temp;

fin.open(file);

if (!fin) 
    error("Cannot open file ", file); // exit

while (!fin.eof()) {
    if (fin.good()) {
        fin >> temp;
        if (!isExist(nodes, temp, node)) {
            node.setWord(temp);
            nodes.push_back(node);
        } else {
            // increase word count here
        }

    } else if (!fin.eof()) 
        error("Unable to read data from ", file);
}
fin.close();
}

bool isExist(const list<Node> &nodes, const string &word, Node &node) {
list<Node>::const_iterator itr;
for (itr = nodes.begin(); itr != nodes.end(); itr++) {
    if(word.compare(itr->getWord()) == 0) {
        return true;
    }
}
return false;
}

const Node& getNode(const list<Node> &nodes, const string &word) {
    list<Node>::const_iterator itr;
    for (itr = nodes.begin(); itr != nodes.end(); itr++) {
        if(word.compare(itr->getWord()) == 0) {
            return *itr;
        }
    }
    return NULL; // This is fail what should I do to return a NULL value when not found
}

void error(const string &message, const char *file) {
cerr << message << file << '\n';
exit(0);
}

コードが機能しません。Java の知識を適用して質問を解決するためのソリューションを生成しようとしましたが、C++ でオブジェクトを制御するのは異なるようです。誰かが私のコードを調べて、より良い方法を提案してくれませんか?

ありがとう。

4

1 に答える 1

0

いいえ、これはベクトルとリストを練習するのに最適なタスクではありません。std::map ドキュメントを見て、効率的で見栄えの良いコードを 3 行書く必要があります。TreeMap Java の知識を適用しなかったのはなぜですか?

于 2012-04-22T06:54:17.267 に答える