1 // container.cpp : 定义控制台应用程序的入口点。 2 //any,保存具有不同值类型的单个值的一种安全、泛型的容器,来自 Kevlin Henney. 3 //array - 兼容STL的容器,是固定大小的数组的包装,来自 Nicolai Josuttis. 4 //multi_arry -多维数组 5 #include "stdafx.h" 6 #include <string> 7 #include <vector> 8 #include <iostream> 9 #include <boost/any.hpp> 10 #include <boost/array.hpp> 11 #include <boost/multi_array.hpp> 12 #include <boost/cstdlib.hpp> 13 using namespace std; 14 /*any用法*/ 15 void container_any() 16 { 17 std::vector<boost::any> a_vec; 18 a_vec.push_back(2); 19 a_vec.push_back(std::string("any")); 20 21 for(unsigned int i=0; i<a_vec.size(); i++) 22 { 23 cout<<a_vec[i].type().name()<<endl; 24 try 25 { 26 int result; 27 string result_c; 28 if(string(a_vec[i].type().name()).compare("int")==0) 29 { 30 result=boost::any_cast<int>(a_vec[i]); 31 cout<<result<<endl; 32 } 33 else 34 { 35 result_c=boost::any_cast<string>(a_vec[1]); 36 cout<<result_c<<endl; 37 } 38 } 39 catch(boost::bad_any_cast& e) 40 { 41 cout<<"cast error:"<<e.what()<<endl; 42 } 43 } 44 } 45 /*array用法*/ 46 void container_array() 47 { 48 /*多出的补0*/ 49 boost::array<int,10> arr={1,2,2,3,5,4,8,9,10}; 50 for(size_t i=0;i<arr.size();i++) 51 { 52 cout<<arr[i]<<endl; 53 } 54 } 55 /*3 Dim*/ 56 void container_multi_array() 57 { 58 boost::multi_array<int,3> multi_arr(boost::extents[2][3][5]); 59 multi_arr[0][0][0]=54; 60 cout<<multi_arr[0][0][0]<<endl; 61 } 62 int _tmain(int argc, _TCHAR* argv[]) 63 { 64 container_any(); 65 container_array(); 66 container_multi_array(); 67 return 0; 68 }