C + + generic pointer forward and reverse loop read times wrong expreeeion:vector iterator + offset out of range problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.