Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2653582
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-03-25 14:26:52

最近在做XML的解析方面的,要用到std::map,在此转载,以便将来查询便利


1. map中的元素其实就是一个pair.
2. map的键一般不能是指针, 比如int*, char*之类的, 会出错. 常用的就用string了,int也行.
3. map是个无序的容器, 而vector之类是有序的. 所谓有序无序是指放入的元素并不是按一定顺序放进去的, 而是乱序, 随机存放的(被映射后近似随机存放).所以遍历的时候有些效率差别.
4. 判断有没有找到该键的内容可以这样:
std::map::const_iterator cIter;
cIter = stdfile.m_map.find(s);
if (cIter == stdfile.m_map.end()) // 没找到就是指向END了   
{
m_vecMoreFile.push_back(s);
}
如果键的内容是指针的话, 应该用NULL指针也可以判断了.
5. 遍历:
std::map::iterator iter;
for (iter = m_map.begin(); iter != m_map.end(); iter++)
{
std::string s = iter->second.filename;
}
由于map内容可以相当一个PAIR, 那就简单了, 用iter->second就可以取得值了.

可顺便转个其它的几种用法:
1 头文件
#include

2 定义
map my_Map;
或者是typedef map MY_MAP;
MY_MAP my_Map;

3 插入数据
(1) my_Map["a"] = 1;
(2) my_Map.insert(map::value_type("b",2));
(3) my_Map.insert(pair("c",3));
(4) my_Map.insert(make_pair("d",4));

4 查找数据和修改数据
(1) int i = my_Map["a"];
my_Map["a"] = i;
(2) MY_MAP::iterator my_Itr;
my_Itr.find("b");
int j = my_Itr->second;
my_Itr->second = j;
不过注意,键本身是不能被修改的,除非删除。

5 删除数据
(1) my_Map.erase(my_Itr);
(2) my_Map.erase("c");
还是注意,第一种情况在迭代期间是不能被删除的,道理和foreach时不能删除元素一样。

6 迭代数据
for (my_Itr=my_Map.begin(); my_Itr!=my_Map.end(); ++my_Itr) {}

7 其它方法
my_Map.size() 返回元素数目
my_Map.empty() 判断是否为空
my_Map.clear() 清空所有元素
可以直接进行赋值和比较:=, >, >=, <, <=, != 等等

遍历:

#include
#include
using namespace std;

int main(){
 mapM;
 M[1]=2;
 M[2]=3;
 map::iterator iter;
 for (iter = M.begin(); iter != M.end(); iter++)
 {
  cout<first<<" "<second< }
 return 0;
}

 通常在使用STL时,除了List和vector,我们还会经常用到Map。
Map使用关键值_Key来唯一标识每一个成员_Tp

STL中的Map声明如下:
template <
	class _Key, class _Tp, 
	class _Compare = less<_Key>,
	class _Alloc = allocatorconst _Key, _Tp> > >
class map
{
...
}

其中_Compare是用来对_Tp进行排序用的,以便提高查询效率。
缺省是less<_Key>
原型如下:
template <class _Tp>
struct less : public binary_function<_Tp,_Tp,bool>
{
	bool operator()(const _Tp& __x, const _Tp& __y) const 
  	{ return __x < __y; }
};

它是一个Functor,留给Map排序用。不过平时我们也可以使用它:
例:
std::less MyLess;
bool bRetVal = MyLess(3, 12);
cout << ((bRetVal == true) ? "Less" : "Greater") << endl;

OK,下面就来示范一个:

#include
#include
 
using namespace std;
 
typedef map > M_TYPE;
typedef M_TYPE::iterator M_IT;
typedef M_TYPE::const_iterator M_CIT;
 
int main()
{
	M_TYPE MyTestMap;
	
	MyTestMap[3] = "No.3";
	MyTestMap[5] = "No.5";
	MyTestMap[1] = "No.1";
	MyTestMap[2] = "No.2";
	MyTestMap[4] = "No.4";
	
	M_IT it_stop = MyTestMap.find(2);
	
	cout << "MyTestMap[2] = " << it_stop->second << endl;
	it_stop->second = "No.2 After modification";
	cout << "MyTestMap[2] = " << it_stop->second << endl;
	
	cout << "Map contents : " << endl;
	for(M_CIT it = MyTestMap.begin(); it != MyTestMap.end(); it++)
	{
		cout << it->second << endl;
	}
	
	return 0;
}

程序输出:

MyTestMap[2] = No.2
MyTestMap[2] = No.2 After modification
Map contents :
No.1
No.2 After modification
No.3
No.4
No.5

可见Map已经根据less对各个成员_Tp进行从小到大进行排了序;
同理,若我们不使用less,改而使用greater,则Map将成员从大到小排序。

/****************CMAP用法*********************/
#include "stdafx.h"
#include
using namespace std;
class Point
{
public:
 Point()
 {
  m_x = 0;
  m_y = 0;
 }
 Point(int x, int y)
 {
  m_x = x;
  m_y = y;
 }
public:
 int m_x;
 int m_y;
};
typedef CMap     CMapPnt; //请在使用之前定义
int main()
{
 Point elem1(1, 100), elem2(2, 200), elem3(3, 300), point;
 CMapPnt mp;
 // insert 3 elements into map,          #1
 mp.SetAt("1st", elem1);
 mp.SetAt("2nd", elem2);
 mp.SetAt("3th", elem3);
 // search a point named "2nd" from map                  #2
 mp.Lookup("2nd", point);
 printf("2nd: m_x: %d, m_y: %d\n", point.m_x, point.m_y);
 // insert a new pair into map      #3
 Point elem4(4, 400);
 mp["4th"] = elem4;
 cout<<"count: "< // traverse the entire map                    #4
 size_t index = 0;
 const char* pszKey;
 POSITION ps = mp.GetStartPosition();
 while( ps )
 {  
  mp.GetNextAssoc(ps, pszKey, point);
  printf("index: %d, m_x: %d, m_y: %d\n", ++index, point.m_x, point.m_y);
 }
 return 0;
}
 
阅读(2285) | 评论(0) | 转发(0) |
0

上一篇:CComVariant相关(转)

下一篇:Qt编程点积

给主人留下些什么吧!~~