C++中的Anagram finder程序
Anagram finder program in C++
我需要编写一个程序来查找单词的变位符号。当程序启动时,它要求用户将单词输入到"词典"中,稍后将在该词典中搜索用户将再次输入的单词的变位符号。
我把字典中的所有单词都存储在一个名为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 createn";
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::"是什么,为什么这两行代码会产生错误?
非常感谢
‘sort’ is not a member of ‘std’
添加#include <algorithm>
‘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;
}
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- Xcode 5控制台应用程序只有在通过finder启动时才会出现分段故障
- C++中的Anagram finder程序