【STL记录】Containers--Arrays

Array是container class array<>的一个实例,是一个固定长度的元素序列。

在使用array之前,需要include头文件:

#include <array>

一、初始化

// all elements of x have value 0 (int())
array<int,4> x = {}; 

//use an initializer list to initialize array
array<int,5> coll = { 42, 377, 611, 21, 44 };  

// one element with value 42, followed by 9 elements with value 0
array<int,10> c2 = { 42 }; 

二、Array Operations

1.Class array<> 的构造函数

OperationEffect
 array<Elem, N> c默认构造函数:使用默认初始化元素创建一个array
 array<Elem, N> c(c2)复制构造函数:通过复制创建一个相同的array(所有元素都被复制)
 array<Elem, N> c = c2复制构造函数:通过复制创建一个相同的array(所有元素都被复制)
 array<Elem, N> c(rv)Move constructor:creates a new array taking the contents of the rvalue rv
 array<Elem, N> c = rvMove constructor:creates a new array taking the contents of the rvalue rv
 array<Elem, N> c = initListCreates an array initialized with the elements of the initializer list

2.Nonmodifying Operations

OperationEffect
 c.empty()返回容器是否为空(相当于size() == 0)
 c.size()返回当前元素的个数
 c.max_size()返回可能存在元素的最大个数
 c1 == c2返回c1是否等于c2
 c1 != c2返回c1是否不等于c2
 c1 < c2返回c1是否小于c2
 c1 > c2返回c1是否大于c2
 c1 <= c2返回c1是否小于等于c2
 c1 >= c2返回c1是否大于等于c2

3.Assignments

OperationEffect
 c = c2将c2所有的元素赋给c
 c = rvMove assigns all elements of the rvalue rv to c
 c.fill(val)将值val赋给c的每一个元素
 c1.swap(c2)交换c1和c2的数据
 swap(c1, c2)交换c1和c2的数据

4.元素访问

OperationEffect
 c[idx]返回索引为idx的元素(没有边界检查)
 c.at(idx)返回索引为idx的元素(当idx超出边界,抛出range-error异常)
 c.front()返回第一个元素(不检查第一个元素是否存在)
 c.back()返回最后一个元素(不检查最后一个元素是否存在)

5.Iterator Functions

OperationEffect
 c.begin()返回第一个元素的随机访问迭代器
 c.end()返回位于最后一个元素之后的随机访问迭代器
 c.cbegin()返回第一个元素的常量随机存取迭代器
 c.cend()返回位于最后一个元素之后的常量随机访问迭代器
 c.rbegin()返回反向迭代器,指向最后一个元素
 c.rend()返回反向迭代器,位于第一个元素之前
 c.crbegin()返回常量反向迭代器,指向最后一个元素
 c.crend()返回常量反向迭代器,位于第一个元素之前

三、Tuple Interface

    Array提供了tuple 接口,所以:

  • tuple_size<>::value    number of elements
  • tuple_element<>::type    the type of a specific element
  • get()    gain access to a specific element

例:

typedef std::array<std::string, 5> FileStrings;
FiveStrings a  = {"hello", "nico", "how", "are", "you"};

std::tuple_size<FiveStrings>::value       // 5
std::tuple_element<1, FiveStrings>::type  // std::string
std::get<1>(a)                            // std::string("nico")

 四、Example of Using Arrays

#include <array>
#include <algorithm>
#include <functional>
#include <numeric>
#include "print.hpp"
using namespace std;

int main()
{
    //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(),            //destinatic
              negate<int>());       //operation
    PRINT_ELEMENTS(a);
}


// print.hpp

#include<iostream>
#include<string>

//PRINT_ELEMENTS()
// - prints optional string optstr followed by
// - all elements of the collection coll
// - in one line,separated by spaces
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;
}

输出:

 

 

转载于:https://my.oschina.net/daowuming/blog/716643

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值