# Include <iostream> # include <algorithm> # include <list> using namespace STD; int iarray [5] = {1, 2, 3, 4, 5 }; void display (list <int> & A, const char * s) {cout <S <Endl; copy (. begin (),. end (), ostream_iterator <int> (cout, ""); cout <Endl ;}int main () {list <int> ilist; copy (iarray, iarray + 5, front_inserter (ilist); display (ilist, "Before find and copy"); List <int >:: iterator P = find (ilist. begin (), ilist. end (), 3); copy (iarray, iarray + 2, inserter (ilist, p); display (ilist, "After find and copy"); Return 0 ;}
Before find and copy
5 4 3 2 1
After find and copy
5 4 1 2 3 2 1
Press any key to continue...
# Include <iostream> # include <algorithm> # include <vector> using namespace STD; Class myclass {public: Friend ostream & operator <(ostream &, const myclass &); myclass (int A, int B): First (a), second (B) {} int first; int second; bool operator <(const myclass & M) const {return first <m. first ;}}; // reloadostream & operator <(ostream & out, const myclass & Mc) {out <MC. first <"" <MC. second <Endl; Return out;} bool less_second (const myclass & M1, const myclass & m2) {return m1.second <m2.second;} void display (vector <myclass> & V, const char * s) {cout <Endl <S <Endl; copy (v. begin (), V. end (), ostream_iterator <myclass> (cout, ""); cout <Endl ;}int main () {int I = 0; vector <myclass> VCT; for (I = 0; I <10; I ++) {myclass my (10-i, I * 3); VCT. push_back (my);} display (VCT, "initial"); sort (VCT. begin (), VCT. end (); // display (VCT, "After sorted by the first:"); sort (VCT. begin (), VCT. end (), less_second); // display (VCT, "After sorted by the second:"); System ("pause"); Return 0 ;}
Initial
10 0
9 3
8 6
7 9
6 12
5 15
4 18
3 21
2 24
1 27
After sorted by the first:
1 27
2 24
3 21
4 18
5 15
6 12
7 9
8 6
9 3
10 0
After sorted by the second:
10 0
9 3
8 6
7 9
6 12
5 15
4 18
3 21
2 24
1 27
Press any key to continue...