Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么可以';增量std::无序映射迭代器? std::无序映射\u缓存; std::vector_lruList;_C++_Iterator_Constants_Increment_Unordered Map - Fatal编程技术网

C++ 为什么可以';增量std::无序映射迭代器? std::无序映射\u缓存; std::vector_lruList;

C++ 为什么可以';增量std::无序映射迭代器? std::无序映射\u缓存; std::vector_lruList;,c++,iterator,constants,increment,unordered-map,C++,Iterator,Constants,Increment,Unordered Map,这很有效 std::rotate(_lruList.begin(),_lruList.begin()+1,_lruList.end()) 但事实并非如此 std::rotate(_cache.begin(),_cache.begin()+1,_cache.end());//缓存上发生错误。begin()+1表示“错误类型” 这对我来说没有意义,因为它们都是迭代器,除了一个用于向量,一个用于无序映射 然后我也试过这个 std::rotate(_cache.begin(),_cache.begin(

这很有效

std::rotate(_lruList.begin(),_lruList.begin()+1,_lruList.end())

但事实并非如此

std::rotate(_cache.begin(),_cache.begin()+1,_cache.end());//缓存上发生错误。begin()+1表示“错误类型”

这对我来说没有意义,因为它们都是迭代器,除了一个用于
向量
,一个用于
无序映射

然后我也试过这个
std::rotate(_cache.begin(),_cache.begin()++,_cache.end())

但我犯了以下错误:
\u左:不能分配给常量变量

\u Right:不能将常量赋值给变量
无序映射
迭代器是正向迭代器。这意味着他们一次只能移动一步,只能向前移动,从一个位置到另一个位置需要穿过所有的中间位置。因此,前向迭代器不支持
运算符+
,因为这将是一个O(n)操作。标准库的作者认为,当人们看到
a+b
时,他们希望它是O(1),因此如果迭代器类型不能满足该要求,那么就不应该支持操作符

vector
迭代器是随机访问的,这意味着它们确实支持
操作符+
,因为它可以实现为O(1)。您可以这样做:

std::unordered_map<int, int> _cache;

std::vector<std::unordered_map<int, int>::iterator> _lruList;

但这也不起作用,因为
std::rotate
是一个修改操作。您不能修改
unordered_-map

unordered_-map
迭代器是正向迭代器。这意味着他们一次只能移动一步,只能向前移动,从一个位置到另一个位置需要穿过所有的中间位置。因此,前向迭代器不支持
运算符+
,因为这将是一个O(n)操作。标准库的作者认为,当人们看到
a+b
时,他们希望它是O(1),因此如果迭代器类型不能满足该要求,那么就不应该支持操作符

vector
迭代器是随机访问的,这意味着它们确实支持
操作符+
,因为它可以实现为O(1)。您可以这样做:

std::unordered_map<int, int> _cache;

std::vector<std::unordered_map<int, int>::iterator> _lruList;

但这也不起作用,因为
std::rotate
是一个修改操作。您不能修改
无序映射中元素的键

我明白了。谢谢你这么详细的解释!为什么允许在前向迭代器上使用
++
,但不允许在前向迭代器上使用
+1
?@Iamanon不能基于值而仅基于类型重载函数。因此,如果不允许
+1000
,则不能允许
+1
。我在回答中解释了不允许的原因。我明白了。谢谢你这么详细的解释!为什么允许在前向迭代器上使用
++
,但不允许在前向迭代器上使用
+1
?@Iamanon不能基于值而仅基于类型重载函数。因此,如果不允许
+1000
,就不能允许
+1
。我在回答中解释了不允许的原因。