boost::array
很遗憾,STL标准容器中并没有数组容器, 对于一组固定大小的数据, 用vector并不一定比Array合适,vector毕竟是大小可变的。而且个人认为,这样会使概念不够清晰,毕竟Array和vector概念上并不是完全等同的。
boost::array就是数组的容器类实现,他完全兼容STL,很有希望被加入下一代的C++标准中。Boost::array内部仍然是固定长度,但是却拥有STL容器兼容的接口,这样就使的Boost::array能够支持STL中的算法,能够和STL中的许多组建协同工作。
例子程序1:
#include <iostream>
#include <algorithm>
#include <functional>
#include <boost/array.hpp>
using namespace std;
using namespace boost;
template <class T>
inline void print_elements (const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
std::cout << *pos << ' ';
}
std::cout << std::endl;
}
int main()
{
// create and initialize array
array<int,10> a = { { 1, 2, 3, 4, 5 } };
print_elements(a);
// modify elements directly
for (unsigned i=0; i<a.size(); ++i) {
++a[i];
}
print_elements(a);
// change order using an STL algorithm
reverse(a.begin(),a.end());
print_elements(a);
// negate elements using STL framework
transform(a.begin(),a.end(), // source
a.begin(), // destination
negate<int>()); // operation
print_elements(a);
return 0;
}
运行结果:
1 2 3 4 5 0 0 0 0 0
2 3 4 5 6 1 1 1 1 1
1 1 1 1 1 6 5 4 3 2
-1 -1 -1 -1 -1 -6 -5 -4 -3 -2
例子程序2:
#include <string>
#include <iostream>
#include <boost/array.hpp>
template <class T>
void print_elements (const T& x)
{
for (unsigned i=0; i<x.size(); ++i) {
std::cout << " " << x[i];
}
std::cout << std::endl;
}
int main()
{
// create array of four seasons
boost::array<std::string,4> seasons = {
{ "spring", "summer", "autumn", "winter" }
};
// copy and change order
boost::array<std::string,4> seasons_orig = seasons;
for (unsigned i=seasons.size()-1; i>0; --i) {
std::swap(seasons.at(i),seasons.at((i+1)%seasons.size()));
}
std::cout << "one way: ";
print_elements(seasons);
// try swap()
std::cout << "other way: ";
std::swap(seasons,seasons_orig);
print_elements(seasons);
// try reverse iterators
std::cout << "reverse: ";
for (boost::array<std::string,4>::reverse_iterator pos
=seasons.rbegin(); pos<seasons.rend(); ++pos) {
std::cout << " " << *pos;
}
std::cout << std::endl;
return 0; // makes Visual-C++ compiler happy
}
运行结果:
one way: winter spring summer autumn
other way: spring summer autumn winter
reverse: winter autumn summer spring
分享到:
相关推荐
Boost程序库完全开发指南:深入C++“准”标准库+Boost程序库完全开发指南:深入C++“准”标准库+Boost程序库完全开发指南:深入C++“准”标准库+Boost程序库完全开发指南:深入C++“准”标准库+Boost程序库完全开发...
通过boost::asio::serialport类实现串口通信的例子
Boost程序库完全开发指南:深入C++_准_标准库 第3版 中文.part2
本文实例讲述了C++之boost::array的用法,分享给大家供大家参考。具体如下: 代码如下:#include #include #include <boost> #include using namespace std; int main() { boost::array<int> ...
Boost程序库完全开发指南: 深入C++“准”标准库!!
boost::asio::serial下6个工程演示多种串口读取写入方式方法,包含simple,with_timeout,async,callback,qt_integration,stream 等多个工程演示多种方式读取,写入串口,char,string ,buffer[]等多种数据格式。
主要介绍了C++之Boost::array用法简介,较为详细的分析了Boost::array中的常见用法,并用实例的形式予以总结归纳,需要的朋友可以参考下
Boost程序库完全开发指南:深入C++_准_标准库 第3版 中文
详细讲述了boost::thread的用法
BOOST程序库完全开发指南:深入C++“准”标准库(第3版).pdf
使用VS2017编译的boost库最新版1.68的动态库和静态库,多线程参数,经过测试可用
Boost程序库完全开发指南:深入C++“准”标准库(第3版).pdf
Boost程序库完全开发指南:深入C++_标准库 第3版带书签
BOOST程序库完全开发指南:深入C++“准”标准库 第3版
boost是一个功能强大、构造精巧、跨平台、开源并且完全免费的c++程序库,有着“c++‘准’标准库”的美誉。 本书基于boost1.42版,介绍了其中的所有99个库,并且详细深入地讲解了其中数十个库,同时实现了若干颇具...
Boost.URL是一个用于操作统一资源标识符(URI)和定位器(URL)的库。
Boost程序库完全开发指南:深入C++_准_标准库 第3版 中文.part1
Boost程序库完全开发指南:深入C++_准_标准库 第3版 中文.rar
Boost程序库完全开发指南:深入C++“准”标准库