boost multi_array使用中的疑问
碧空星尘 2005-09-28 09:17:31 在我以前的应用中,我用 vector<vector<string> >来保存我数据库查询的结果集,后来感觉在使用中耗费内存挺厉害的,于是想用multi_array试试。从文档和例程上看,multi_array是使用连续地址空间的,因此对他能否保存string类型数据保持怀疑。做了个简单的测试:
-------------------------------------------
#include <iostream>
#include "boost/multi_array.hpp"
#include "boost/cstdlib.hpp"
using namespace std;
int main () {
// Create a 3D array that is 3 x 4 x 2
typedef boost::multi_array<string, 3> array;
int x=2;
int y=4;
int z=2;
array A(boost::extents[x][y][z]);
// Assign a value to an element in the array
A[0][0][0] = "asjdfasf";
A[1][2][0] = "23412352154eeeeeee88";
cout<<A[0][0][0]<<endl;
cout<<A[1][2][0]<<endl;
return boost::exit_success;
}
-------------------------------------
竟然可以。所以考虑使用boost::multi_array<string, 2>来代替vector<vector<string> >。但是使用vector可以很方便的使用push_back增加元素,不知道multi_array有没有提供类似的功能?我感觉好像multi_array都是要自己给定大小的