Hello I just can't get it to work. I ahve structure with 4 members and I create list and iterator:
std::list<structure> one;
std::list<structure>::iterator two;
Now I want to get acces to element:
one.end-1;
two = one.end()-1;
it doesn't work >.> Compilator throws like 14 errors.
Errors go like:
All of them goes something like:
Glowne.cpp(105) : error C开发者_C百科2784: 'reverse_iterator<_RanIt>::difference_type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::list<_Ty>::_Iterator<_Secure_validation>' with [ _Ty=Klient, _Secure_validation=true ]
Everything working great, thanks for mentioning back method, really guys You are great.
Bidirectional iterators (as used by std::list
) do not support "pointer arithmetic", only single steps:
two = one.end();
--two;
Also, the statement one.end-1;
has absolutely no effect at all, I would get rid of that.
If you want the last element of a list, use the back()
member.
List is not a random access container, that means you cannot use arithmetic operations on its iterators.
To move through the list you can use operators ++
and --
to move the iterator by one element.
If you'd like to move to the arbitrary position, use something other, for example std::vector, instead.
Most of the containers have front()
and back()
method, for accessing first and last element.
精彩评论