STL - 容器 - Array

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 ----------------

 

posted @ 2015-10-12 16:40  Master HaKu  阅读(185)  评论(0编辑  收藏  举报