使用STL map 用 string 做索引 插入删除数据
2024-08-26 07:20:37
1、代码
#include <map>
#include <string>
#include <stdio.h>
#include <vector>
#include <stdlib.h> using namespace std; class A
{
public:
int m_iX; public :
A();
A(int i);
~A();
}; A::A()
{
} A::A(int i)
{
m_iX = i;
} A::~A()
{
} typedef map<string, A *> MapA; void dumpMap(MapA & xMapA)
{
for(MapA::iterator pA = xMapA.begin(); pA != xMapA.end(); pA++)
{
printf("index : %s , value : %3d\n", pA->first.c_str(), pA->second->m_iX);
}
printf("-----------------------------------\n");
} int testOne()
{
printf("%s %d %s()\n", __FILE__, __LINE__, __func__); MapA xMapA; for(int i = ; i < ; i++)
{
A * a = new A(i);
char szIndex[] = {};
snprintf(szIndex, sizeof(szIndex), "%03d", i);
string strIndex = szIndex;
xMapA.insert(make_pair(strIndex, a));
}
dumpMap(xMapA);
printf("map size : %d\n", xMapA.size());
for(int i = ; i < ; i+=)
{
A * a = new A(i);
char szIndex[] = {};
snprintf(szIndex, sizeof(szIndex), "%03d", i);
string strIndex = szIndex;
MapA::iterator ppA = xMapA.find(strIndex);
if(ppA == xMapA.end())
{
printf("can not find : [%s]\n", strIndex.c_str());
continue;
}
xMapA.erase(ppA);
}
dumpMap(xMapA); printf("map size : %d\n", xMapA.size()); return ;
} int main(int argc, char * argv[])
{
testOne();
return ;
}
2、执行结果
./test-map
test-map.cpp testOne()
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
-----------------------------------
hash size :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
index : , value :
-----------------------------------
hash size :
over
最新文章
- ZBrush该如何通过结合KeyShot制作逼真玉佩
- 基于DDD的.NET开发框架 - ABP分层设计
- 适用于Magento的最合适的.htaccess写法
- C# winform程序怎么打包成安装项目(图解)
- 重拾C,一天一点点_11
- 【BZOJ】【2661】【Beijing WC2012】连连看
- CreateProcessWithLogonW(好像可以指定进程的上下文环境)
- hdu1233(最小生成树)
- 学习javascript基础知识系列第二节 - this用法
- 浅谈SharePoint 2013 站点模板开发 转载自http://www.cnblogs.com/jianyus/p/3511550.html
- PHP读取大文本文件并处理数据的思路
- PHP类自动加载技术
- cf A Simple Task---线段树
- .NET 使用 Azure Blob 存储图片或文件
- logging模块使用
- 基于matplotlib的数据可视化 - 热图imshow
- Solr相似度算法一:DefaultSimilarity(基于TF-IDF的默认相似度算法)
- 一头扎进 Java IO中-------java IO文件
- $Python技巧大全
- window server 搭建git服务器
热门文章
- TFS和VSS的简单对比
- 43. 动态规划求解n个骰子的点数和出现概率(或次数)[Print sum S probability of N dices]
- 22.整数二进制表示中1的个数[Get1BitCount]
- Solr5.3.1通过copyField设置多个field(字段)同时检索
- mysql show
- [Android Pro] fragment中嵌套viewpager,vierpager中有多个fragment,不显示
- EZ的间谍网络(codevs 4093)
- LinuxC语言读取文件,分割字符串,存入链表,放入另一个文件
- iOS开发网络篇—网络请求(HTTP协议)小结(转)
- Codeforces Round #360 (Div. 2) E. The Values You Can Make DP