首页 > 代码库 > map 使用及map中数据更新测试
map 使用及map中数据更新测试
#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. 需要小于号的重载,用于map中,key的默认排序
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 insert 成功失败的判断方法
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<<"成功"<<std::endl;
}
else{
std::cout<<"失败"<<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 更新map中的data,需要用at命令,不能用insert。
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;
}
#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. 需要小于号的重载,用于map中,key的默认排序
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 insert 成功失败的判断方法
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<<"成功"<<std::endl;
}
else{
std::cout<<"失败"<<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 更新map中的data,需要用at命令,不能用insert。
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;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。