C++ 对于每个字输出一个对列表,该行的行号和字数
我试图为stdin中的每个单词编写一个程序,输出一个成对的列表,格式为C++ 对于每个字输出一个对列表,该行的行号和字数,c++,string,dictionary,stdin,std-pair,C++,String,Dictionary,Stdin,Std Pair,我试图为stdin中的每个单词编写一个程序,输出一个成对的列表,格式为L:N,其中L是行号,N是给定单词的出现次数 因此,如果stdin是: hello world hello hello 输出应该是 hello 1:1 2:2 world 1:1 在下面的代码中 #include <iostream> #include <map> #include <string> #include <iterator> using std::cin; u
L:N
,其中L
是行号,N
是给定单词的出现次数
因此,如果stdin是:
hello world
hello hello
输出应该是
hello 1:1 2:2
world 1:1
在下面的代码中
#include <iostream>
#include <map>
#include <string>
#include <iterator>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::map;
using std::pair;
int main(int argc, const char *argv[])
{
map<string, pair<unsigned int, unsigned int>> table;
string word;
while (cin >> word) {
++table[word];
}
for (std::map<string, pair<unsigned int, unsigned int>>::iterator itr = table.begin(); itr != table.end();
++itr) {
cout << itr->first << "\t => \t" << itr->second << itr->third << endl;
}
while (cin >> word) {
++table[word];
}
}
#包括
#包括
#包括
#包括
使用std::cin;
使用std::cout;
使用std::endl;
使用std::string;
使用std::map;
使用std::pair;
int main(int argc,const char*argv[]
{
地图表格;
字符串字;
while(cin>>word){
++表[字];
}
for(std::map::iterator itr=table.begin();itr!=table.end();
++itr){
cout(第一个字){
++表[字];
}
}
我正在尝试制作一个使用三个元素的映射,并且有一个迭代器,可以遍历映射、计算行数并使用
getline()
获取每行单词的出现次数。此代码仅输出单词总数。为了帮助您开始,我建议您使用带有行号和出现次数结构的地图:
struct Word_Attributes
{
unsigned int line_number;
unsigned int occurances_on_line;
};
typedef std::vector<Word_Attributes> Attribute_Container;
typedef std::map<string, Attribute_Container> Dictionary;
int main(void)
{
Dictionary my_words;
std::string text_line;
unsigned int line_number = 1;
// Read in text lines until EOF or failure.
while (getline(cin, text_line)
{
// Extract words from the text line.
std::string word;
std::istringstream text_stream(text_line);
while (text_stream >> word)
{
// A word is extracted.
// See if it is in the dictionary.
Dictionary::iterator iter;
iter = my_words.find(word);
// If the word is in the dictionary, check for attributes.
if (iter != my_words.end())
{
// use iter to get the property list.
// Check the property list for the line number.
// If line number exists, increment the occurrances.
// Otherwise, create a new attributes structure and
// append to the property list.
}
else
{
// The word is not in the dictionary,
// create an initial attributes structure for the word.
Word_Attributes attributes;
attributes.line_number = line_number;
attributes.occurances_on_line = 1;
Attribute_Container property_list;
property_list.push_back(attributes);
my_words[word] = property_list;
}
}
}
return EXIT_SUCCESS;
}
你应该看到:
hello ---> Line 1, occurrences: 1
| +--> Line 2, occurrences: 2
V
world ---> Line 1, occurrences: 1
完成上面的模具是读者的一个练习。我将使用此容器来表示数据:
// word line count
std::map<std::string, std::map<size_t, size_t>> wordCounts;
没有比这更简单的了。如果单词或行号不在结构中,它会添加它,如果是,它会使用已经存在的内容
填写时会出现如下情况:
std::string line;
for(size_t lineNumber = 1; std::getline(std::cin, line); ++lineNumber)
{
std::istringstream ss{line}
for(std::string word; ss >> word;)
++wordCounts[word][lineNumber];
}
当您使用调试器时,是哪一行导致了问题?当您使用word时,您如何知道何时到达该行的末尾?您是否尝试过getline
?我尝试过getline
,但它编译成一团,我不知道如何正确实现它
++wordCounts[word][lineNumber];
std::string line;
for(size_t lineNumber = 1; std::getline(std::cin, line); ++lineNumber)
{
std::istringstream ss{line}
for(std::string word; ss >> word;)
++wordCounts[word][lineNumber];
}