码迷,mamicode.com
首页 > 编程语言 > 详细

c++ Array运用实例

时间:2019-12-14 21:15:26      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:c++   运用   fun   cout   ring   cpp   names   size   int   

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

 

c++ Array运用实例

标签:c++   运用   fun   cout   ring   cpp   names   size   int   

原文地址:https://www.cnblogs.com/herd/p/12040734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!