【面试题总结】1、统计字符串中某个单词出现的次数(1-C++实现)
【解决方法一】C++ map解决
一、map中的find函数:
用于查找map中是否包含某个关键字条目,传入的参数是要查找的key,最后返回一个迭代器,如果没有找到,则返回的迭代器等于end()返回的迭代器。示例代码:
#include<iostream>
#include<string>
#include<map>
using namespace std; int main() {
map<int, string> mapStudent; mapStudent.insert(pair<int, string>(, "student_one"));
mapStudent.insert(pair<int, string>(, "student_two"));
mapStudent.insert(pair<int, string>(, "student_three")); map<int, string>::iterator iter;
iter = mapStudent.find();
if (iter != mapStudent.end())
cout << "Find,the value is: " << iter->second << endl;
system("pause");
return ;
}
运行结果:
二、map的插入方法:
插入的方法有好几种,下面介绍:map.insert(pair<type1,type2>(key,value))这种,还有一种是map[key] = value,前者出现重复不会发生改变,后者出现重复则会发生覆盖,后者如果没有给value值,直接使用map[key],则其value值默认为0。示例代码:
#include<iostream>
#include<string>
#include<map>
using namespace std; int main() { map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(, "student_one"));
mapStudent.insert(pair<int, string>(, "student_two"));
mapStudent.insert(pair<int, string>(, "xxooxxooxxo"));//不起作用 map<int, string>::iterator iter;
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {
cout << iter->first << ' ' << iter->second << endl;
}
cout << endl;
mapStudent[] = "xxooxxooxxoo";//覆盖掉前面的value
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {
cout << iter->first << ' ' << iter->second << endl;
}
system("pause");
return ;
}
运行结果:
三、是否插入成功?
我们通过pair来获取是否插入成功,pair返回两个变量,第一个是map的迭代器,第二个是插入成功的标志,插入成功pair的第二个参数是true,插入失败,第二个参数为false。实例代码:
pair<map<type1,type2>::iterator,bool> ret; ret = map_s.insert(pair<type1,type2>(key,value)) if(ret.second==ture) cout<<"插入成功"<<endl;
else
cout<<"插入失败"<<endl;
四、统计字符出现个数:
思路1:先创建一个map,遍历字符串,逐个判断如果存在则count++,不存在则创建一个,让其value为1
思路2:通过插入失败来增加字符的个数,如果插入失败则表明map中存在该字符,count++即可
思路3:需要对map了解,直接使用库里提供的[]运算符重载。通过键值找节点,直接给给实值+1.
思路1:先创建一个map,遍历字符串,逐个判断如果存在则count++,不存在则创建一个,让其value为1。代码如下:
#include<iostream>
#include<string>
#include<map>
using namespace std; // 方法1:
int main()
{
map<char, int> map_s;
string str = "kkk ahguird-j l"; for (int i = ; i < str.length(); ++i)
{
map<char, int>::iterator iter = map_s.find(str[i]);
if (iter != map_s.end())
{
iter->second++;
}
else // 如果找不到就添加一个,找到了就count++
{
map_s.insert(pair<char, int>(str[i], )); // 如果测试用例为 "qwerqwer"时,前4次循环都是执行的这句else即insert插入操作
}
}
map<char, int>::iterator iter = map_s.begin(); for (; iter != map_s.end(); iter++)
{
cout << iter->first << ' ' << iter->second << endl;
}
cout << endl; system("pause");
return ;
}
运行结果:
思路2:通过插入失败来增加字符的个数,如果插入失败则表明map中存在该字符,count++即可。代码如下:
#include<iostream>
#include<string>
#include<map>
using namespace std; int main()
{
map<char, int> map_s;
string str = "kkk ahguird-j l"; pair<map<char, int>::iterator, bool> ret;
for (int i = ; i < str.length(); ++i)
{
ret = map_s.insert(pair<char, int>(str[i], ));
if (ret.second == false) // 如果插入失败,则该迭代器的第一个参数的value++
{
ret.first->second++;
}
}
map<char, int>::iterator iter = map_s.begin();
for (; iter != map_s.end(); iter++)
{
cout << iter->first << ' ' << iter->second << endl;
}
cout << endl; system("pause");
return ;
}
运行结果:
思路3:直接使用库里提供的[]运算符重载。通过键值找节点,直接给给实值+1.
#include<iostream>
#include<string>
#include<map>
using namespace std; int main() {
string str;
map<char, int> map_s;
while (cin >> str) {
for (int i = ; i < str.length(); ++i) {
map_s[str[i]]++;
}
map<char, int>::iterator iter;
for (iter = map_s.begin(); iter != map_s.end(); ++iter) {
cout << iter->first << ':' << iter->second << endl;
}
}
}
思路3是真的6阿~
参考连接:https://blog.csdn.net/qq_34312386/article/details/55281662
对思路3补充说明:假设一个map对象map_s中只存在一对pair<char,int>('a',1),现在执行map_s['b']则map_s中存在两对pair分别是('a',1),('b',0),是的,没错,'b'的value默认是0,那么如果刚才执行的不是map_s['b'],而是map_s['b']++,那么map_s中的两对pair为('a',1)和('b',1),理解为插入‘b’,然后value值++,所以,map_s['b']++这个语句的理解如下:
如果map_s中不存在key为‘b’的元素,那么在map_s中添加‘b’,value默认为0,然后map_s['b']++,value变成1.
如果map_s中存在key为 'b' 的元素,那么该语句表示map_s['b']的value++.
#include<iostream>
#include<map>
using namespace std; int main() {
map<char, int> m;
m.insert(pair<char, int>('a', ));
map<char, int>::iterator iter = m.begin();
cout << iter->first << ' '<<iter->second<<endl;
m['b'];
iter++;
cout << iter->first << ' ' << iter->second << endl;//value默认是0
m['b']++;
cout << iter->first << ' ' << iter->second << endl;//将value++
while ();
}
【面试题总结】1、统计字符串中某个单词出现的次数(1-C++实现)的更多相关文章
- Java基础知识强化之集合框架笔记61:Map集合之统计字符串中每个字符出现的次数的案例
1. 首先我们看看统计字符串中每个字符出现的次数的案例图解: 2. 代码实现: (1)需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5) ...
- 统计字符串中每个字符出现的次数(Python)
#统计字符串中每个字符出现的次数 以The quick brown fox jumps over the lazy dog为例 message='The quick brown fox jumps o ...
- HashMap 统计一个字符串中每个单词出现的次数
HashMap 统计一个字符串中每个单词出现的次数 import java.util.HashMap; import java.util.Map; public class Test { public ...
- 使用Map,统计字符串中每个字符出现的次数
package seday13; import java.util.HashMap; import java.util.Map; /** * @author xingsir * 统计字符串中每个字符出 ...
- python统计文本中每个单词出现的次数
.python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...
- 【面试题总结】1、统计字符串中某个字符出现的次数(2-Python实现)
1.可以使用Python的字典实现,对于一个特定的字符串,使用for循环遍历其中的字符,并保存成字典形式.字典的key为字符,value为字符在整个字符串中出现的次数. 2.拓展:如果题目为比较两个字 ...
- Java 13天基础 06天map集合小练习(黑马程序员) 统计字符串中每个字符出现的次数 (经典面试题)
import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * 目标 : 输出一个字符串中每个字符出现的 ...
- javascript 统计字符串中每个字符出现的次数
var str = "abdcadfasfdbadfafdasdfasyweroweurowqrewqrwqrebwqrewqrejwq;;"; // console.log(nu ...
- Java中统计字符串中各个字符出现的次数
import java.util.Iterator; import java.util.Set; import java.util.TreeMap; public class TreeMapDemo ...
随机推荐
- vue、react中循环遍历为什么会有key,key有什么作用?
先讲一下,vue和react都是在操作虚拟dom,并且根据diff算法进行新旧dom对比,从而更新dom,以vue举例: vue官方文档中写到有 key 的特殊属性主要用在 Vue 的虚拟 DOM 算 ...
- iOS7 新后台及下载SDK介绍
在iOS7以前的系统中,App默认是不能后台运行的,如果要后台运行,可以采用以下两类方法: (1)使用beginBackgroundTaskWithExpirationHandler函数,向系统申请一 ...
- stm32 SysTick系统定时器
它是一个24位向下递减的定时器,每计数一次所需时间为1/SYSTICK,SYSTICK是系统定时器时钟,它可以直接取自系统时钟,还可以通过系统时钟8分频后获取 当定时器计数到0时,将从LOAD 寄存器 ...
- Ubuntu18.0 解决python虚拟环境中不同用户下或者python多版本环境中指定虚拟环境的使用问题
一. 不同用户下配置virtualenvwrapper的问题 问题描述: 安装virtualnev和virtualnevwrapper之后,在.bashrc进行virtualenvwrapper的相关 ...
- 【问题】XShell连接不上Debian root用户
类似文章:https://www.lianst.com/3231.html 修改此文件 重启ssh服务 ssh restart有问题,换一条命令OK 你的Linux发行版可能不一样,针对CentOS参 ...
- 熟记这些python内置函数,你离大佬就不远了
python内置了很多函数,方便我们在写程序的时候调用,在ython 2.7 的所有内置函数共有80个.熟练记住和使用这些内置函数,将大大提高写Python代码的速度和代码的优雅程度. 以下代码示例用 ...
- jquery中prop,attr,data的区别
这两天翻了jq的源码,今天看到了jq关于数据存储的几个方法,遂总结一下,和小伙伴没分享一下,哪里说的不对,还望批评指正~~~ 废话不多说,直接上代码: $(function(){ $('#div1') ...
- Hibernate初探之单表映射——第二章:Hibernate进阶
第二章:Hibernate进阶 1.hibernate.cfg.xml常用配置 2.session 简介 3.transaction简介 4.session详解 5.对象关系映射常用配置 1.hibe ...
- 为什么Object.prototype在Function的原型链上与Function.prototype在Object的原型链上都为true
关于javascript的原型链有一个问题我一直很疑惑:为什么 Function instanceof Object 与 Object instanceof Function都为true呢? Func ...
- 一款超好用的第三方评论插件--Gittalk
使用GITALK的背景: 1. 最近在做一个基于Java的个人博客系统,已经基本完工了,突然发现怎么没有评论的操作,如果再从头开始从数据库开始写的话,花费的代价有点大,于是乎我就在网上寻找一款适合我的 ...