Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 使用STL中的泛型算法及函数对象
【标  题】:使用STL中的泛型算法及函数对象
【关键字】:STL
【来  源】:http://blog.csdn.net/roger_77/archive/2006/10/10/1328600.aspx

使用STL中的泛型算法及函数对象

Your Ad Here

使用了vector容器,remove_if(), for_each()等算法,并使用到了函数对象。

vector容器中存放的是多个字段的结构体,然后根据结构体的某个字段删除其在容器中的元素。

// 主要是用于练习使用STL中的泛型算法

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
/// 结构体
struct MyStruct
{
 int nID;
 string strName;
// string strDesc;
};

class CMyStructComp
{
private:
 string m_strName;
public:
 CMyStructComp(const string strName):m_strName(strName)
 {}
 bool operator()(const MyStruct &data)const
 {
  //if (m_strName == data.strName)
  //{
  // return true;
  //}
  //return false;
  return (m_strName == data.strName)?true:false;
 }
};
// 为减少string对象的构造及析构,使用字符串指针
class CMyStructComp2
{
private:
 char* m_pName;
public:
 CMyStructComp2(const char* pName)
 {
  m_pName = (char*) pName;
 }
 bool operator()(const MyStruct &data)const
 {
  if (!strcmp(m_pName, data.strName.c_str()))
  {
   return true;
  }
  return false;
 }
};
void printMyStruct(const MyStruct &data)
{
 std::cout <<"ID:"<< data.nID << ",Name:" << data.strName << std::endl;
}
 
std::vector<MyStruct > vecData;
void insertData()
{
 MyStruct myData;
 myData.nID = 1;
 myData.strName = "aaaaa";
 vecData.push_back(myData);
 myData.nID = 2;
 myData.strName = "bbbbb";
 vecData.push_back(myData);
 myData.nID = 3;
 myData.strName = "CCCCC";
 vecData.push_back(myData);
 myData.nID = 4;
 myData.strName = "ddddd";
 vecData.push_back(myData);
 myData.nID = 5;
 myData.strName = "EEEEE";
 vecData.push_back(myData);
}

int _tmain(int argc, _TCHAR* argv[])
{
 insertData();
 // 打印vector中所有的数据
 for_each(vecData.begin(),vecData.end(),printMyStruct);
 string strName("ddddd");
 // 删除vector中符合条件的数据
 vecData.erase(remove_if(vecData.begin(),vecData.end(),CMyStructComp(strName)),vecData.end());
 std::cout <<"删除元素后:"<< std::endl ;
 // 打印vector中所有的数据
 for_each(vecData.begin(),vecData.end(),printMyStruct);

 strName = "bbbbb";
 // 删除vector中符合条件的数据
 vecData.erase(remove_if(vecData.begin(),vecData.end(),CMyStructComp2(strName.c_str())),vecData.end());
 std::cout <<"删除元素后:"<< std::endl ;
 // 打印vector中所有的数据
 for_each(vecData.begin(),vecData.end(),printMyStruct);
 strName = "aaaaa";
 // 删除vector中符合条件的数据
 std::vector<MyStruct >::iterator itr;
 itr = remove_if(vecData.begin(),vecData.end(),CMyStructComp2(strName.c_str()));
 size_t nSize = 0;
 size_t nCapacity = 0;
 nSize = vecData.size();
 nCapacity = vecData.capacity();
 std::cout <<"remove_if删除元素后Size:"<< nSize << std::endl ;
 std::cout <<"remove_if删除元素后Capacity:"<< nCapacity << std::endl ;
 // 打印vector中所有的数据
 for_each(vecData.begin(),vecData.end(),printMyStruct);
 vecData.erase(itr);
 std::cout <<"erase删除元素后Size:" << nSize << std::endl ;
 std::cout <<"erase删除元素后Capacity:"<< nCapacity << std::endl ;
 // 打印vector中所有的数据
 for_each(vecData.begin(),vecData.end(),printMyStruct);
 system("pause");
 return 0;
}
 
vc++将数字和日期转成字符串:【上一篇】
字符表:【下一篇】
【相关文章】
  • 技术沙龙.:主题为《代码解析Castle(IOC)应用实例 -开源CMS 系统Cuyahoga》
  • STL学习小记
  • STL之父访谈录(一万二千字的大块头)
  • 利用Castle IOC实现远程调用的接口统一(上)
  • STL实践指南
  • 标准模板库STL之一 序列
  • Ineffective C++(1):STL容器不仅仅可以保存指针
  • STL之xhash
  • STL, Loki,Boost 中Functor比较之Boost库(一)
  • STL:map的使用:删除map中指定值为value的所有元素
  • 【随机文章】
  • RGBQUAD 与 COLORREF 的区别
  • 破解Access2003数据库密码
  • 破解圣经之------滚瓜烂熟篇(2)
  • [转贴]逐渐挖掘Varargs
  • [技术]数据容灾备份的等级和技术
  • Why should you use linux
  • AOP编程实践之AspectWeaverSample1.0
  • 开发Delphi对象式数据管理功能(八)
  • 规范化编程:ANSI和UNICODE的使用
  • Linux Device Drivers学习笔记2--Chapter.2 Building and Running Modules
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.