📜  C++中的数组类

📅  最后修改于: 2021-05-26 02:03:21             🧑  作者: Mango

C++ 11中数组类的引入为C样式数组提供了更好的替代方法。数组类优于C样式数组的优点是:

  • 数组类知道其自身的大小,而C风格的数组缺少此属性。因此,在传递给函数时,我们不需要将Array的大小作为单独的参数传递。
  • 使用C样式数组时,数组被衰减为指针的风险更大。数组类不会衰减为指针
  • 数组类通常比C型数组更有效,更轻量且更可靠。

数组上的操作:-
1. at() :-此函数用于访问数组的元素。
2. get() :-此函数还用于访问数组的元素。该函数不是数组类的成员,而是类元组的重载函数。
3. 运算符[] :-这类似于C样式的数组。此方法也用于访问数组元素。

// C++ code to demonstrate working of array,
// to() and get()
#include
#include // for array, at()
#include // for get()
using namespace std;
int main()
{
    // Initializing the array elements
    array ar = {1, 2, 3, 4, 5, 6};
  
    // Printing array elements using at()
    cout << "The array elements are (using at()) : ";
    for ( int i=0; i<6; i++)
    cout << ar.at(i) << " ";
    cout << endl;
  
    // Printing array elements using get()
    cout << "The array elements 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[]
    cout << "The array elements are (using operator[]) : ";
    for ( int i=0; i<6; i++)
    cout << ar[i] << " ";
    cout << endl;
  
    return 0;
  
}

输出:

The array elemets are (using at()) : 1 2 3 4 5 6 
The array elemets are (using get()) : 1 2 3 4 5 6 
The array elements are (using operator[]) : 1 2 3 4 5 6 

4. front() :-这将返回数组的第一个元素。
5. back() :-这将返回数组的最后一个元素。

// C++ code to demonstrate working of
// front() and back()
#include
#include // for front() and back()
using namespace std;
int main()
{
    // Initializing the array elements
    array 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;
  
}

输出:

First element of array is : 1
Last element of array is : 6

6. size() :-返回数组中元素的数量。这是C样式数组缺少的属性。
7. max_size() :-它返回数组可以容纳的最大元素数,即声明数组的大小。 size()和max_size()返回相同的值。

// C++ code to demonstrate working of
// size() and max_size()
#include
#include // for size() and max_size()
using namespace std;
int main()
{
    // Initializing the array elements
    array 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;
  
}

输出:

The number of array elements is : 6
Maximum elements array can hold is : 6

8. swap() :-swap()将一个数组的所有元素彼此交换。

// C++ code to demonstrate working of swap()
#include
#include // for swap() and array
using namespace std;
int main()
{
  
    // Initializing 1st array
    array ar = {1, 2, 3, 4, 5, 6};
  
    // Initializing 2nd array
    array 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;
  
    // Swapping ar1 values with ar
    ar.swap(ar1);
  
    // Printing 1st and 2nd array after swapping
    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;
  
}

输出:

The first array elements before swapping are : 1 2 3 4 5 6 
The second array elements before swapping are : 7 8 9 10 11 12 
The first array elements after swapping are : 7 8 9 10 11 12 
The second array elements after swapping are : 1 2 3 4 5 6 

9. empty() :-当数组大小为零时,此函数返回true,否则返回false。
10. fill() :-此函数用于用特定值填充整个数组。

// C++ code to demonstrate working of empty()
// and fill()
#include
#include // for fill() and empty()
using namespace std;
int main()
{
  
    // Declaring 1st array
    array ar;
  
    // Declaring 2nd array
    array 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 empty
Array after filling operation is : 0 0 0 0 0 0 
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”