中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > 综合其它
为什么要用boost::array
作者:未知 时间:2005-07-27 23:27 出处:CSDN 责编:chinaitpower
              摘要:为什么要用boost::array

为什么要用boost::array


1、boost::array 与 std::vector
( From Chapter 2. Boost.Array)
As replacement for ordinary arrays, the STL provides class std::vector. However, std::vector<>

provides the semantics of dynamic arrays. Thus, it manages data to be able to change the number

of elements. This results in some overhead in case only arrays with static size are needed.

2、便于使用stl中的算法

3、越界检查
   尽管抛出assert错误,但总比没有强

4、整体操作数据很方便
  boost::array::assign(...)


-----------------------------------------------------------
eg:


#include <iostream>
#include <algorithm>
#include <string>
#include <boost/array.hpp>

template<typename T>
void PrintValue(T value)
{
 std::cout<<value<<std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
 //---------------------------------
 //int
 
 boost::array<int, 10> arrInt = {0,1,2}; //未赋值的初试化为0
 std::for_each(arrInt.begin(),arrInt.end(),PrintValue<int>);
 
 //所有的值都赋为20
 arrInt.assign(20);
 std::for_each(arrInt.begin(),arrInt.end(),PrintValue<int>);

 //--------------------------------------
 //string
 boost::array<std::string, 5> arrStr; //默认为""
 arrStr.assign("NULL");
 std::for_each(arrStr.begin(),arrStr.end(),PrintValue<std::string>);

 //越界检查
 //std::string strErr = arrStr[5]; //发出assert

 //比较
 boost::array<std::string, 5> arrStr2;
 arrStr2.assign("MULL");
 if(arrStr > arrStr2)
  std::cout << "arrStr > arrStr2" <<std::endl;
 
 if(arrStr != arrStr2)
  std::cout << "arrStr != arrStr2" <<std::endl;

 //boost::array<std::string, 6> arrStr3;
 //arrStr3.assign("NULL");
 //if(arrStr != arrStr3)   //无法比较,类型不同5 != 6
 // std::cout << "arrStr != arrStr3" <<std::endl;

 return 0;
}


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有