您的位置:首页 > 其它

STL - 容器 - Array

2015-10-12 16:40 369 查看
Array是C++ 11给STL新增加的容器

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 ----------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: