c++ - My range for loop doesn't work -


I have to print the vector size and all the contents inside it. But it is not repeated for my loop, it does not go above one, but rather the value of the whole loop stays at 0.

  #include "stdafx.h" #include & lt; String h & gt; #include & lt; String & gt; # Include & lt; Iostream & gt; # Include & lt; Cctype & gt; #include & lt; Vector & gt; # Include & lt; List & gt; Using std :: string; Using Std :: vector; Of vector & lt; Int & gt; V2 (10); (Auto i: v2) {if (i == 0) {std :: cout & lt; & Lt; V2.size () & lt; & Lt; Std :: endl; } Std :: cout & lt; & Lt; "Element Value" & lt; & Lt; (I) & lt; & Lt; "Is" & lt; & Lt; V2 [i] & lt; & Lt; Std :: endl; }  

So I only want to print the size once. Then print each element value that I know will be 0 by default. But it only displays "Element Value 0" 9 times.

If you print the size of the vector first, then the output statement before the range based on the statement

  std :: vector & lt; Int & gt; V2 (10); Std :: cout & lt; & Lt; V2.size () & lt; & Lt; Std :: endl; Size_ty i = 0; (Auto x: v2) {std :: cout & lt; & Lt; "Element Value" & lt; & Lt; I ++ & lt; & Lt; "Is" & lt; & Lt; X & LT; & Lt; Std :: endl; }  

But if you are using a count then it would be better to use general for the statement

  std :: vector & lt; Int & gt; V2 (10); Std :: cout & lt; & Lt; V2.size () & lt; & Lt; Std :: endl; (Std :: vector & lt; int & gt; :: size_type i = 0; i & lt; v2.size (); i ++) {std :: cout & lt; & Lt; "Element Value" & lt; & Lt; I & lt; & Lt; "Is" & lt; & Lt; V2 [i] & lt; & Lt; Std :: endl; }  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -