[英]Why does the compiler allow vector.begin()=vector.end() in C++?
[英]Random access iterators - does vector.end() - vector.begin() = vector.size() in C++;
我想写一些类似于下面的代码
class c{
public:
//...
big_structure* find(int e){
auto it = std::lower_bound(v1.begin(), v1.end(), e);
return v2[it - v1.begin()];
}
private:
std::vector<int> v1; //v1 is sorted;
std::vector<big_structure*> v2; //v2.size() = v1.size() + 1
}
这合法吗,而且当e不在v1中时,它将返回v2 [v1.size()]吗?
如果可能的话,我不希望它== v1.end()。
这合法吗,而且当
e
不在v1
时会返回v2[v1.size()]
吗?
是。 可以将末端随机访问迭代器与相同序列中的其他迭代器进行算术运算,从而获得预期的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.