我有一个看起来像这样的矢量:
vector<list<entry> > index;
Entry是一个包含字符串键和另一个int的向量的类,它是在读入的文件中找到此键的行。我正在重载我的类的==运算符以比较要输入的键字符串,如果它匹配,我只需将该行插入int向量。如果它不匹配,我将插入密钥和找到它的行。到目前为止,我已经尝试了一切以使其工作!到目前为止,我的运算符重载看起来像这样:
bool index_table::operator==(string key, vector<list<entry> > index) const
{
......code......
}
我似乎无法弄清楚该运营商应该放什么。我是否使用这样的矢量:
index[i[j.key]]
我已经尝试了所有内容并在Stack Overflow上进行了研究,但我似乎还没有找到答案。
编辑:这是班级。 Key只是一个字符串,它可能有多行与之关联。这在条目结构中得到了解决。class index_table
{
private:
struct entry
{
string word;
vector<list <int> > line;
};
vector<list<entry> > index;
vector<string>::iterator it;
public:
index_table();
void insert(string &key, int value);
vector<int> & find(string & key) const;
bool operator==(string, vector<list<entry> >) const;
};
class text_filter
{
public:
text_filter() //sets up the valid chars
{
valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-0123456789";
}
void map_to_valid_chars(string&);
private:
string valid;
};
void text_filter::map_to_valid_chars(string& line) //takes a line and replaces invalid chars with spaces
{
for (int i = 0; i < line.size(); i++)
{
if( valid.find(line[i]) == string::npos)
{
line[i] = ' ';
}
}
}
int main(int argc, char **argv)
{
index_table table;
string word, uWord; //altered word and unaltered word
stringstream ss;
vector<string> unAltWord; // the unaltered words parsed by the stringstream
vector<string> altWord; //altered words modified by map_to_valid_chars member function
text_filter textFilt;
int lineNum = 0;
string searchTerm, line;
vector<string> lines;
ifstream inputFile;
string findLine; //this is for finding a word in the line
if (argv[1])
inputFile.open(argv[1]);
else
{
cout << "usage: ./Indexfile filename" << endl;
exit(1);
}
//parse words and lines
while (getline (inputFile, line))
{
while (ss >> uWord)
{
unAltWord.push_back(uWord);
}
}
altWord = unAltWord;
for (int i = 0; i < altWord.size(); i++)
{
textFilt.map_to_valid_chars(altWord[i]);
}
//insert words and lines into the index table
for (int i = 0; i < lines.size(); i++)
{
findLine = lines[i];
for (int j = 0; j < altWord.size(); j++)
{
if (findLine.find(altWord[j]) != string::npos)
table.insert(altWord[j], line[i]);
}
}
return 0;
}