c++ Array运用实例

myprint.hpp

#include <iostream>
#include <string>

template <typename T>
inline void PRINT_ELEMENTS(const T& coll, const std::string& optstr = "")
{
    std::cout << optstr;
    for (const auto& elem:coll)
    {
        std::cout << elem << "  ";
    }
    std::cout << std::endl;
}

test.cpp

#include <array>
#include <algorithm>
#include <functional>
#include <numeric>

#include "myprint.hpp"

using namespace std;

int main()
{
    array<int, 10> array1 = { 3,7,9,5,2 };
    PRINT_ELEMENTS(array1);

    array1.back() = 666;
    array1[array1.size() - 2] = 555;
    PRINT_ELEMENTS(array1);

    cout << "sum:" << accumulate(array1.begin(), array1.end(), 0)<<endl;

    transform(array1.begin(),array1.end(),array1.begin(),negate<int>());

    PRINT_ELEMENTS(array1);

    system("pause");
    return 0;
}

3 7 9 5 2 0 0 0 0 0
3 7 9 5 2 0 0 0 555 666
sum:1247

-3 -7 -9 -5 -2 0 0 0 -555 -666
请按任意键继续. . .

 

posted @ 2019-12-14 20:12  西北逍遥  阅读(540)  评论(0编辑  收藏  举报