STL - 容器 - Array

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: Array是C++ 11给STL新增加的容器 ArrayTest.cpp #include #include #include #include #include "../../Core/print.

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

 

目录
相关文章
|
5天前
|
存储 设计模式 算法
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
11 1
|
11天前
|
存储 算法 C++
详解C++中的STL(标准模板库)容器
【4月更文挑战第30天】C++ STL容器包括序列容器(如`vector`、`list`、`deque`、`forward_list`、`array`和`string`)、关联容器(如`set`、`multiset`、`map`和`multimap`)和容器适配器(如`stack`、`queue`和`priority_queue`)。它们为动态数组、链表、栈、队列、集合和映射等数据结构提供了高效实现。选择合适的容器类型可优化性能,满足不同编程需求。
|
13天前
|
C++ 容器
STL—map容器
STL—map容器
|
17天前
|
存储 算法 程序员
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
|
25天前
|
C++ 容器
约瑟夫经典问题C++,STL容器queue解法
约瑟夫经典问题C++,STL容器queue解法
14 0
|
1月前
|
存储 C语言 C++
C++中STL常用容器(vector、deque、list、map、set)一文带你了解
C++中STL常用容器(vector、deque、list、map、set)一文带你了解
|
1月前
|
存储 算法 C++
C++容器STL相关面试问题
C++容器STL相关面试问题
|
2天前
|
监控 Kubernetes Docker
【Docker 专栏】Docker 容器内应用的健康检查与自动恢复
【5月更文挑战第9天】本文探讨了Docker容器中应用的健康检查与自动恢复,强调其对应用稳定性和系统性能的重要性。健康检查包括进程、端口和应用特定检查,而自动恢复则涉及重启容器和重新部署。Docker原生及第三方工具(如Kubernetes)提供了相关功能。配置检查需考虑检查频率、应用特性和监控告警。案例分析展示了实际操作,未来发展趋势将趋向更智能和高效的检查恢复机制。
【Docker 专栏】Docker 容器内应用的健康检查与自动恢复
|
1天前
|
NoSQL Redis Docker
Mac上轻松几步搞定Docker与Redis安装:从下载安装到容器运行实测全程指南
Mac上轻松几步搞定Docker与Redis安装:从下载安装到容器运行实测全程指南
10 0
|
2天前
|
存储 安全 数据库
【Docker 专栏】Docker 容器内应用的状态持久化
【5月更文挑战第9天】本文探讨了Docker容器中应用状态持久化的重要性,包括数据保护、应用可用性和历史记录保存。主要持久化方法有数据卷、绑定挂载和外部存储服务。数据卷是推荐手段,可通过`docker volume create`命令创建并挂载。绑定挂载需注意权限和路径一致性。利用外部存储如数据库和云服务可应对复杂需求。最佳实践包括规划存储策略、定期备份和测试验证。随着技术发展,未来将有更智能的持久化解决方案。
【Docker 专栏】Docker 容器内应用的状态持久化