C++容器之array
来源:互联网 发布:大华电子称软件 编辑:程序博客网 时间:2024/05/16 12:28
简介
数组是一个固定大小的顺序容器:它以线性序列存储指定数量的元素。
成员函数
迭代器操作
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; intmain(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;intmain(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;intmain(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::atcout << "The second element is : " << arr.at(1) << endl;arr.at(1) = 20;cout << "The second element is : " << arr.at(1) << endl;// array::frontcout << "array::front : " << arr.front() << endl;// array::backcout << "array::back : " << arr.back() << endl;// array::dataint *p;p = arr.data();cout << "array::data" << *p << endl;return(0);}
Modifiers
array::fill()
向数组填充指定的值。
array::swap()
交换两个数组。两个数组类型和大小必须相同。
示例代码
#include<iostream>#include<array> using namespace std; voidprint(array<int, 5> arr){ for(int i = 0; i < arr.size(); i++) { cout <<arr.at(i) << ""; } cout << endl;}intmain(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);}
- C++容器之array
- STL::array之数组容器
- c++顺序容器之array
- TR1 五种新的容器 之 array
- java容器之Array转换List
- java容器之Array转换List
- c++STL通用容器 之 bitset容器
- 用c语言实现c++ array容器类
- C++STL之堆栈容器
- C++STL之set容器
- [C++]STL容器之priority_queue
- C++STL之vector容器
- C++STL之list容器
- C++STL之序列容器
- STL之vector,数组线性容器array,list容器,算法find,find_if,bind1st,仿函数
- array数组容器 C++
- C++容器array
- STL容器array
- 轻量级持久存储系统 MemcacheDB
- C++学习笔记2,各基础类型及共用体的长度测试
- NYOJ 456 邮票分你一半
- VS2008中 .h头文件出现“fatal error RC1004: unexpected end of file found”问题
- Servlet容器Tomcat中web.xml中url-pattern的配置详解
- C++容器之array
- ARM中LDR伪指令与LDR加载指令
- 百度校招笔试题
- 矩阵在计算斐波那契数列的运用
- NYOJ 460 项链
- 擦肩而过
- squid3配置要点
- LeetCode:Single Number
- 起点