C++容器之array

简介

         数组是一个固定大小的顺序容器:它以线性序列存储指定数量的元素。

成员函数


迭代器操作


array::begin()

         返回一个迭代器指向array的第一个元素。如果array为空,返回值不能被引用。返回类型为array::iterator。

array::end()

         返回一个迭代器指向array最后一个元素的下一个位置。返回结果不能被引用。如果array为空,返回值和begin()一致。

array::rbegin()

         返回一个反转迭代器指向array的最后一个元素,如果array为空,返回值不能被引用。返回类型为array::reverse_iterator。

array::rend()

         返回一个反转迭代器指向array的第一个元素的下一个位置。如果array为空,返回值和rbegin()一致。返回值类型为array::reverse_iterator。

array::cbegin()

         begin()的const版本。

array::cend()

         end()的const版本。

array::crbegin()

         cbegin()的const版本。

array::crend()

         rend()的const版本。


示例代码

#include<iostream>
#include<array>
 
using namespace std;
 
int
main(void)
{
    array<int, 5> arr = {1, 2, 3, 4, 5};
   
    // array::begin end
    for(array<int, 5>::iterator it = arr.begin(); it != arr.end(); ++it)
    {
       cout << *it << "";
    }
    cout << endl;
   
    // array::rbegin rend
    for(array<int, 5>::reverse_iterator it = arr.rbegin(); it != arr.rend(); ++it)
    {
       cout << *it << "";
    }
    cout << endl;
   
    for(array<int, 5>::const_iterator it = arr.cbegin(); it != arr.cend(); ++it)
    {
       cout << *it << "";
    }
    cout << endl;
   
    for(array<int, 5>::const_reverse_iterator it = arr.crbegin(); it != arr.crend(); ++it)
    {
       cout << *it << "";
    }
    cout << endl;
   
    return(0);
}

Capacity


array::size()

         返回array中元素个数。返回值类型为array::size_type。

array::max_size()

         返回array能存储元素个数的最大值,和size()返回值相等。返回值类型array::size_type。

array::empty()

         测试数组是否为空。返回值为bool型,如果size()等于0表示array为空,返回true,否则返回false。

示例程序


#include <iostream>
#include <array>

using namespace std;

int
main(void)
{
	array<int, 0> arr1;
	array<int, 5> arr2 = {1, 2, 3, 4, 5};
	
	cout << "arr1 size : " << arr1.size() << endl;
	cout << "arr2 size : " << arr2.size() << endl;
	
	cout << "arr1 max size : " << arr1.max_size() << endl;
	cout << "arr2 max size : " << arr2.max_size() << endl;
	
	cout << "arr1 is " << (arr1.empty() ? "is empty":"is not empty") << endl;
	cout << "arr2 is " << (arr2.empty() ? "is empty":"is not empty") << endl;
	
	return 0;
}


Element access


array::operator[]

         返回array指定位置的引用。返回类型为reference/const_reference。

array::at

         返回array指定位置的引用。返回类型为reference/const_reference。

array::front

         返回array第一个元素的引用。返回类型为reference/const_reference。

array::back

         返回array最后一个元素的引用。返回类型为reference/const_reference。

array::data

         返回指向array第一个元素的指针。

示例程序

#include <iostream>
#include <array>

using namespace std;

int
main(void)
{
	array<int, 5> arr = {1, 2, 3, 4, 5};
	
	// array::operator=
	cout << "The first element is : " << arr[0] << endl;
	arr[0] = 10;
	cout << "The first element is : " << arr[0] << endl;
	arr[0] = 10;
	
	// array::at
	cout << "The second element is : " << arr.at(1) << endl;
	arr.at(1) = 20;
	cout << "The second element is : " << arr.at(1) << endl;
	
	// array::front
	cout << "array::front : " << arr.front() << endl;
	
	// array::back
	cout << "array::back : " << arr.back() << endl;
	
	// array::data
	int *p;
	p = arr.data();
	cout << "array::data" << *p << endl;
	
	return(0);
}



Modifiers


array::fill()

         向数组填充指定的值。

array::swap()

         交换两个数组。两个数组类型和大小必须相同。

示例代码

#include<iostream>
#include<array>
 
using namespace std;
 
void
print(array<int, 5> arr)
{
    for(int i = 0; i < arr.size(); i++)
    {
       cout <<arr.at(i) << "";
    }
    cout << endl;
}
int
main(void)
{
    array<int, 5> arr1 = {2, 2, 2, 2, 2};
    array<int, 5> arr2 = {1, 2, 3, 4, 5};
   
    // array::fill
    cout << "Beforefill() : ";
    print(arr1);
    arr1.fill(1);
    cout << "Afterfill() : ";
    print(arr1);
   
    arr1.swap(arr2);
    cout << "arr1: ";
    print(arr1);
    cout << "arr2";
    print(arr2);
   
    return(0);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ STL中的array是一个固定大小的数组容器,它提供了一组功能完备的操作函数,用于管理和操作数组。 arrayC++内置数组相比,具有以下优势: - 通过array可以方便地获取数组的大小(通过size()函数)和最大容量(通过max_size()函数)。- array提供了一系列的成员函数,如at()、front()、back()等,用于访问和操作数组元素。 - 支持迭代器,可以使用迭代器遍历和修改数组的元素。 - array的大小是固定的,不能动态改变。 以下是一个使用array的简单示例: ```cpp #include <iostream> #include <array> int main() { std::array<int, 5> arr = {1, 2, 3, 4, 5}; std::cout << "Array size: " << arr.size() << std::endl; std::cout << "Array elements: "; for (const auto& element : arr) { std::cout << element << " "; } std::cout << std::endl; arr[2] = 10; std::cout << "Modified array elements: "; for (const auto& element : arr) { std::cout << element << " "; } std::cout << std::endl; return 0; } ``` 上述示例中,我们创建了一个包含5个整数的array容器arr,并初始化为{1, 2, 3, 4, 5}。通过arr.size()可以获取数组的大小,输出结果为5。然后我们使用for循环和迭代器遍历数组的所有元素,并输出到控制台。 接着我们修改数组的第三个元素为10,然后再次遍历数组并输出修改后的结果。 array是一个非常方便和实用的容器,可以用于替代传统的C风格数组,并提供更多的功能和安全性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值