C + + Map Custom comparison function

Source: Internet
Author: User

#include <iostream>#include<map>using namespacestd;voidFuninta[]) {a[0] = A;}structcompare{BOOL operator()(Const Char* S1,Const Char* S2)Const    {        returnstrcmp (S1, S2) <0; }};intMain () {map<Char*,Char*, compare>student; Charid1[ One] ="1395682451"; Charname1[ -] ="xiaoming"; Student.insert (Pair<Char*,Char*>(ID1, name1)); cout<< Student[id1] <<Endl; Charid2[ One] ="1395682451"; Charname2[ -] ="Lisi"; Student.insert (Pair<Char*,Char*>(Id2, name2)); cout<< Student[id2] <<Endl; STUDENT[ID1]=name2; cout<< Student[id2] <<Endl; Map<Char*,Char*>teacher; Teacher.insert (Pair<Char*,Char*>(ID1, name1)); Teacher.insert (Pair<Char*,Char*>(Id2, name2)); cout<<"Teacher 1 is:"<< Teacher[id1] <<Endl; cout<<"Teacher 2 is:"<< Teacher[id2] <<Endl; intb[2] = {1,2 };    Fun (b); cout<< b[0] <<Endl; return 0;}

The default is to use a memory address comparison, the above teacher is using the default comparison method, you will find that although the ID1 and ID2 content exactly the same, but still inserted successfully.

Student uses the string content comparison, insert operation insertion was unsuccessful because the same string was encountered, so returns, which is not overwritten by the original value, if the subscript override method is used, the original values will be overwritten.

C + + Map Custom comparison function

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.