2014年01月18日 星期六 20:18
array是C++11标准最新引入的一个容器类型,简单来说,它就是一个更加好用的、STL Container Style的数组。具体怎么好用,可以参考下面的示例代码:
#include <iostream> #include <array> #include <stdexcept> #include <cstring> #include <cstdlib> using namespace std; int main(int argc, char **argv) { cout << "array" << endl; array<int,5> ary; ary.fill(0); for(auto x : ary) { cout << x << endl; } for(unsigned int i=0;i<ary.size();i++){ ary[i]=i; } try { cout << ary.at(ary.size()) << endl; }catch (out_of_range &e) { cout << "out of range: " << e.what() << endl; } for(auto x : ary) { cout << x << endl; } cout << "front: " << ary.front() << endl; cout << "back: " << ary.back() << endl; cout << "size: " << ary.size() << endl; cout << "max size: " << ary.max_size() << endl; cout << "empty? " << ary.empty() << endl; int *mi=(int *)malloc(ary.size()*sizeof(int)); for(unsigned int i=0;i<ary.size();i++){ mi[i]=i*i; } memcpy(ary.data(),mi,ary.size()*sizeof(int)); for(auto x : ary) { cout << x << endl; } free(mi); return 0; }
参考资料:
Zeuux © 2024
京ICP备05028076号