C + + Map

Source: Internet
Author: User

#include <map> #include <iostream>using namespace Std;class person{public:int age;int ID; person (int age, int id) {This->age = Age;this->id = ID;}};   struct Cmpbykey {bool operator () (const person * k1, const person * K2) {return K1->age < k2->age;  }  }; Map<person *, int, cmpbykey> imap;int main () {person * pperson;for (int i = ten; i > 0; i--) {Pperson = new person ( I, I); Imap.insert (Pair<person *, int> (Pperson, i));} Map<person *,int, Cmpbykey>::iterator ibegin = Imap.begin (); Map<person *,int, Cmpbykey>::iterator iEnd = Imap.end (); while (ibegin! = iend) {cout<<ibegin->first->id << ":"; cout << ibegin->first- >age << endl;ibegin++;} cout<< "####################" <<endl;pperson = new Person (1, ten);p Air<map<person *, int, cmpbykey> :: iterator, bool> insert_pair;insert_pair = Imap.insert (Pair<person *, int> (Pperson)); if (Insert_ Pair.second = = true) {Cout<<"    Insert successfully "<<endl;    } else {cout<< "Insert Failure" <<endl; }ibegin = Imap.begin (); while (ibegin! = iend) {cout<<ibegin->first->id << ":"; cout << ibegin- >first->age<<endl;ibegin++;}}

  

C + + Map

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.