// ForeachDemo.cpp Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2013-10-19 File created. // 2013-10-19 Last modification. // I read from the net that the new C++ should support foreach loops // similar to those in Java. // I tried to make this program, but my compiler is old. // In the end the program showed very strange behaviour. #include #include // class string etc. #include using namespace std ; int main() { int integers_to_vector_based_array[ 5 ] = { 33, 444, 55, 6, 777 } ; vector array_of_integers ; for ( int integer_index = 0 ; integer_index < 5 ; integer_index ++ ) { array_of_integers.push_back( 4 ) ; } cout << "\n\n array_of_integers printed with a for loop:\n\n " ; for ( int integer_index = 0 ; integer_index < array_of_integers.size() ; integer_index ++ ) { cout << " " + array_of_integers[ integer_index ] << flush ; } /* System.out.print( "\n\n array_of_integers printed with a foreach loop:\n\n " ) ; for ( int integer_in_array : array_of_integers ) { System.out.print( " " + integer_in_array ) ; } **/ cout << "\n\n array_of_strings printed with a traditional for loop:\n\n " ; vector array_of_strings ; //= { "January", "February", "March" } ; array_of_strings.push_back( "January" ) ; array_of_strings.push_back( "February" ) ; array_of_strings.push_back( "March" ) ; for ( int string_index = 0 ; string_index < array_of_strings.size() ; string_index ++ ) { cout << " " << array_of_strings[ string_index ] ; } cout << "\n\n array_of_strings printed with a foreach loop:\n\n " ; /* for ( string& string_in_array : array_of_strings ) { cout << " " << string_in_array ; } */ }