如何使我的程序接受多个单词
How to make it so that my program accepts multiple words
我正在制作一个列表向量的程序。它跟踪一个单词,以及该单词在哪个行号上找到。
例:
teddy bears are cute
so are you
因此,它将泰迪熊存储为第 1 行,熊存储为第 1 行。我遇到的唯一问题是当一个单词被重复时。它将存储为第 1 行,但我希望程序也存储为第 2 行。我不确定我该怎么做。这是我到目前为止的代码
class index_table
{
public:
index_table() { table.resize(128);}
vector <int> &find1(string &);
private:
class entry
{
public:
string word;
vector <int> line;
};
vector< list <entry> > table;
};
void index_table :: insert( string & key, int value)
{
entry obj;
int c = key[0]; //vector for the table.
obj.word = key; //Storing word
obj.line.push_back(value); //Storing what line it was found on
table[c].push_back(obj); //Stores the word and line number.
}
如何使我的程序可以在不同的数字行上存储多个单词?我将不得不在我的表格[c]中搜索一个单词是否相同?我怎样才能正确地做到这一点?
这不是您问题的解决方案,我正在回答您的评论
"我以前从未使用过地图,所以我不完全确定如何实现它......"
#include<iostream>
#include<fstream>
#include<sstream>
#include<map>
#include<set>
int main()
{
std::map< std::string, std::set<int> > word_count;
std::ifstream input_file("input.txt");
std::string single_line, single_word;
int line_number = 0;
while(std::getline(input_file, single_line))
{
++line_number;
std::stringstream word_reader(single_line);
while(word_reader >> single_word)
{
word_count[single_word].insert(line_number);
}
}
input_file.close();
for(auto word:word_count)
{
std::cout << word.first << ":";
for(auto line:word.second)
{
std::cout << line << " ";
}
std::cout << std::endl;
}
}
Input.txt
的内容:
teddy bears are cute
so are you
输出:
are:1 2
bears:1
cute:1
so:2
teddy:1
you:2
相关文章:
- 文本文件中的单词链表
- 如何在 C 中正确使用 libiconv 使其不会报告"Arg list too long"?
- 在指针的帮助下,文本文件中单词的频率
- std::vector的包装器,使数组的结构看起来像结构的数组
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何在全屏模式下(在OpenGL中)使背景透明
- 找到两对数字,使它们的乘积的绝对差最小化
- C++:如何使函数只返回作为列表一部分的字符串
- 如何根据单词在文本中出现的概率输出单词
- 如何使数组以单词而不是数字的形式出现
- 如何使我的单词查找更清晰、更高效
- 如何使Palindrome代码不必担心(用户input)单词间距
- C++文字冒险游戏 - 使单词大写
- 有效地使用具有数组的KMP来计数单词频率计数
- 使用线程在以元音或辅音开头的单词之间交替
- 使用线程根据起始字符打印出单词
- Check Writer:使程序将值10-19显示为单词
- 通过修改二叉搜索算法来改进它,使其在搜索大量单词(单词列表)中的单词时工作得更快
- 如何使我的程序接受多个单词
- C++11编译器何时会使RVO和NRVO优于移动语义和常量引用绑定