First look at the picture
Code:
//test.cpp: Defines the entry point of the console application.
//C + + programming
//2015-5-4
//STL strandard Template libeary
#include"stdafx.h"
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <iterator>
usingnamespaceStd
int_tmain (intARGC, _tchar* argv[])
{
//examples of generic pointers and copy functions
DoubleA[] = {1.1,4.4,3.3,2.2};
vector<Double> va (A, A +4);//Initialize and define VA
typedef vector<Double>::iterator VI;
VI first = Va.begin ();//customize a forward generic pointer first and point to the initial element of VA
for(First, first < Va.end (); first++)//forward Output VA
cout << *first <<" ";
cout << Endl;
//cyclic reverse output VA
for(--first; First>va.begin (); first--)Do not do Va.begin ()-1
{
cout << *first <<" ";
if(First = = Va.begin () +1)When the pointer reaches an address after being (), the value on the Begin () address is output at the same time
cout << * (first-1) <<" ";
}
cout << Endl;
Copy (Va.begin (), Va.end (), ostream_iterator<Double> (cout," "));//overall forward output VA
cout << Endl;
typedef vector<Double>::reverse_iterator Vri;
Vri last = Va.rbegin ();
//cycle from tail to first output VA using reverse pointer
for(last; Last<va.rend (); last++)
{
cout << *last <<" ";
}
cout << Endl;
//use reverse pointer loop to output VA from first to tail
for(--last; Last>va.rbegin (); last--)
{
cout << *last <<" ";
if(Last = = Va.rbegin () +1)When the pointer reaches the previous address of rbeing (), outputs the value on the Rbegin () address at the same time
cout << * (Last-1) <<" ";
}
cout << Endl;
Copy (Va.rbegin (), Va.rend (), ostream_iterator<Double> (cout," "));//overall from tail to first output VA
cout << Endl;
return0;
}
cannot be for(--first; First>va.begin (); first--) or for(--last; Last>va.rbegin (); last--).
va.begion ()-1 orVa.rbegin ()-1, because the fisrt pointers, theyThe scopes in the array are respectively [Begion,end] and (rend,rbegion)
That is, a range other than a.begin when the forward generic pointer reverses the loop. So the code-1 knowing pointer continues to do first--, to this error Expreeeion:vector iterator + offset out of range.
The inverse generic pointer is the same.
C + + generic pointer forward and reverse loop read times wrong expreeeion:vector iterator + offset out of range problem