hashtable,无序映射迭代器用法
hashtable,unorderd_map iterator usage
我希望熟悉unorderd_map的人能给我一个最好的问题答案。
我试图使用迭代器访问存储在无序映射上的值,但遇到了以下错误。
error: assignment of data-member ‘mystruct::time_diff’ in read-only structure
以下是我的代码示例:
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
struct mystruct
{
string str_name;
time_t my_last_time;
int time_diff;
};
int main()
{
unordered_map<int,mystruct>hash_table;
//filling my hash table
for(int i=0;i<10;i++)
{
mystruct myst;
myst.str_name="my string";
myst.my_last_time=time(0);
myst.time_diff=0;
hash_table.insert(make_pair(i,myst));
}
//now, i want to access values of my hash_table with iterator.
unordered_map<int,mystruct>::const_iterator itr=hash_table.begin();
for (itr = hash_table.begin(); itr != hash_table.end(); itr++ )
{
time_t now=time(0);//pick current time
itr->second.time_diff=now-itr->second.my_last_time;//here is where my error comes from
}
return 0;
}
因此,当编译时出现错误:
error: assignment of data-member ‘mystruct::time_diff’ in read-only structure
const_iterator
允许您在不修改容器成员的情况下对容器成员进行迭代。
如果你想修改它们,你需要使用一个普通的iterator
。
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 如何在c++迭代器类型中包装std::chrono
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 对于set上的循环-获取next元素迭代器
- 为什么output_editor Concept不需要output_e迭代器标记
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 使用迭代器时如何访问对象在向量中的位置?
- std::vector::迭代器是否可以合法地作为指针
- 跟随整数索引列表的自定义类迭代器
- 不明白迭代器,引用和指针失效,一个例子
- C++迭代器"for loop"习惯用法,其步长> 1 并允许非随机访问反向迭代器
- 广度优先搜索中的 C++ 迭代器和指针用法
- hashtable,无序映射迭代器用法
- 从 std::unique 返回的迭代器的用法
- C++ 集合、迭代器、查找发现重复项的行的用法
- 如何使用擦除-删除习惯用法并在循环中增加迭代器
- c++类迭代器实用程序:定义和用法