C++ 使用set/multiset查找以“s”开头的单词
这段代码的问题源于get_words_start_函数的最后一段代码C++ 使用set/multiset查找以“s”开头的单词,c++,count,set,find,multiset,C++,Count,Set,Find,Multiset,这段代码的问题源于get_words_start_函数的最后一段代码 /* Name: xx Date: xx Purpose:Read text from file, count number of words, unique words, word frequency, & number of words that begin with letter 's' */ #include <iostream> #include <fstream>
/*
Name: xx
Date: xx
Purpose:Read text from file, count number of words, unique words, word frequency, & number of words that begin with letter 's'
*/
#include <iostream>
#include <fstream>
#include <string>
#include <set>
using namespace std;
multiset<string> display_and_load_words(string filename);
set<string> get_unique_words(multiset<string>& words);
set<string> get_words_beginning_s(multiset<string>& words);
int main() {
cout << "The Word Counter program\n\n";
string filename = "dickens.txt";
cout << "FILE TEXT: ";
//display_text(filename);
auto words = display_and_load_words(filename);
cout << "WORD COUNT: " << words.size() << endl << endl;
auto unique_words = get_unique_words(words);
auto words_beginning_s = get_words_beginning_s(words);
cout << unique_words.size() << " UNIQUE WORDS: ";
for (string word : unique_words) {
cout << word << ' ';
}
cout << endl << endl;
cout << "COUNT PER WORD: ";
for (string word : unique_words) {
cout << word << '=' << words.count(word) << ' ';
}
cout << endl << endl;
cout << "WORDS THAT BEGIN WITH 'S': ";
for (string word : words_beginning_s) {
cout << word << ' ';
}
cout << endl << endl;
}
multiset<string> display_and_load_words(string filename) {
multiset<string> words;
ifstream infile(filename);
if (infile) {
string word;
while (infile >> word) {
cout << word << ' ';
string new_word = "";
for (char c : word) {
if (c == '.' || c == ',') {
continue; // remove punctuation
}
else if (isupper(c)) {
new_word += tolower(c); // convert to lowercase
}
else {
new_word += c;
}
}
words.insert(new_word); // add word
}
cout << endl << endl;
infile.close();
}
return words;
}
set<string> get_unique_words(multiset<string>& words) {
set<string> unique_words;
for (string word : words) {
auto search = unique_words.find(word);
if (search == unique_words.end()) {
unique_words.insert(word);
}
}
return unique_words;
}
set<string> get_words_beginning_s(multiset<string>& words) {
set<string> words_beginning_s;
for (string word : words) {
auto search = words_beginning_s.find(word);
if (search == words_beginning_s.end()) {
for (int i = 0; i < words_beginning_s.size(); ++i) {
if (words_beginning_s[0] == 's') {
words_beginning_s.insert(word);
}
}
}
}
return words_beginning_s;
}
如果使用set/multiset,如何比较每个单独单词本身(而不是整个单词)中的位置值?文本文件中的字符串示例-John进入存储:而通常情况下,简单的for循环可以与初始位置一起使用,以比较值并计算它出现的次数,类似于-
for (int i = 0; i < words_beginning_s.length(); ++i) {
if (words_beginning_s[0] == 's') {
++s_word;
}
使用“集/多集”时,这不起作用。这是一个非常新的问题,如果这个问题看起来很愚蠢,那么很抱歉。您可以使用multisets成员函数来获取某个范围的迭代器,然后从该范围创建一个集合
例如:
#include <iostream>
#include <set>
#include <string>
std::set<std::string> get_words_beginning_s(const std::multiset<std::string>& words) {
// create a set from the iterators you get from lower_bound("s") and lower_bound("t"):
return {words.lower_bound("s"), words.lower_bound("t")};
}
int main() {
std::multiset<std::string> words{
"foo", "slayer", "bar", "sepultura", "tesseract", "skinny puppy", "yello"
};
for(const std::string& word : get_words_beginning_s(words)) {
std::cout << word << '\n';
}
}
它必须是某种类型的一套吗?它必须是一套。我知道。这就是我得到错误的地方。我不太明白的是,当使用集合时,它需要用什么方法来搜索每个单词的开头。无关:否则,如果isupperc{new_-word+=tolowerc;},否则{new_-word+=c;}只使用其他的new_-word+=tolowerc;-不需要先检查它是否为大写。查找介于和下限之间的单词谢谢你的解决方案@Ted。由于某种原因,我的代码在尝试此操作时停止工作,但当我从函数中删除常量时,它工作了。如果我尝试从for循环中删除const,它将再次停止工作。这是因为在这一点上,程序将修改值,否则,而在函数中,您可能希望更改某些内容?@DukeJohnson太棒了!在本例中,不允许修改std::string类型所用比较器中包含的集合中的值。如果你必须修改一个值,你必须从集合中提取它,修改它,然后再插入它。我明白了。非常感谢你的帮助和解释。
sepultura
skinny puppy
slayer