C++STL之数组Array(C++11)

一、数组的简单使用以及用时测试:

#include<iostream>
#include<array>
#include<cstdlib>//qsort bsearch NULL
#include<ctime>
using namespace std;


const int SIZE = 100000;//数组的长度
int main()
{
   //存放long类型元素,指定大小为SIZE
	array<long, SIZE>arr;
    
	clock_t start = clock();//ms
	for (int i = 0; i < SIZE; i++)
	{
		arr[i] = rand()%100000;
	}
	cout << "插入100000个元素耗时为: " << (clock() - start) << endl;
	cout << "array.size()" << arr.size() << endl;
	cout << "array.front()" << arr.front() << endl;
	cout << "array.back()" << arr.back() << endl;
	cout << "array.data()" << arr.data() << endl;//返回数组起始地址
	
	//qsort();
	//bsearch();
	return 0;
}
执行结果:
插入100000个元素耗时为: 16
array.size()100000
array.front()41
array.back()1629
array.data()0069E2BC
请按任意键继续. . .

耗时因计算机而异,同一台计算机每次run结果可能不同

二、数组的底层结构分析

template<class _Ty,	size_t _Size>
	class array
	{	// fixed size array of values
public:
	enum {_EEN_SIZE = _Size};	// helper for expression evaluator
	typedef array<_Ty, _Size> _Myt;
	typedef _Ty value_type;
	typedef size_t size_type;
	typedef ptrdiff_t difference_type;
	typedef _Ty *pointer;
	typedef const _Ty *const_pointer;
	typedef _Ty& reference;
	typedef const _Ty& const_reference;
	//普通迭代器和常迭代器
	typedef _Array_iterator<_Ty, _Size> iterator;
	typedef _Array_const_iterator<_Ty, _Size> const_iterator;
	//普通的反向迭代器和常反向迭代器
	typedef _STD reverse_iterator<iterator> reverse_iterator;
	typedef _STD reverse_iterator<const_iterator> const_reverse_iterator;

	void assign(const _Ty& _Value){}
	void fill(const _Ty& _Value)	{}
	void swap(_Myt& _Other)_NOEXCEPT_OP(_NOEXCEPT_OP(_Swap_adl(_Elems[0], _Elems[0]))){	}
	iterator begin() _NOEXCEPT{}
	const_iterator begin() const _NOEXCEPT{}
	iterator end() _NOEXCEPT	{}
	size_type size() const _NOEXCEPT{}
	bool empty() const _NOEXCEPT{}
	const_reference at(size_type _Pos) const{}
	reference operator[](size_type _Pos){}
	const_reference operator[](size_type _Pos) const{}
	reference front(){	}
	const_reference front() const{}
	reference back(){}
	const_reference back() const{}
	_Ty *data() _NOEXCEPT{}
	const _Ty *data() const _NOEXCEPT{}

	//数组容器的真实面目
	_Ty _Elems[_Size == 0 ? 1 : _Size];
	};

这里罗列出我VS2013的Array部分SourceCode,主要是三个部分:

1.类型重定义以及array的迭代器的定义;
2.数组的方法;
3.数组的定义:	_Ty _Elems[_Size == 0 ? 1 : _Size];
可以看出,如果我们给定大小为0时,默认会分配1个长度

可能在不同的编译器上的实现有一些差异,但是基本思路和方法的使用是一致的,要不然怎么叫标准模板库,标准的意思是所有平台都应该遵循的,所以不影响我们理解STL

具体看下我们在上面的演示代码中用的几个方法的实现:

//typedef _Ty& reference;
reference front()//获取首元素
{	
	return (_Elems[0]);
}
reference back()//获取末尾元素
{	
	return (_Elems[_Size - 1]);
}
_Ty *data() _NOEXCEPT//获取数组起始地址
{
	return (_Elems);
}
//typedef size_t size_type;
size_type size() const _NOEXCEPT//获取数组的size
{
	return (_Size);
}
相对来说,array实现起来比较简单,代码也很好理解

不过我们可以发现一个不是特点的特点:

array没有构造函数,因为它不需要构造函数....
array是定容的,一旦指定不可改变,实际上就和我们C语言使用的数组本质是一样的

三、最后看一个官方文档的例子:

#include <string>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <array>
 
int main()
{
    // construction uses aggregate initialization
    std::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 prior to the CWG 1270 revision
                                        // (not needed in C++11 after the revision and in C++14 and beyond)
    std::array<int, 3> a2 = {1, 2, 3};  // never required after =
    std::array<std::string, 2> a3 = { std::string("a"), "b" };
 
    // container operations are supported
    std::sort(a1.begin(), a1.end());
    std::reverse_copy(a2.begin(), a2.end(), 
                      std::ostream_iterator<int>(std::cout, " "));
 
    std::cout << '\n';
 
    // ranged for loop is supported
    for(const auto& s: a3)
}

执行结果:
3 2 1 
a b

参考地址:https://en.cppreference.com/w/cpp/container/array

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值