[STL] inserter and front_inserter usage

Source: Internet
Author: User
# 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...

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.