字数统计函数在将单词添加到一组唯一单词时遇到问题

Wordcount function having trouble adding words to a set of unique words

本文关键字:一组 唯一 问题 遇到 单词时 函数 统计 添加 单词      更新时间:2023-10-16

我正在编写一个字数统计函数,它应该能够将元素从 stdin 读取到字符串中。然后计算字符串并返回单词数、行数、字符串大小和唯一单词数。

我的问题是当涉及到向唯一集合添加单词时。当我编写它以将元素添加到集合时,它会将空格计为单词的一部分,然后完全推送到我的集合中。 例: 输入:

this is                                                                                                                                                                                                                                         
is                                                                                                                                                                                                                                      
a test                                                                                                                                                                                                                                          
test 

输出

a                                                                                                                                                                                                                                               
test                                                                                                                                                                                                                                            
is test this                                                                                                                                                                                                                                    
line is 4                                                                                                                                                                                                                                       
Words = 7                                                                                                                                                                                                                                       
size is 27                                                                                                                                                                                                                                      
Unique is 6 

它总共有 7 个单词和 6 个唯一单词。我尝试通过在运行时打印代码片段来调试它,以便我可以跟踪我出错的地方。我只能得出结论,问题出在我的 if 循环中。我怎么能过去,我已经卡了一段时间了。

这是我的代码:

#include<iostream>
#include<string>
#include<set>
using std::string;
using std::set;
using std::cin;
using std::cout;
set<string> UNIQUE;
size_t sfind(const string s) //will take string a count words, add to set
{
string a;
int linecount = 0;
int state = 0;               //0 represents reading whitespace/tab, 1 = reading letter  
int count = 0;              //word count
for(size_t i =0; i < s.length(); i++) {
a+=s[i];                                          //add to new string to add to set
if(state ==0) {                                  //start at whitespace       
if(state != ' ' && state != 't') {         //we didnt read whitespace
count++;
state =1;
}
}
else if(s[i]== ' ' || s[i] == 't' || s[i] == 'n') {
state = 0;
UNIQUE.insert(a);                   //add to UNIQUE words
a.clear();                         // clear and reset the string
}
if (s[i] == 'n') {
linecount++;
}
}
for(set<string>::iterator i = UNIQUE.begin(); i!= UNIQUE.end(); i++) {  
cout << *i;
}
cout << 'n';
cout << "line is " << linecount << 'n';
return count;
}
int main()
{
char c;
string s; 
while(fread(&c,1,1,stdin)) {
s+=c;   //read element add to string
}
cout << "Words = " << sfind(s) << 'n';
cout << "size is " << s.length() << 'n';
cout << "Unique is "<< UNIQUE.size() << 'n';  
return 0;
}

我也将使用

fread(&c,1,1,stdin)

因为我稍后会用更大的字数统计函数使用它。

与其编写代码试图在空格上解析字符串,不如使用 std::istringstream 进行解析。

下面是一个示例:

#include <string>
#include <iostream>
#include <sstream>
#include <set>
int main()
{
std::set<std::string> stringSet;
std::string line;
while (std::getline(std::cin, line))
{
std::istringstream oneline(line);
std::string word;
while (oneline >> word)
{
std::cout << word << "n";
stringSet.insert(word);
}
}
std::cout << "nnThere are " << stringSet.size() << " unique words";
}

现场示例