WINCE STL使用回忆

来源:互联网 发布:mac粉底液nc20 编辑:程序博客网 时间:2024/05/17 22:24

好久木有用STL的东东了,现在弄几个经常需要注意的地方

1:MAP插入数据

 ///##########################

 #include <map>

 #include <vector>
 #include <string>

 #include <iostream>

 using namespace std;
 #define TSTRING std::wstring
 //////#########################

 BOOL Cwince_stl_map_testDlg::OnInitDialog()
 {
 CDialog::OnInitDialog();

 map<int, wstring> mapStudent;

 pair<map<int, wstring>::iterator, bool> Insert_Pair;
 //
 CString  II=L"我和你";
 CString III=L"心连心";

 TSTRING strValuehmjhmjII =     (TSTRING )   II;
 TSTRING strValuehmjhmjIII =     (TSTRING )   III;
 //Insert_Pair = mapStudent.insert(pair<int, TSTRING>(1, strValuehmjhmjII));
 //mapStudent.insert(pair<int, wstring>(2, strValuehmjhmjIII));

   mapStudent.insert(map<int,wstring>::value_type (1, strValuehmjhmjII));

   mapStudent.insert(map<int,wstring>::value_type (2, strValuehmjhmjIII));

 Insert_Pair =  mapStudent.insert(map<int,wstring>::value_type (2, strValuehmjhmjIII));//这个用来判断MAP插入对象是否成功,很有用,以前竟然没有用过,悲催

 if(Insert_Pair.second == true)
 {
  RETAILMSG(TRUE, (TEXT("==cha ru cheng gong ==\r\n")));
 }
 else
 { 
    RETAILMSG(TRUE, (TEXT("==cha ru shi bai ==\r\n"))); 

 }

 map<int, wstring>::iterator iter;

 for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

 {
 RETAILMSG(1,(L"jiexishijian1===== %d\r\n" ,(iter->first)   ));
 RETAILMSG(1,(L"jiexishijian2===== %s\r\n" ,(iter->second).c_str()    ));

 // RETAILMSG(TRUE, (TEXT("==555==\r\n"))); 
 // RETAILMSG(1,(L"jiexishijian===== %s\r\n" ,mapStudent[0].c_str()   ));
 //RETAILMSG(1,(L"jiexishijian1===== %s\r\n" ,mapStudent[1].c_str()   ));
 //RETAILMSG(1,(L"jiexishijian2===== %s\r\n" ,mapStudent[2].c_str()   ));
 }
 int nSize = mapStudent.size();
 RETAILMSG(1,(L"mapStudent.size===== %d\r\n" ,nSize  ));
 return TRUE;  // return TRUE  unless you set the focus to a control
 }

//###################################MAP插入一个结构体

 //////#########################
 struct SourceKey {
  long nUserId;
  int iSence;
 };

 bool operator==(SourceKey& a,SourceKey& b) {
  if ((a.nUserId == b.nUserId) && (a.iSence == b.iSence)) {
   return true;
  }

  return false;
 }
 bool operator<(const SourceKey& a,const SourceKey& b)
 {
  return a.nUserId < b.nUserId;
 }

 

 


 //###################

 

///////////////////////

 void Cwince_stl_map_testDlg::OnBnClickedButton1()
 {
  map<int, SourceKey> mapStudent;
     map<int, SourceKey>::iterator iter;
  pair<map<int, SourceKey>::iterator, bool> Insert_Pair;
  SourceKey key1;
  SourceKey key2;
  key1.nUserId = 1221;
  key1.iSence = 1;
  key2.nUserId = 122222;
  key2.iSence = 2;

  mapStudent.insert(make_pair(1, key1));
  mapStudent.insert(make_pair(2, key2));
  mapStudent.insert(make_pair(3, key2));

  for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

  {
   RETAILMSG(1,(L"shan chu zhi hou ===== %d\r\n" ,(iter->first)   ));
   RETAILMSG(1,(L"shan chu zhi hou2===== %d\r\n" ,(iter->second).nUserId    ));
  
  }
 }

//###################################################

2:STL MAP插入结构体

  1. struct A{   
  2.  string name;   
  3.  double value;   
  4. };   
  5.   
  6. class U{   
  7. public:   
  8.   map<int, A> param;   
  9.   ....................   
  10.   void setParam(int, A);   
  11.   ..................   
  12. };   
  13. void U:: setParam(int s, A pa){   
  14.   param.insert(pair<int,struct A>(s, pa));   //注意主要关注这个,如果没有这个强制STUCT就出问题了。。。。
  15. }   
  16.   
  17. class M{   
  18. public:   
  19.   U multi[20];   
  20.   ...................................   
  21.  void initM();   
  22. };   
  23.   
  24. void M:: initM(){   
  25.  for(int i=0; i<20; i++){   
  26.     string someStr;   
  27.     A initPa = {someStr, 0.0};   
  28.     multi[i].setParam(i, initPa);   
  29.  }   
原创粉丝点击