增加贴图的值
Increment the value of a map
本文关键字:增加 更新时间:2023-10-16
需要你的帮助,如果你能快速帮助我,那就更好了。这是一个非常琐碎的问题,但仍然无法理解我到底需要在一行中放什么。
下面的代码我有
for (busRequest = apointCollection.begin(); busRequest != apointCollection.end(); busRequest++)
{
double Min = DBL_MAX;
int station = 0;
for (int i = 0; i < newStations; i++)
{
distance = sqrt(pow((apointCollection2[i].x - busRequest->x1), 2) + pow((apointCollection2[i].y - busRequest->y1), 2));
if (distance < Min)
{
Min = distance;
station = i;
}
}
if (people.find(station) == people.end())
{
people.insert(pair<int, int>(station, i));
}
else
{
how can i increment "i" if the key of my statation is already in the map.
}
}
简单地说,我坐第一辆公共汽车去第二个环路,坐第一个车站,找到最小距离。在我完成第二个循环后,我将距离最小的车站添加到我的地图中。在我进行所有循环之后,如果有同一个站,我需要增加它,所以这意味着该站正在使用两次等等。
我需要帮助,只要给我提示或提供我需要添加的行。
我提前感谢你,等待你的帮助。
我想你指的是Min Distance
而不是i
?请检查并告诉我。
for (busRequest = apointCollection.begin(); busRequest != apointCollection.end(); busRequest++)
{
double Min = DBL_MAX;
int station = 0;
for (int i = 0; i < newStations; i++)
{
distance = sqrt(pow((apointCollection2[i].x - busRequest->x1), 2) + pow((apointCollection2[i].y - busRequest->y1), 2));
if (distance < Min)
{
Min = distance;
station = i;
}
}
if (people.find(station) == people.end())
{
people.insert(pair<int, int>(station, i)); // here???
}
else
{
// This routine will increment the value if the key already exists. If it doesn't exist it will create it for you
YourMap[YourKey]++;
}
}
在C++中,您可以直接访问映射键,而无需插入它。C++将自动使用默认值创建它。在您的情况下,如果people
映射中不存在station
,并且您将访问people[station]
,则people[station]
将自动设置为0
(int
的默认值为0)
所以你可以这样做:
if (people[station] == 0)
{
// Do something
people[station] = station; // NOTE: i is not accessible here! check ur logic
}
else
{
people[station]++;
}
此外:在您的代码中,不能在IF条件内访问i
以插入人员映射。
相关文章:
- 数组索引的值没有增加
- 为什么我的代码在输出中增加了93天
- 有效地使用std::unordered_map来插入或增加键的值
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 为什么要增加导致崩溃的指针
- 增加传递地址的值
- 如何增加以前由新运算符分配的 C++ std::list 数组的大小?
- 为什么"i"在循环的每次迭代中都没有增加?(C++)
- std::thread 增加 DLL 引用计数,从而防止卸载 DLL
- 如何在C++中为增加但记住删除先前对象的对象分配唯一标识符
- 将一对插入地图并增加计数?
- 整数类型应该显式转换(例如"int"到"无符号")还是只会增加混乱?
- 我的 c++ 应用程序中的运行时间从 0 增加到 60 太快了(例如一毫秒或一微秒)
- 为什么当通过 TCP 发送的消息速率增加时,请求-响应消息对的延迟会降低?
- 琐碎并发代码的吞吐量不会随着线程数量的增加而增加
- C++:功能在输出前自动使用50减/增加输入数
- 追加位以增加字符的大小
- 为什么在C++中增加指针后打印了一个值而不是 NULL/0?
- 内存增加和字符串流?
- 如何增加使用 CUDA 实现的 FLOPS