可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
using the following code:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec;
for(vector<int>::size_type ix = 0; ix != 10; ix++)
{
ivec.push_back(ix);
}
vector<int>::iterator mid = (ivec.begin() + ivec.end()) / 2;
cout << *mid << endl;
return 0;
}
I get an error compiling with g++:
iterator_io.cpp: In function `int main()':
iterator_io.cpp:13: error: no match for 'operator+' in '(&ivec)->std::vector<_Tp, _Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>]() + (&ivec)->std::vector<_Tp, _Alloc>::end [with _Tp = int, _Alloc = std::allocator<int>]()'
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/stl_iterator.h:654: note: candidates are: __gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+(const typename std::iterator_traits<_Iterator>::difference_type&) const [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/stl_bvector.h:261: note: std::_Bit_iterator std::operator+(ptrdiff_t, const std::_Bit_iterator&)
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/stl_bvector.h:345: note: std::_Bit_const_iterator std::operator+(ptrdiff_t, const std::_Bit_const_iterator&)
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/stl_iterator.h:765: note: __gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::operator+(typename __gnu_cxx::__normal_iterator<_Iterator, _Container>::difference_type, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&) [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]
I know the ivec.end() can not be used as a normal vector element. But I can't understand what the error information means...something about operator+?
回答1:
You cannot add two iterators together.
operator+
is not defined for two iterators, because that operation wouldn't make sense. Iterators are a kind of generalization over pointers - they point to the specific element stored in container. At which element the sum of iterators is pointing?
However, when you use a vector, you can add integers to iterators, like that:
vec.begin() + vec.size() / 2
and that is why you have candidates are: (...)
in your error message, followed by some definitions of operator+
.
In your case the best, and cleanest way will be not using the iterators, but simple getting the value from specified position:
int mid = vec[vec.size() / 2];
回答2:
You cannot add iterators. What you can do:
vector<int>::iterator mid = ivec.begin() + distance(ivec.begin(), ivec.end()) / 2;
回答3:
It simply means that vector
iterators have no addition operator (+
). You cannot add ivec.begin()
and ivec.end()
.
To get the middle element, you can simply use the subscript operator:
cout << ivec[ivec.size()/2] << endl;
If you insist on using an iterator, you can get an iterator that points to the middle in this fashion:
vector<int>::iterator mid = ivec.begin();
mid += ivec.size()/2;
cout << *mid << endl;
You can do this, because the vector
iterator is a random access iterator (in all implementations I'm aware of it encapsulates an actual pointer to the raw container-data).
回答4:
You can't add iterators.
What you'll want to use is a combination of std::distance() and std::advance():
vector<int>::iterator mid = std::advance(ivec.begin(), std::distance(ivec.begin(), ivec.end()) / 2);
Why use std::advance()
instead of the iterator's addition operator? std::advance()
works optimally regardless of the iterator type (random access, forward only, bidirectional, etc), so if you switch from std::vector
to std::list
the above code can remain the same, and still work optimally.
回答5:
not possible to add two iterators
use at to get the middle item:
ivec.at(ivec.size()/2);
回答6:
You can not add two iterators together, because iterators are not integers. That's what the error message means. If you want to access the element in the middle of a vector, use ivec[ivec.size() / 2]
.