C++ stl array数组

array是固定大小的序列容器。属于std命名空间。


成员函数:

1、容器相关:注意××end()迭代器指向的位置是无效的。

非const修饰的迭代器:

begin():返回指向容器内部第一个元素的迭代器

iterator begin() noexcept;
const_iterator begin() const noexcept;

end():返回指向容器内部最后一个元素的下一个位置迭代器

iterator end() noexcept;
const_iterator end() const noexcept;

rbegin():返回一个反向迭代器,指向容器的最后一个元素

reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;

rend():返回一个反向迭代器,指向容器第一个元素前面的位置

reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;


const修饰的迭代器。功能与非const迭代器一样。

cbegin():返回的const迭代器

const_iterator cbegin() const noexcept;

cend():返回的const迭代器

const_iterator cend() const noexcept;

crbegin():返回const反向迭代器

const_reverse_iterator crbegin() const noexcept;

crend()返回const反向迭代器

const_reverse_iterator crend() const noexcept;


2、容器的容量相关的成员函数

size():返回容器中元素的个数

constexpr size_type size() noexcept;

max_size():返回容器所能容纳的元素的最大个数

constexpr size_type max_size() noexcept;

empty():测试数组array是否为空,返回一个bool值标识array是否为空。

constexpr bool empty() noexcept;

3、元素访问

operator[]:返回下标为n的元素的引用

reference operator[] (size_type n);
const_reference operator[] (size_type n) const;

at:返回下标为n的元素的引用

reference at ( size_type n );
const_reference at ( size_type n ) const;

front:返回容器中第一个元素的引用

reference front();
const_reference front() const;

back:返回容器中最后一个元素的引用

reference back();
const_reference back() const;

data:返回指向第一个元素的指针

value_type* data() noexcept;
const value_type* data() const noexcept;

4、关于修改的函数

fill():填充数组,将array中的元素均填充为val。

void fill (const value_type& val);

swap():交换内容,将array  x的元素值与调用swap的array对象的值调换。

void swap (array& x) noexcept(noexcept(swap(declval<value_type&>(),declval<value_type&>())));
 
 
 

例子:

#include <iostream>
#include <array>

using namespace std;

array <int,5>globalarray;//默认初始化为0
void arrdefine_test()
{
	array<int,5>first; //未初始化,元素值不确定
	array<int,5>second = {1,2,3,4,5}; //初始化
	array<int,5>third = second; //复制初始化
	cout<<"global array"<<endl;
	for(int i = 0; i < 5; i++)
		cout << globalarray[i] << " ";
	cout << endl;
	cout<<"first"<<endl;
	for(int i = 0; i < 5; i++)
		cout << first[i] << " ";
	cout << endl;
	cout << "second"<< endl;
	for(int i = 0; i < 5; i++)
		cout << second[i] << " ";
	cout << endl<<"third"<<endl;
	for(int i = 0; i < 5; i++)
		cout << third[i] << " ";
	cout << endl;
	
}
void iterator_test()
{
	array<int, 5> myarr = {3,4,5,6,7};
	array<int, 5>::iterator arriter;
	array<int, 5>::reverse_iterator rarriter;
	array<int, 5>::const_iterator carriter;
	array<int, 5>::const_reverse_iterator crarriter;
/*
	auto it;
	for(it = myarr.begin(); it != myarr.end(); it++){
		cout << *it ;
	}
	cout << endl;
*/
	//begin()\end()返回iterator和const_iterator两种迭代器
	for(arriter = myarr.begin(); arriter != myarr.end(); arriter++){
		cout << *arriter ;
	}//34567
	cout << endl;
	for(carriter = myarr.begin(); carriter != myarr.end(); carriter++){
		cout << *carriter ;
	}//34567
	cout << endl;
	//cbegin()\cend()值返回const_iterator一种迭代器
	for(carriter = myarr.cbegin(); carriter != myarr.cend(); carriter++){
		cout << *carriter ;
	}//34567
	cout << endl;
	//rbegin()\rend()返回reverse_iterator和const_reverse_iterator两种类型的迭代器
	for(rarriter = myarr.rbegin(); rarriter != myarr.rend(); rarriter++){
		cout << *rarriter;
	}//76543	
	cout << endl;


	for(crarriter = myarr.rbegin(); crarriter != myarr.rend(); crarriter++){
		cout << *crarriter;
	}//76543	
	cout << endl;
	//crbegin()和crend()只返回const_reverse_iterator一种迭代器
	for(crarriter = myarr.crbegin(); crarriter != myarr.crend(); crarriter++){
		cout << *crarriter ;
	}//76543
	cout << endl;
}


