In C ++, the simple function of map container is used to test the attach struct pointer.

Source: Internet
Author: User
// Testingandtobe. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <iostream> # include <map> # include <string> using namespace STD; struct student {// virtual void fun () {Pointer Points to unknown crash void fun () {m_ I = 0; cout <"struct pointer calls the function test in the struct ,... m_ I = "<m_ I <Endl;} static int m_ I;}; int Student: m_ I = 0; typedef struct tagstudentinfo {int NID; string strname;} studentinfo, * pstudentinfo; Class sort {public: bool operator () (studentinfo const & _ A, studentinfo const & _ B) Const {If (_. NID <_ B. NID) return true; If (_. nid = _ B. NID) return _. strname. compare (_ B. strname) <0; return false ;}}; int _ tmain (INT argc, _ tchar * argv []) {/************** test function pointer *************/student * m_p = 0; m_p-> fun (); /************** test map container *************** // insert Data Map in array mode <int, string> mapstudent; mapstudent [3] = "student one"; mapstudent [1] = "student two"; mapstudent [2] = "student three"; Map <int, string >:: ite Rator ITER; For (iter = mapstudent. Begin (); iter! = Mapstudent. end (); ITER ++) {cout <"insert test... "<ITER-> second <Endl ;}// test the Map <int, string >:: iterator iterr; iterr = mapstudent. lower_bound (2); {cout <"findtest... "<iterr-> second <Endl;} iterr = mapstudent. upper_bound (2); {cout <"findtest... "<iterr-> second <Endl;} pair <Map <int, string >:: iterator, Map <int, string >:: iterator> mappair; mappair = mapstudent. pai_range (2); If (mappair. first = mappair. second) {cout <" Donot find "<Endl;} else {cout <" findtest... find "<Endl ;}// insert function inserts data/* Map <int, string> mapstudent; pair <Map <int, string >:: iterator, bool> insert_pair; insert_pair = mapstudent. insert (pair <int, string> (1, "student one"); If (insert_pair.second = true) {cout <"insert successfully" <Endl ;} else {cout <"insert failure" <Endl;} insert_pair = mapstudent. insert (pair <int, string> (2, "student two"); If (in Sert_pair.second = true) {cout <"insert successfully" <Endl;} else {cout <"insert failure" <Endl;} Map <int, string> :: iterator ITER; For (iter = mapstudent. begin (); iter! = Mapstudent. end (); ITER ++) {cout <ITER-> first <"" <ITER-> second <Endl ;} ********* * **************** // use the student information to map the score to int nsize; map <studentinfo, Int, sort> mapstudentt; Map <studentinfo, Int, sort >:: iterator iterrr; studentinfo. nid = 1; studentinfo. strname = "student_one"; mapstudentt. insert (pair <studentinfo, int> (studentinfo, 90); studentinfo. Nid = 2; studentinfo. strname = "student_two"; mapstudentt. insert (pair <studentinfo, int> (studentinfo, 80); For (iterrr = mapstudentt. begin (); iterrr! = Mapstudentt. End (); iterrr ++) cout <"test order..." <iterrr-> second <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.