【问题标题】:Anagram finder program in C++C ++中的字谜查找程序
【发布时间】:2014-06-20 19:45:42
【问题描述】:

我需要编写一个查找单词字谜的程序。当程序启动时,它要求用户将单词输入“字典”,稍后将搜索用户将再次输入的单词的字谜。

我已将字典中的所有单词存储在一个名为 oldDict 的向量中。然后每个单词中的字符按字母顺序排列,新的字符排序单词存储在一个名为newDict的向量中,以保留oldDict中的原始单词。

然后用户输入一个单词,必须在字典中找到该单词。输入单词后,我再次尝试按字母顺序对单词的字符进行排序,以便将其与newDict 中的每个元素进行比较,并以这种方式查找字谜。

下面是我的代码:

ifndef ANAGRAM_H
#define ANAGRAM_H
#include <iostream>
#include <vector>
#include <string>
using namespace std;

vector <string> oldDict;
vector <string> newDict;
int dictSize = 0;

void readDict(void){    // Allows the user to input all the words they would like to     have in the anagram dictionary.
    cout<< "Please enter the number of dictionary entries you wish to create\n";
    cin >> dictSize;
    string word = "";
    cout << "Please enter the dictionary entries, 1 by 1, pushing <enter> after each entry.\n";
    for(int i = 0; i <dictSize; i++){
        cin >> word;
        oldDict.push_back(word);
    }
    newDict = oldDict;
}   

void sortChars(void){   //sorts the letters in each word of the 'dictionary' so that the     letters are in alphabetical order.
    for(int i = 0; i < dictSize; i++){
        std::sort(newDict[i].begin(), newDict[i].end());    
    }
}

void getWords(void){
    int num = 0;
    cout << "Please enter the number of words for which you would like to find anagrams     of:\n";
    cin >> num;
    string word = "";
    for(int i = 0; i < num; i ++){
        cout << "Please enter a word:\n";
        cin>>word;
        std::sort(word.begin(), word.end());    
        for(int i = 0; i < dictSize; i++){
            string word2 = newDict[i];
            bool isAn = isAnagram(word, word2);
            if(isAn == true){
                cout << oldDict[i];
            } else{
            }
        }
    }
}

bool isAnagram(string word1, string word2){

    if(word1.compare(word2) ==0){
        return true;
    } else {
        return false;   
    }
}

void menu(void){
    readDict();
    sortChars();
    getWords();
}
#endif

该过程从代码底部的order() 函数开始。

在尝试编译代码时,我收到以下错误:

In file included from main.cpp:3:0:
./anagram.h: In function ‘void sortChars()’:
./anagram.h:25:3: error: ‘sort’ is not a member of ‘std’
   std::sort(newDict[i].begin(), newDict[i].end()); 
   ^
./anagram.h: In function ‘void getWords()’:
./anagram.h:37:4: error: ‘sort’ is not a member of ‘std’
    std::sort(word.begin(), word.end()); 
    ^
./anagram.h:40:38: error: ‘isAnagram’ was not declared in this scope
     bool isAn = isAnagram(word, word2);

有人可以帮我解决这些错误吗?我真的不明白 'isAnagram' 给出错误时,如果有人可以解释一下 'std::' 的作用以及为什么这两行代码会产生错误?

非常感谢

【问题讨论】:

  • #include &lt;algorithm&gt; 和使用前声明的函数
  • 基于错字的问题:不太可能对其他人有用。

标签: c++ dictionary finder anagram


【解决方案1】:

‘sort’ is not a member of ‘std’#include &lt;algorithm&gt;

‘isAnagram’ was not declared in this scope在第一次使用之前声明函数。

此外,isAnagram 的实现看起来不正确。你不能简单地比较字符串。您应该在比较它们之前对字符串进行排序。

bool isAnagram(string word1, string word2){
    std::sort(word1.begin(), word1.end()); // added
    std::sort(word2.begin(), word2.end()); // added
    if(word1.compare(word2) ==0){
        return true;
    } else {
        return false;   
    }
}

【讨论】:

  • 感谢您的回复。当您说“在首次使用之前声明该功能”时,我无法理解您的意思?我如何声明一个布尔变量“isAn”并立即为其分配一个布尔值,这将由 isAnagram 函数返回,这是否不正确?
  • 哦,没关系,我一直忘记函数的顺序在 C++ 中很重要。非常感谢您的帮助:)
  • 如果您想了解declaration vs definition in C and C++,我建议您使用谷歌搜索突出显示的术语并浏览一些链接。
猜你喜欢
  • 2012-07-22
  • 2013-09-20
  • 2012-11-03
  • 2010-10-28
  • 2022-01-21
  • 2019-07-07
  • 2013-07-05
  • 2016-03-18
  • 2011-12-07
相关资源
最近更新 更多