c++ - Input not being put in through out Vector -
okay, trying read input vector, print out in reverse order. have pretty idea of how ( think) problem code reads user input first slot of vector. . . need read 10 separate strings input 10 different slots in vector. . . here i've got far:
#include <iostream>; #include <vector>; #include <string>; using namespace std; int main() { vector<string> name_list(11); int n = 0; (int n = 0; n < 11; ++n); getline(cin, name_list[n]); cout << name_list[0]; int stop; cin >> stop; return 0; }
you have semicolon after loop statement
for (int n = 0; n < 11; ++n);
so loop nothing
also speaking 10 elements need read.
the code following way:
const size_t n = 10; vector<string> name_list( n ); ( int n = 0; n < n; ++n ) { getline( cin, name_list[n] ); } ( vector<string>::size_type n = name_list.size(); n != 0; ) { cout << name_list[--n] << endl; }
also same can written using standard algorithms. example
const size_t n = 10; vector<string> name_list; name_list.reserve( n ); copy_n( istream_iterator<string>( cin ), n, back_inserter( name_list ) ); reverse_copy( name_list.begin(), name_list.end(), ostream_iterator<string>( cout, "\n" ) );
Comments
Post a Comment