void access_test()
{
	array<int,20> myarr;
	int mfront;
	int mback;
	int *data;
	int mdata;
	for(int i = 0; i < 10; i++)
		myarr[i] = i;
	for(int i = 0; i < 10; i++)
		cout << myarr[i] << " ";
	cout << endl;
	for(int i = 10; i < 20; i++)
		myarr.at(i) = i;
	for(int i = 10; i < 20; i++)
		cout << myarr.at(i) << " ";
	cout << endl;
	
	mdata = myarr.at(16);
	cout << "myarr.at(16): "<< mdata << endl;
	mdata = myarr[13];
	cout << "myarr[13]"<< mdata << endl;
 	mfront = myarr.front();
	cout << "front:  " << mfront << endl;
	mback = myarr.back();
	cout <<" back:  " << mback << endl;
	data = myarr.data();	
	cout <<"data:  "<< *data << endl;	
}


void capacity_test()
{
	array<int,10>myarr;
	array<int,0>myarr2;
	for(int i = 0; i < 5; i++){
		myarr[i] = i;
	}
	cout <<"size:"<< myarr.size() << endl;//10
	cout <<"sizeof(myarr):"<<sizeof(myarr)<<endl; //字节数40
	cout << "max_size:"<<myarr.max_size() << endl; //10
	cout << "empty:"<< (myarr.empty()?"true":"false") << endl; //false
	cout << "empty:"<<(myarr2.empty()?"true":"false") << endl;//true
}
void modefy_test()
{
	array<int,10> myarr; //array<int, 10>其实是一种类似于int的数据类型,可以用于定义变量myarr。
	array<int,10> youarr;//array<int, 5>或array<int, 10>均是不同的类型。
	myarr.fill(17);
	for(int &x:myarr)cout <<" "<<x;
	cout << endl;
	youarr.fill(100);
	for(int i = 0; i < 10; i++)
		cout << " " << youarr[i];
	cout << endl;
	cout<<"myarr.swap(youarr)"<<endl;


	//swap()函数的两个对象必须元素个数一致,元素的类型也要一致。
	myarr.swap(youarr); //使用成员函数swap()
	for(int &x:myarr)cout << " " << x;
	cout << endl;
	for(int&x:youarr)cout << " " << x;
	cout << endl;
	cout<<"swap(myarr,youarr)"<<endl;
	swap(myarr,youarr); //使用的算法函数swap()
	for(int &x:myarr)cout << " " << x;
	cout << endl;
	for(int&x:youarr)cout << " " << x;
	cout << endl;


}
int main()
{
	arrdefine_test();
	iterator_test();
	access_test();
	capacity_test();
	modefy_test();
	return 0;
}
结果是:

global array
0 0 0 0 0 
first
-1219775224 -1080231112 134518057 1 65535 
second
1 2 3 4 5 
third
1 2 3 4 5 
34567
34567
34567
76543
76543
76543
0 1 2 3 4 5 6 7 8 9 
10 11 12 13 14 15 16 17 18 19 
myarr.at(16): 16
myarr[13]13
front:  0
 back:  19
data:  0
size:10
sizeof(myarr):40
max_size:10
empty:false
empty:true
 17 17 17 17 17 17 17 17 17 17
 100 100 100 100 100 100 100 100 100 100
myarr.swap(youarr)
 100 100 100 100 100 100 100 100 100 100
 17 17 17 17 17 17 17 17 17 17
swap(myarr,youarr)
 17 17 17 17 17 17 17 17 17 17
 100 100 100 100 100 100 100 100 100 100

  • 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、付费专栏及课程。

余额充值