c++ 11中提供了数组类Array(容器)该Array的引入引入为C风格的数组提供了更好的选择。array类相对于c类数组有以下的优点:
1. 数组类(Array class)知道自己的大小,而c风格的数组缺乏此属性。所以当传递给函数时,我们不需要把数组的大小作为单独的参数传递。
2. 使用c风格的数组,数组退化为指针的风险更大;而数组类不会衰退为指针。
3. Array通常比c风格的数组更有效、更轻、更可靠。
数组上的操作:
1 .at():用于访问数组的元素
2 . get():也用于访问数组的元素,该函数不是数组类(Array)的成员,而是类元组的重载函数
3 . 操作符[ ]:类似于C风格的数组,也可用于访问数组元素。
// to() and get()
#include<iostream>
#include<array> // for array, at()
#include<tuple> // for get()
using namespace std;
int main() {
// Initializing the array elements(初始化array)
array<int,6> ar = {1, 2, 3, 4, 5, 6};
// Printing array elements using at()(打印array元素使用at())
cout << "The array elemets are (using at()) : ";
for ( int i=0; i<6; i++)
cout << ar.at(i) << " ";
cout << endl;
// Printing array elements using get()(使用get()打印元素)
cout << "The array elemets are (using get()) : ";
cout << get<0>(ar) << " " << get<1>(ar) << " ";
cout << get<2>(ar) << " " << get<3>(ar) << " ";
cout << get<4>(ar) << " " << get<5>(ar) << " ";
cout << endl;
// Printing array elements using operator[](使用[]打印array元素)
cout << "The array elements are (using operator[]) : ";
for ( int i=0; i<6; i++)
cout << ar[i] << " ";
cout << endl;
return 0;
}
运行结果如图:
4. front():- This returns the first element of array.(返回数组第一个元素)
5.back():- This returns the last element of array(返回数组最后一个元素)
// front() and back()
#include<iostream>
#include<array> // for front() and back()
using namespace std;
int main() {
// Initializing the array elements
array<int,6> ar = {1, 2, 3, 4, 5, 6};
// Printing first element of array
cout << "First element of array is : ";
cout << ar.front() << endl;
// Printing last element of array
cout << "Last element of array is : ";
cout << ar.back() << endl;
return 0;
}
运行结果:
6. size() :返回数组容器的大小,C风格的数组不存在此方法.
7. max_size():返回数组容器可以包含的最大元素的个数,该值和size()返回的值是相同的。
// size() and max_size()
#include<iostream>
#include<array> // for size() and max_size()
using namespace std;
int main(){
// Initializing the array elements
array<int,6> ar = {1, 2, 3, 4, 5, 6};
// Printing number of array elements
cout << "The number of array elements is : ";
cout << ar.size() << endl;
// Printing maximum elements array can hold
cout << "Maximum elements array can hold is : ";
cout << ar.max_size() << endl;
return 0;
}
运行结果:
8.swap() : The swap() swaps all elements of one array with other.(和另一个数组元素进行交换)
#include<iostream>
#include<array> // for swap() and array
using namespace std;
int main() {
// Initializing 1st array
array<int,6> ar = {1, 2, 3, 4, 5, 6};
// Initializing 2nd array
array<int,6> ar1 = {7, 8, 9, 10, 11, 12};
// Printing 1st and 2nd array before swapping
cout << "The first array elements before swapping are : ";
for (int i=0; i<6; i++)
cout << ar[i] << " ";
cout << endl;
cout << "The second array elements before swapping are : ";
for (int i=0; i<6; i++)
cout << ar1[i] << " ";
cout << endl;
//交换ar和ar1
ar.swap(ar1);
// 打印交换后的array
cout << "The first array elements after swapping are : ";
for (int i=0; i<6; i++)
cout << ar[i] << " ";
cout << endl;
cout << "The second array elements after swapping are : ";
for (int i=0; i<6; i++)
cout << ar1[i] << " ";
cout << endl;
return 0;
}
运行结果:
9. empty() :array为空时,返回true,否则返回false
10.fill() :- This function is used to fill the entire array with a particular value.(使用特定的值填充数组)
// and fill()
#include<iostream>
#include<array> // for fill() and empty()
using namespace std;
int main() {
// Declaring 1st array
array<int,6> ar;
// Declaring 2nd array
array<int,0> ar1;
// Checking size of array if it is empty
ar1.empty()? cout << "Array empty":
cout << "Array not empty";
cout << endl;
// Filling array with 0
ar.fill(0);
// Displaying array after filling
cout << "Array after filling operation is : ";
for ( int i=0; i<6; i++)
cout << ar[i] << " ";
return 0;
}
运行结果:
到此,array类的基本方法已经全部介绍完毕,更多的信息还请查阅官网。