C++ C++;I';我无法用正确的值填充此BST
我已经创建了一个充满WordInfo对象的BST,它有一个向量来指出哪些WordInfo对象是同义词或反义词。每个单词都由其源文件dictionary.txt上的整数标识。BST到目前为止已经收到了它的单词列表,但是我很难填写同义词。坦率地说,我很困惑如何让我的对象以我希望的方式交互 以下是我认为问题的核心:C++ C++;I';我无法用正确的值填充此BST,c++,token,C++,Token,我已经创建了一个充满WordInfo对象的BST,它有一个向量来指出哪些WordInfo对象是同义词或反义词。每个单词都由其源文件dictionary.txt上的整数标识。BST到目前为止已经收到了它的单词列表,但是我很难填写同义词。坦率地说,我很困惑如何让我的对象以我希望的方式交互 以下是我认为问题的核心: //--function for getting synonyms in a vector void pushSynonyms(string synline, BST <W
//--function for getting synonyms in a vector
void pushSynonyms(string synline, BST <WordInfo> wordTree)
{
int lineSize = synline.size();
const char *aux;
aux=synline.data();
int index=0;
int searchedOne= aux[0];
//wanting to find an element in the tree with this ID
//lacking: search function
while (index<=lineSize){
mySynonyms.push_back (aux[index]);
index++;
}
}
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include "MiBST.h"
using namespace std;
class WordInfo {
public:
WordInfo() {
// nothing to define?
}
~WordInfo() {
//nothing to define?
}
//--id accesor
int id () const {return myId;}
//--input function for filling Words
void readWords (istream &in)
{
in>>myId>>word;
}
//--function for getting synonyms in a vector
void pushSynonyms(string synline, BST <WordInfo> wordTree)
{
int lineSize = synline.size();
const char *aux;
aux=synline.data();
int index=0;
int searchedOne= aux[0];
//lacking: define search function
while (index<=lineSize){
mySynonyms.push_back (aux[index]);
index++;
}
}
//--function for getting antonyms in a vector
void pushAntonyms(string synline, BST <WordInfo> wordTree )
{
int lineSize = synline.size();
const char *aux;
aux=synline.data();
int index=0;
// now I need fo find the right words to pair up
while (index<=lineSize){
myAntonyms.push_back (aux[index]);
index++;
}
}
//--output function
void printWords (ostream &out)
{
out<<myId<<" "<<word;
}
//--equals operator
bool operator == (const WordInfo &otherWordInfo) const
{ return myId == otherWordInfo.myId;}
//--equals operator for String
bool operator == (const string & aString) const
{return word ==aString;}
//--less than operator
bool operator < (const WordInfo & otherWordInfo)const
{return myId<otherWordInfo.myId;}
//--more than operator
bool operator > (const WordInfo &otherWordInfo) const
{ return myId > otherWordInfo.myId;}
private:
vector<int> mySynonyms;
vector <int> myAntonyms;
string word;
int myId;
};
//--- Definition of input operator
istream & operator>>(istream & in, WordInfo & word)
{
word.readWords(in);
//I want to call word.readSyns(in) too, how?
}
//---Definition of output operator
ostream & operator <<(ostream &out, WordInfo &word)
{
word.printWords(out);
}
int main() {
//search each word by id and
// define its synonyms
string wordFile;
cout<< "enter name of dictionary file: ";
getline (cin,wordFile);
ifstream inStream(wordFile.data());
if(!inStream.is_open())
{
cerr<<"cannot open "<<wordFile<<"\n";
exit(1);
}
//build the bst of word records
BST <WordInfo> wordTree; //BST of word records
WordInfo aword; // a word record
//--loop that fills tree with words
while((inStream>> aword && (!(aword=="synonyms"))))
{
wordTree.insert(aword);
}
string line;
//--loop that takes synonyms
while((inStream>>line)&& (line!="antonyms")){
aword.pushSynonyms(line, wordTree);
}
//--loop that takes antonyms
while(inStream >> line) {
if (inStream.eof())break;
}
wordTree.graph(cout);
system("PAUSE");
return 0;
}
撇开其他人的评论不谈--我承认这个示例中的代码比我愿意看到的要多--我认为您面临的基本问题是您现有的搜索函数按对象搜索,而您需要一个按id进行搜索的函数。创建一个新的搜索函数,将id作为参数,并在树中迭代,比较当前WordInfo对象的id与传入的id。找到具有匹配id的id时,返回该id(而不是返回是否找到该id的true/false)。如果找不到具有匹配id的对象,请返回null。您需要找到一种将id(int)与WordInfo对象进行比较的方法。我的C++有点生疏,所以语法可能有点偏离。
//--- Definition of find()
template <typename DataType>
DataType& BST<DataType>::find(const int id ) const
{
typename BST<DataType>::BinNodePointer locptr = myRoot;
typename BST<DataType>::BinNodePointer parent =0;
bool found = false;
while (!found && locptr != 0)
{
if (locptr->data > id) // descend left
locptr = locptr->left;
else if (locptr->data < id) // descend right
locptr = locptr->right;
else // item found
found = true;
}
return found ? locptr->data : null;
}
/--find()的定义
模板
数据类型&BST::find(const int id)const
{
typename BST::binnodepenterlocptr=myRoot;
typename BST::BinNodeInter父级=0;
bool-found=false;
而(!found&&locptr!=0)
{
if(locptr->data>id)//向左下降
locptr=locptr->左;
否则如果(locptr->data右侧;
else//找到项目
发现=真;
}
返回找到的?locptr->数据:空;
}
注意:这需要实现
操作符>(const int id)
和操作符库,我发现这个答案非常具体和令人满意。尽管如此,我仍然担心操作符>(const int id)的实现是否有任何用处:模板bool BST::operator>(const int anotherId)const{typename BST::BinNodePointer locptr;return(locptr->data>anotherId);},因为我还没有为locptr分配任何东西..编译器会对此进行标记。
1 cute
2 hello
3 ugly
4 easy
5 difficult
6 tired
7 beautiful
synonyms
1 7
7 1
antonyms
1 3
3 1 7
4 5
5 4
7 3
//--- Definition of find()
template <typename DataType>
DataType& BST<DataType>::find(const int id ) const
{
typename BST<DataType>::BinNodePointer locptr = myRoot;
typename BST<DataType>::BinNodePointer parent =0;
bool found = false;
while (!found && locptr != 0)
{
if (locptr->data > id) // descend left
locptr = locptr->left;
else if (locptr->data < id) // descend right
locptr = locptr->right;
else // item found
found = true;
}
return found ? locptr->data : null;
}