Here, I learn lists, and I do not understand why it does not display a sorted list.

Displays only if entered as follows:

copy (myList.begin (myList.end (), ostream_iterator (cout, ""));


#include <iostream> #include <list> #include <iterator> #include <ctime> #include <cstdlib> using namespace std; int main() { list<int> myList; srand(time(NULL)); for(int i = 0; i < 15; i++) { myList.push_back(rand()%20); } list<int>::iterator v = myList.begin(); list<int>::iterator v1 =myList.end(); cout << "Список: "; for( v ; v!=v1;v++) { cout<<*v<<" "; } myList.sort(); cout << "\nОтсортированный список: "; for( v ; v!=v1;v++) { cout<<*v<<" "; } 

    2 answers 2

    It does not output because in the second cycle v==v1 and it simply does not execute. It is necessary so:

     for ( list<int>::const_iterator it = myList.begin(); it != myList.end(); it++) { cout << *it << " "; } 

      It was possible just before the second loop (or inside it) to set the iterator to the beginning v = myList.begin();