STL - 容器 - Array
2015-10-12 16:40
369 查看
Array是C++ 11给STL新增加的容器
ArrayTest.cpp
运行结果:
---------------- simpleOperation(): Run Start ----------------
11 22 33 44 0 0 0 0 0 0
11 22 33 44 0 0 0 0 42 9999999
sum: 10000151
-11 -22 -33 -44 0 0 0 0 -42 -9999999
---------------- simpleOperation(): Run End ----------------
ArrayTest.cpp
#include <array> #include <algorithm> #include <functional> #include <numeric> #include "../../Core/print.hpp" #include "ArrayTest.h" using namespace std; void ArrayTest::simpleOperation() { // create array with 10 ints array<int, 10> a = { 11, 22, 33, 44 }; PRINT_ELEMENTS(a); // modify last two elements a.back() = 9999999; a[a.size() - 2] = 42; PRINT_ELEMENTS(a); // process sum of all elements cout << "sum: " << accumulate(a.begin(), a.end(), 0) << endl; // negate all elements transform(a.begin(), a.end(), // source a.begin(), // destination negate<int>()); // operation PRINT_ELEMENTS(a); } void ArrayTest::run() { printStart("simpleOperation()"); simpleOperation(); printEnd("simpleOperation()"); }
运行结果:
---------------- simpleOperation(): Run Start ----------------
11 22 33 44 0 0 0 0 0 0
11 22 33 44 0 0 0 0 42 9999999
sum: 10000151
-11 -22 -33 -44 0 0 0 0 -42 -9999999
---------------- simpleOperation(): Run End ----------------
相关文章推荐
- charles 常用设置
- 读取其他软件listview控件的内容
- Android MediaScanner:(四)MediaScanner之scanSingleFile
- 第4周项目6--多项式求和
- 一个界面中使用两个tableView
- 一夜八次受不了,偶要做回程序猿
- 第6周 项目2 - 建立链栈算法库
- 第七周 周一 顺序环形队列
- 2015-10-12 【项目1 - 建立顺序栈算法库】
- Android Studio在创建/导入项目的时候,一直处于building “XXX”gradle project info的解决办法
- POJ2299 Ultra-QuickSort 树状数组+离散化
- 云平台磁盘布局
- Java基础知识强化之IO流笔记60:打印流 之 改进复制文本文件的案例
- UI线程中调用Handler.post与直接执行方法的区别
- 第四周 项目4-建设双链表算法库
- UITableView全面解析
- 第七周项目1-建立顺序环形队列算法库
- 第七周--项目1建立环形队列算法库
- CSS实现带倒三角标记的WEB标准菜单效果代码
- 简单仿魅族手机Setings界面设计