Map usage and data update test in Map

Source: Internet
Author: User
# Include <map>
# Include <vector>
# Include <iostream>


Using namespace STD;


Struct urlkeystru
{
Int iurlid;
String strurl;
};


Struct traceip
{
String IP;
Int ittl;
};


Struct urlresdatastru
{
Int isuccdialcount;
Int iblockcount;
Vector <traceip> vectraceip;
};


// 1. requires an overload smaller than the number, which is used for the default sorting of keys in map.
Bool operator <(const urlkeystru & struurlkey1, const urlkeystru & struurlkey2)
{
If (struurlkey1.iurlid <struurlkey2.iurlid)
Return true;
Else
Return false;
}


Int main (){


Urlkeystru key = {1, "Hello \ n "};
Urlresdatastru data;
STD: Map <urlkeystru, urlresdatastru> ll;

Data. isuccdialcount = 1;
// LL. insert (STD: pair <urlkeystru, urlresdatastru> (Key, data ));
Ll. insert (STD: make_pair (Key, data ));


// 2 Method for judging successful insert failures
Data. isuccdialcount = 2;
STD: pair <STD: Map <urlkeystru, urlresdatastru >:: iterator, bool> ret;
Ret = ll. insert (STD: pair <urlkeystru, urlresdatastru> (Key, data ));

If (Ret. Second ){
STD: cout <"succeeded" <STD: Endl;
}
Else {
STD: cout <"failed" <STD: Endl;
}


STD: Map <urlkeystru, urlresdatastru >:: iterator it;

For (IT = ll. Begin (); it! = Ll. End (); It ++)
{
STD: cout <"key. iurlid = "<(IT-> first ). iurlid <"--> value. isuccdialcount = "<(IT-> second ). isuccdialcount <STD: Endl;
}


// 3 update the data in the map. The at command must be used, and the INSERT command cannot be used.
Data. isuccdialcount = 3;
Ll. At (key) = data;


For (IT = ll. Begin (); it! = Ll. End (); It ++)
{
STD: cout <"key. iurlid = "<(urlkeystru) (IT-> first )). iurlid <"--> value. isuccdialcount = "<(urlresdatastru) (IT-> second )). isuccdialcount <STD: Endl;
}


Return 0;
}

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.