首页 > 其他 > 详细

STL - 容器 - Array

时间:2015-10-12 17:21:37      阅读:83      评论:0      收藏:0      [点我收藏+]

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

 

STL - 容器 - Array

原文:http://www.cnblogs.com/davidgu/p/4872095.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!