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
请按任意键继续. . .

猜你喜欢

转载自www.cnblogs.com/herd/p/12040734.html