C++地图和集合相关查询
C++ Maps & Sets related queries
我试图解决c++入门书中关于地图的一个特定问题;在这些集合中,我必须制作一个用作单词计数器的地图容器,但我必须忽略标点符号&案例。例如,"example."、"example"answers"example"都应该增加单词计数器中的同一个键。
地图的基本代码如下:-
map<string, size_t> word_count;
string word;
while (cin >> word)
++word_count[word];
for (const auto &a : word_count)
cout <<a.first<<" occurs "<<a.second<<((a.second>1) ? " times" : " time");
那么,我应该在这个代码中添加什么,这样我的单词计数器就会忽略大小写&相同单词上的标点符号?
#include <map>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using std::map;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
using std::transform;
using std::remove_if;
int main( int, char** )
{
map< string, string::size_type > dictionary;
string word = "";
while ( getline( cin, word ) )
{
word.erase( remove_if( word.begin( ), word.end( ), ::isspace ), word.end( ) );
word.erase( remove_if( word.begin( ), word.end( ), ::ispunct ), word.end( ) );
transform( word.begin( ), word.end( ), word.begin( ), ::tolower );
dictionary[ word ]++;
}
for ( const auto& entry : dictionary )
{
auto word = entry.first;
auto count = entry.second;
cout << "'" << word << "' total count: " << count << endl;
}
return EXIT_SUCCESS;
}
构建
clang++-stdlib=libc++-std=c++11-o家庭作业.cpp
资源
- CPP参考网站
- CPP资源网络
- C++资源的最终列表
您可以使用tolower:
#include <algorithm>
#include <string>
std::string str = "ExampleText";
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
同时删除所有标点符号。为此,您必须使用自定义算法。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 处理多个异常集合的C++方法
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- 查询SQLite数据库中的日期
- 如何将ampl中的集合表示为c++中的向量
- 检查值是否在集合p1和p2中,但不在p3中
- 带过滤器的现代迭代c++集合
- 如何将结构插入到集合中并打印集合的成员
- 集合上的输出迭代器:assign和increment迭代器
- 如何将函数集合传递给客户端类,以便将它们当作客户端类本身的成员使用
- 实现一个在集合上迭代的模板函数
- 如何在ArangoDb AQL查询中指定数据库
- 尝试使用集合函数时出现分段错误
- 互斥指针的集合
- c++找不到具有相同哈希的无序集合元素
- 查询数据库以在 C++ 中创建自定义类集合的最佳方法
- MongoDB C驱动程序:如何使用Regex查询集合
- C++地图和集合相关查询
- 查询Qt资源集合(.qrc)中的资源数量
- 具有前缀和后缀查询的字符串集合的数据结构