C++11 容器Array
2024-10-16 07:36:18
array是一个固定大小的顺序容器,不能动态改变大小,array内的元素在内存中以严格的线性顺序存储
与普通数组声明存储空间大小[]的方式是一样有效的,只是加入了一些成员函数和全局函数[get (array)、operators (array)],以便当作标准容器使用
零大小的array是有效的,但是不可以被成员函数front、back、data间接引用
array的swap是一个线性操作交换所有的元素,通常是非常低效的
与普通数组声明存储空间大小[]的方式是一样有效的,只是加入了一些成员函数和全局函数[get (array)、operators (array)],以便当作标准容器使用
零大小的array是有效的,但是不可以被成员函数front、back、data间接引用
array的swap是一个线性操作交换所有的元素,通常是非常低效的
Constructor:
.template < class T, size_t N > class array; 举例:
array<int,10> iArray={1,2,3,4,5,6,7,8,9,10};
Member functions:
Iterators
begin | Return iterator to beginning |
end | Return iterator to end |
rbegin | Return reverse iterator to reverse beginning |
rend | Return reverse iterator to reverse end |
cbegin | Return const_iterator to beginning |
cend | Return const_iterator to end |
crbegin | Return const_reverse_iterator to reverse beginning |
crend | Return const_reverse_iterator to reverse end |
std::array<int,> arr = { , , , , };
std::cout << "arr contains:";
for ( auto it = arr.cbegin(); it != arr.cend(); ++it )
{
*it = 34; //error can't modify *it
arr.begin(); //point to array first element
arr.front(); //return the first element
arr.back(); //return the end element
std::cout << *it << std::endl;
}
Capacity
empty | Test whether list is empty |
size | Return size |
max_size | Return maximum size |
Element access
operator[] | Access element |
at | Access element |
front | Access first element |
back | Access last element |
data | Get pointer to first data |
注意:使用at程序崩溃时不会显示堆栈信息,尽量使用[]去array的值
back
data//返回指向array中第一个元素的指针
const char* cstr = "Test string";
std::array<char,> charray;
std::memcpy (charray.data(),cstr,);
std::cout << charray.data() << std::endl;//如果是char类型则打印值 Test string
//如果array中保存的是int
cout << iArray.data() << endl;//两者等效,等于打印出第一个元素的地址
cout << &charray << endl; array<string,> sArray={"hello","c++","I"};
for (auto it = sArray.cbegin(); it != sArray.cend(); ++it)
{
cout << *it << '\t';//打印出hello c++ I
}
cout << sArray.data() << endl;//打印地址
Modifiers
fill | Fill array with value |
swap | Swap content |
fill
std::array<int, > arr;
arr.fill();
for (auto it = arr.begin(); it != arr.end(); it++)
{
std::cout << " " << *it;
} arr.assign();
for (auto it = arr.begin(); it != arr.end(); it++)
{
std::cout << " " << *it;
}
OutPut:
34 34 34 34 34 5 5 5 5 5
swap
Global functions
get(array) | Get element (tuple interface) (function template ) |
operators (array) | Global relational operator functions for array |
get(array)//Returns a reference to the Ith element of array arr.
函数原型:
.template <size_t I, class T, size_t N> T& get ( array<T,N>& arr ) noexcept;
.template <size_t I, class T, size_t N> T&& get ( array<T,N>&& arr ) noexcept;
.template <size_t I, class T, size_t N> const T& get ( const array<T,N>& arr ) noexcept;
Output:
first element in myarray: 30
first element in mytuple: 10
operators(array)
模板原型如下: .template <class T, size_T N>
bool operator== ( const array<T,N>& lhs, const array<T,N>& rhs );
.template <class T, size_T N>
bool operator!= ( const array<T,N>& lhs, const array<T,N>& rhs );
.template <class T, size_T N>
bool operator< ( const array<T,N>& lhs, const array<T,N>& rhs );
4template <class T, size_T N>
bool operator> ( const array<T,N>& lhs, const array<T,N>& rhs );
.template <class T, size_T N>
bool operator<= ( const array<T,N>& lhs, const array<T,N>& rhs );
.template <class T, size_T N>
bool operator>= ( const array<T,N>& lhs, const array<T,N>& rhs );
std::array<int,> a = {, , , , };
std::array<int,> b = {, , , , };
std::array<int,> c = {, , , , };
if (a==b) std::cout << "a and b are equal\n";
if (b!=c) std::cout << "b and c are not equal\n";
if (b<c) std::cout << "b is less than c\n";
if (c>b) std::cout << "c is greater than b\n";
if (a<=b) std::cout << "a is less than or equal to b\n";
if (a>=b) std::cout << "a is greater than or equal to b\n";
Output:
a and b are equal
b and c are not equal
b is less than c
c is greater than b
a is less than or equal to b
a is greater than or equal to b
最新文章
- [译]A Beginner’s Guide to npm — the Node Package Manager
- Codeforces Round #367 (Div. 2) D. Vasiliy&#39;s Multiset(可持久化Trie)
- 图解Android - Looper, Handler 和 MessageQueue
- Inside Flask - Flask 简介
- ubuntu 使用adb shell命令识别android设备
- SonarQube代码质量管理平台工具
- awk打印第n个参数到最后一个技巧/将n行组成一列
- Android Multimedia框架总结(四)MediaPlayer中从Java层到C++层类关系及prepare及之后其他过程
- Oracle AWRSQRPT报告生成和性能分析
- 另类AOP设计
- imx6 工具链下载地址
- Spring配置之标签的三两事
- Pycharm永久激活方式
- virtualenv虚拟环境的使用
- 神经网络之dropout层
- [Objective-C] 创建常量
- python threading
- IIS部署常见问题总结
- HDU 5884 Sort(二分+优先队列)
- nyoj----522 Interval (简单树状数组)