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

使用STL map 用 string 做索引 插入删除数据的更多相关文章

  1. 为什么查询出来的数据保存到Arraylist?插入删除数据为啥用LinkedList?

    引言:这是我在回答集合体系时,被问到的一个问题,也是因为没有深入学习所以回答的并不是很好,所以这两天看了一下,以下是我的一些回答与学习方法. 学习方法:我们学习,系统性的学习肯定是比零散的学习更有效的 ...

  2. stl map底层之红黑树插入步骤详解与代码实现

    转载注明出处:http://blog.csdn.net/mxway/article/details/29216199 本篇文章并没有详细的讲解红黑树各方面的知识,只是以图形的方式对红黑树插入节点需要进 ...

  3. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  4. map, string 强大的STL

    hdu 1247  Hat's Words Input Standard input consists of a number of lowercase words, one per line, in ...

  5. STL map详细用法和make_pair函数

    今天练习华为上机测试题,遇到了map的用法,看来博客http://blog.csdn.net/sprintfwater/article/details/8765034:感觉很详细,博主的其他内容也值得 ...

  6. STL——map

    看到map这里,都不知道它主要是干嘛的,你有没有这样的疑问. map的主要作用:提供对T类型的数据进行快速和高效的检索 .C++ STL中标准关联容器set, multiset, map, multi ...

  7. STL map 用法

    首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象  class pair可以将两个值视为一个单元.容器类别map和mul ...

  8. C++ STL map详解

    一.解释: p { margin-bottom: 0.25cm; direction: ltr; color: #00000a; line-height: 120%; text-align: just ...

  9. STL map 详细用法

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个称为该关键字的值)的数据 处理能力. 需要的库 #include <map> ...

随机推荐

  1. linux里的vi怎么移动到最后一行

    下面是几个vi与行移动有关的命令: G:光标移至最后一行 nG:光标移至第n行首 n+:光标下移n行 n-:光标上移n行 注意输入命令,需要首先按ESC键回到命令模式. 转自: http://zhid ...

  2. Serialize and Deserialize Binary Tree

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

  3. 六间房 繁星 酷我 来疯 秀吧 新浪秀 直播播放器 Live 1.2

    适合用于进行录制的时候 特别说明: 安装 falsh play 19 时 不能正常播放 每个按钮都有提示,不详细说明 下载地址 http://pan.baidu.com/s/1i32ETIt 下载地址 ...

  4. POJ 2478 Farey Sequence(欧拉函数前n项和)

    A - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  5. 设置IIS会话过期时间

    打开默认网站----双击ASP--展开会话属性---更改超时时间-

  6. mysql_4(解决中文乱码问题)

    mysql> create database if not exists xdb default character set utf8;Query OK, 1 row affected (0.0 ...

  7. WebService之CXF框架

    本文主要包括以下内容 ant工具的使用 利用cxf实现webservice cxf与spring整合 ajax访问webservice ant 工具 1.为什么要用到ant这个工具呢? Ant做为一种 ...

  8. Java创建Web项目

    首先下载Tomcat服务,用来运行JAVA程序,跟windows中的IIS类似 下载地址:tomcat.apache.org ,最好下载ZIP压缩版的,解压后就可以直接用.如下图: 检查Tomcat是 ...

  9. SQL中行列转换Pivot

    --建表 ),课程 ),分数 int) --插入数据 ) ) ) ) ) ) 1.静态行转列(确定有哪些列) select 姓名, end)语文, end)数学, end)物理 from tb gro ...

  10. 设计模式学习之原型模式(Prototype,创建型模式)(5)

    通过序列化的方式实现深拷贝 [Serializable] public class Person:ICloneable { public string Name { get; set; } publi ...