如何在c++中使用映射
How to use maps in c++?
我读过c++中的映射,我知道它们的作用,但我不知道如何使用它们。
如果我有n个数字,并且每个数字都分配了一定数量的苹果(例如"x1=5"有4个苹果,"x2=-2"有7个苹果等),我如何对这些数字进行排序并相应地求和苹果的数量,以便x1仍然拥有相同数量的苹果,尽管它在排序数据结构中的位置(我认为是映射)会改变?
我不太明白你的问题,但你可以这样说:
std::map<int, int> map;
map[5] = 4; // x1 = 5 has 4 apples.
map[-2] = 7; // x2 = -2 has 7 apples.
//to find the value of x1 :
int x1 = map[5];
std::map
中的项总是按键排序,所以您不必手动排序。
赋值后的和。
std::map<int,int>::iterator it;
int total = 0;
for(it i = map.begin(); i != map.end(); ++i)
{
total+=it->second;
}
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 将函数类成员映射到类本身内部
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 使用std::函数映射对象方法
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++映射分割错误(核心转储)
- 内联映射初始化的动态atexit析构函数崩溃
- 使用"std::unordereded_map"映射到"std::list"对象
- 如何从多映射中删除特定的重复项
- 在未初始化映射的情况下,将值插入到映射的映射中
- QT通过C++添加映射QML项目
- 在c++中访问int到类对象的映射时出错
- 在C++中搜索嵌套多映射值
- 错误处理.将系统错误代码映射到泛型
- C++匿名结构作为std::映射值
- 如何从存储在std::映射中的std::集中删除元素
- 递归无序映射
- 正在VS调试器中监视映射条目