为什么要用boost::array
2005-06-16 18:49
274 查看
为什么要用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;
}
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;
}
相关文章推荐
- 为什么要用boost::array
- 为什么要用boost::array
- [Boost基础]容器数据结构——array定长数组
- Android之TypedArray 为什么需要调用recycle()
- 浅尝boost之array
- 浅尝Boost之Array的初始化
- NSString为什么用copy而不是strong,NSMutableString为什么用strong而不是用copy(或者NSArray及NSMutableArray等)
- boost源码剖析之:泛型多维数组类multi_array
- boost 智能指针auto_ptr scoped_ptr shared_ptr scoped_array shared_array 总结
- ArrayBlockingQueue源码中为什么方法要用局部变量引用类变量
- C++之Boost::array用法简介
- python的array()为什么会报错
- 【Boost】boost::array详解
- boost的multi_array的用法
- juc中ArrayBlockingQueue为什么出入用同一个锁?
- 为什么in_array(0, ['a', 'b', 'c'])返回true
- 观V8源码中的array.js,解析 Array.prototype.slice为什么能将类数组对象转为真正的数组?
- c++智能指针:boost::scoped_ptr,boost::shared_ptr,boost::scoped_array
- 读boost::multi_array有感,多维数组实现(非类型模板,偏特化)
- boost源码剖析之:泛型多维数组类multi_array