使用STL map 用 string 做索引 插入删除数据
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 做索引 插入删除数据的更多相关文章
- 为什么查询出来的数据保存到Arraylist?插入删除数据为啥用LinkedList?
引言:这是我在回答集合体系时,被问到的一个问题,也是因为没有深入学习所以回答的并不是很好,所以这两天看了一下,以下是我的一些回答与学习方法. 学习方法:我们学习,系统性的学习肯定是比零散的学习更有效的 ...
- stl map底层之红黑树插入步骤详解与代码实现
转载注明出处:http://blog.csdn.net/mxway/article/details/29216199 本篇文章并没有详细的讲解红黑树各方面的知识,只是以图形的方式对红黑树插入节点需要进 ...
- POJ 3096 Surprising Strings(STL map string set vector)
题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...
- map, string 强大的STL
hdu 1247 Hat's Words Input Standard input consists of a number of lowercase words, one per line, in ...
- STL map详细用法和make_pair函数
今天练习华为上机测试题,遇到了map的用法,看来博客http://blog.csdn.net/sprintfwater/article/details/8765034:感觉很详细,博主的其他内容也值得 ...
- STL——map
看到map这里,都不知道它主要是干嘛的,你有没有这样的疑问. map的主要作用:提供对T类型的数据进行快速和高效的检索 .C++ STL中标准关联容器set, multiset, map, multi ...
- STL map 用法
首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象 class pair可以将两个值视为一个单元.容器类别map和mul ...
- C++ STL map详解
一.解释: p { margin-bottom: 0.25cm; direction: ltr; color: #00000a; line-height: 120%; text-align: just ...
- STL map 详细用法
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个称为该关键字的值)的数据 处理能力. 需要的库 #include <map> ...
随机推荐
- Unique Paths | & ||
Unique Paths I A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diag ...
- js 函数声明方式以及javascript的历史
1.function xx(){} 2.匿名方式 window.onload=function(){dslfjdslfkjdslf}; 3.动态方式 var demo=new Function ...
- cc.game
概述 使用cc.game单例代替了原有的cc.Application以及cc.AppControl. cc.game是Cocos2d-JS的游戏对象,主要职责包括,配置的读取,引擎的加载,游戏脚本的加 ...
- 【USACO】ariprog
输入 : N M 要找到长度为 N 的等差数列,要求数列中每个数字都可以表达成 a^2 + b^2 的和, 数字大小不超过M^2 + M^2 输出: 等差数列首元素 间隔 (多组答案分行输出) 解题 ...
- Win8 Cisco VPN Client 442错误解决办法
进入注册表regedit,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\CVirtA找到DisplayName, x86系统的将值" ...
- 基因变异(codevs 3194)
题目描述 Description 小毛终于来到了冥王星,这是一颗已经不属于行星的矮行星,它的表面温度低于-220度.在这里,小毛惊奇的发现,他带来的厌氧菌开始了基因变异,裂变的速度与光照时间(秒)成乘 ...
- 使用webstorm调试node程序
前言 相信大家接触过不少node代码了,如果你应用的比较初级或者针对你的项目不需要接触过深的node代码,也许你仅仅需要简单的console.log('your variable')就完全满足你的需要 ...
- swift复合类型
1.元组类型 (tuple) 元组就是多个元素的组合,是一个用圆括号括起来分号分隔的多个数据的一个集合体. 例如:定义一个学生变量,要求姓名 jim,年龄 19,性别 male 的元组变量为 // ...
- Spring中的设计模式学习
Spring提供了一种Template的设计哲学,包含了很多优秀的软件工程思想. 1. 简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. ...
- 《Java并发编程实战》学习笔记 线程安全、共享对象和组合对象
Java Concurrency in Practice,一本完美的Java并发参考手册. 查看豆瓣读书 推荐:InfoQ迷你书<Java并发编程的艺术> 第一章 介绍 线程的优势:充分利 ...