Friday, 15 June 2012

iterate through an array of vector in c++ -


I'm trying to iterate through my vector array, but I can not seem to do this work , My error is on another, and for the loop follow the error message:

Expected primary expression before 'double'

I have seen How to recycle a regular vector, but how do I move a vector array? I followed this structure:

  #include & lt; Iostream & gt; # Include & lt; Vector & gt; Int main () {std :: vector & lt; Int & gt; Myvector; For (int i = 1; i & lt; = 5; i ++) myvector.push_back (i); Std :: cout & lt; & Lt; "Include in the maccut:"; (Std :: vector & lt; int & gt; :: iterator = myvector.begin (); for it! = Myvector.end (); ++ this) std :: cout & lt; & Lt; '' & Lt; & Lt; this *; Std :: cout & lt; & Lt; '\ N'; Return 0; }   

I can not seem to get my vector array version to work.

  #include & lt; Iostream & gt; # Include & lt; Fstream & gt; # Include & lt; Vector & gt; using namespace std; Int main (int arc, char * argv []) {vector & lt; Double & gt; Vector [7]; Double number [7]; Ifstream infile ("data.txt"); String floating; For (int i = 0; i > no [0]> gt; [1]> gt; [2]> gt; [3]> gt; [4]> [4] & gt; no [5]] & Gt; number [6]) {for (int i = 0; i & lt; = 6; i ++) {vector [i]. Push_back (num [i]); }} Cout & lt; & Lt; Endl; For (int i = 0; i & lt; = 6; i ++) {// Error on this line // Make sure that what is wrong before vector & lt; Double & gt; :: iterator it = vector for [i] .begin () (vector  gt;: iterator = vector [i]. Beginn (); it! = Vector [i] .end (); ++ this) {cout & lt; & Lt; '' & Lt; & Lt; this *; }} Return 0; }    

on this line of code

  For (vector & lieutenant; double> gt;: iterator = vector [i]. Beginn (); it! = Vector [i] .end (); ++ it)   

vector is not a type, it is a variable in the local scope to hide names in the global scope.

You can type

  for (:: std :: vector & lt; double> gt; :: iterator = vector} instead .begin ( ); It! = Vector [i] .end (); ++ this)   

or

 for  (it's auto = vector [I] .begin (); it! = Vector [i] .end (); ++ it)   

But a better solution is not to use the same name for many different things.

No comments:

Post a Comment