好久木有用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插入结构体