c++ - 是否可以在 C++ 中声明两个变量 For-Each 循环?
问题描述
我正在尝试编写一个可以检查的函数,如果两个字符串具有相同频率的所有字符。所以为了做到这一点,我尝试制作两个地图,即unordered_map<char, int>
. 所以在我的下一步中,我只想比较char
地图上每个值的每个值。
string A;
string B;
unordered_map<char, int> a;
unordered_map<char, int> b;
for (char i : A)
{
a[i]++;
}
for (char i : B)
{
b[i]++;
}
for (char i : A &&char j : B)
{
//code goes here
}
那么,是否有可能有一个带有两个不同变量的 For-Each 循环?
解决方案
我将绕过您的for
循环问题,说您是否想知道两个映射是否包含您可以使用的相同键值元素std::unordered_map::operator==
。
unordered_map<char, int> a;
unordered_map<char, int> b;
for (char i : A)
{
a[i]++;
}
for (char i : B)
{
b[i]++;
}
if (a == b) // this does the right thing
{
// the frequencies are the same
}
推荐阅读
- javascript - 添加新项目已加载,指示器图像在项目从 SharePoint 中的文件夹加载到网页后
- node.js - TypeError:无法读取猫鼬上未定义的属性“查找”
- node.js - 找不到模块'sqlite3'
- php - Laravel 中的 Slack 通知 - to() 函数什么都不做?
- python - 如何使用 discord.py 获取服务器 ID?
- flutter - 如何将 showdatepicker + showtimepicker 结果与当前日期 (datetime.now()) 进行比较
- javascript - 对象数组首先显示具有指定键的对象
- corda - Corda 节点驱动程序不拾取自定义序列化程序
- elixir - 如何正确使用mix phx.gen.html?收到错误:“(混合)预期架构“帖子”是有效的模块名称”
- javafx - 为什么我的 javafx 标签在更改后不显示?