c++中map容器簡易功能實現測試附帶結構體指標測試

來源:互聯網
上載者:User
// TestingandTobe.cpp : 定義控制台應用程式的進入點。//#include "stdafx.h"#include<iostream>#include<map>#include<string>using namespace std;struct student{// virtual void fun(){  指標指向未知崩潰    void fun(){m_i=0;cout<<"結構體指標調用結構體中的函數測試,...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(_A.nID < _B.nID) return true; if(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0; return false; } };int _tmain(int argc, _TCHAR* argv[]){/*************測試函數指標*************/student *m_p=0;m_p->fun();/*************測試map容器**************///數組方式插入資料map<int,string> mapStudent;mapStudent[3]="student one";mapStudent[1]="student two";mapStudent[2]="student three";map<int,string>::iterator iter;for(iter=mapStudent.begin();iter!=mapStudent.end();iter++){cout<<"insert test..."<<iter->second<<endl;}//測試尋找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.equal_range(2);if(mappair.first==mappair.second){cout<<"donot find"<<endl;}else{cout<<"FindTest...find"<<endl;}    //insert函數插入資料/*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(Insert_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; } *//**************************  測試排序***************************///用學生資訊映射分數 int nSize;map<StudentInfo, int, sort>mapStudentt ; map<StudentInfo, int, sort>::iterator iterrr;StudentInfo studentInfo; 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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.