您的位置:首页 > 其它

有关容器的一点小小总结

2011-08-31 00:25 441 查看
本文并非是对c++容器的用法详解,只是把自己这几天用到vector及map时,出现的一些小问题在此记录下,以备不时只需。。。。。。

1.定义容器之前需在头文件中:

#include <map>

#include <vector>

using namespace std;

2.vc6.0中如果添加:#pragma warning(disable:4786)则可以避免出现很多警告信息

3.第一次接触<algorithm>中find函数用法

for(itermap=map.begin();itermap!=map.end();itermap++)

{

if (itermap->second.size() > 0)

{

iterVector = find(itermap->second.begin(),itermap->second.end(),strFind);

if (iterVector != itermap->second.end())

{

iMapPosition = itermapLightInfo->first;

}

}

}

感觉这种方法还是很方便用的(虽然有很多种方法可以实现类似的查找功能)

4.函数用法AfxExtractSubString(strTemp,strValue,iSubString++,'-');//从strValue中按-来一段一段截取字符串,如:abc-de-f,iSubstring等于1时,strTemp则为abc

5.如何判断_variant_t类型变量是否为空

_variant_t varTemp;

if (varTemp.vt == VT_NULL || varTemp.vt == VT_EMPTY)

为空;

6.三种插入数据的方法:

第一种:用insert函数插入pair数据,下面举例说明(以下代码虽然是随手写的。//此例摘抄自:http://hi.baidu.com/gaj19870311/blog/item/12acf94c6557e807b2de057d.html
Map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, “student_one”));
mapStudent.insert(pair<int, string>(2, “student_two”));
mapStudent.insert(pair<int, string>(3, “student_three”));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
第二种:用insert函数插入value_type数据,下面举例说明
Map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
mapStudent.insert(map<int, string>::value_type (2, “student_two”));
mapStudent.insert(map<int, string>::value_type (3, “student_three”));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
cout<<iter->first<<” ”<<iter->second<<end;
第三种:用数组方式插入数据
Map<int, string> mapStudent;
mapStudent[1] = “student_one”;
mapStudent[2] = “student_two”;
mapStudent[3] = “student_three”;
区别:第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的;但是用数组方式就不同了,它可以覆盖以前该关键字对应的值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: