//aList.cpp -- by tsaiwn@csie.nctu.edu.tw // test list in C++ STL; see http://www.cplusplus.com/reference/stl/ #include #include #include #include #include using namespace std; void print(int b[ ], int n); // later void print(list x) { list::iterator i = x.begin( ); for( ; i != x.end( ); ++i) cout << " " << *i; cout << endl; }//print(list void testAry(int y[ ], int); bool myCompf(int a, int b) { return a > b; } //Descending int main( ) { int x[ ] = {55, 88, 38, 49, 58}; list a (x, x+(sizeof x/sizeof x[0])); cout << "Original list a: "; print(a); cout <<"a.sort( ): "; a.sort( ); print(a); cout << "with myCompf: "; a.sort(myCompf); print(a); //Java: Collections.sort(a, Collections.reverseOrder()); print(a); cout << "Pass original x[ ] to testAry..\n"; testAry(x, sizeof x/sizeof x[0]); cout << "=== after testAry( ):" << endl; print(x, sizeof x/sizeof x[0]); return 0; }//main( void testAry(int y[ ], int n) { cout << "=== testAry ===" < v(y, y+n); v.insert(v.begin( ), 85); // head v.push_back( 66); // tail // though we can sort/print Vector ... but .. int *yy = new int[v.size( )]; // dynamic array of size of v copy(v.begin( ), v.end( ), yy); // in print(yy, n); sort(yy, yy+n); // yy is an Array print(yy, n); //Java: sort(yy, Collections.reverseOrder()); print(yy, n); n = v.size( ); y = new int[n]; // OK because int y[ ] is actually int *y for(int i=0; i < n; ++i) y[i]= v[i]; // copy v into array cout << "In testAry, new y== "; print(y, n); }// testAry( void print(int b[ ], int n) { for(int i=0; i< n; ++i) cout <<" "<< b[i] ; // print each in b[] cout << endl; }//print(int [ ] /****************** C:\testc\STL>g++ aList.cpp C:\testc\STL>a Original list a: 55 88 38 49 58 a.sort( ): 38 49 55 58 88 with myCompf: 88 58 55 49 38 Pass original x[ ] to testAry.. === testAry === 85 55 88 38 49 38 49 55 85 88 In testAry, new y== 85 55 88 38 49 58 66 === after testAry( ): 55 88 38 49 58 C:\testc\STL> ********************************/