[英]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.