Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

คลาสอาร์เรย์ใน C++


คลาส Array ใน C++ นั้นมีประสิทธิภาพเพียงพอและรู้ขนาดของตัวเองด้วย

ฟังก์ชันที่ใช้ในการดำเนินการกับอาร์เรย์คือ

  • size() =เมื่อต้องการคืนค่าขนาดของอาร์เรย์ เช่น คืนค่า no ขององค์ประกอบของอาร์เรย์
  • max_size() =เพื่อคืนค่าจำนวนสูงสุดขององค์ประกอบในอาร์เรย์
  • get(), at(), operator[] =เพื่อเข้าถึงองค์ประกอบอาร์เรย์
  • front() =เพื่อคืนค่าองค์ประกอบด้านหน้าของอาร์เรย์
  • back() =เพื่อคืนค่าองค์ประกอบสุดท้ายของอาร์เรย์
  • empty() =ส่งคืนค่าจริงหากขนาดอาร์เรย์เป็นจริง ไม่เช่นนั้นจะเป็นเท็จ
  • fill() =เพื่อเติมอาร์เรย์ทั้งหมดด้วยค่าเฉพาะ
  • swap() =เพื่อสลับองค์ประกอบของอาร์เรย์หนึ่งไปยังอีกอาร์เรย์หนึ่ง

นี่คือตัวอย่างการดำเนินการทั้งหมดตามที่กล่าวไว้ข้างต้น −

โค้ดตัวอย่าง

#include<iostream>
#include<array>
using namespace std;

int main() {
   array<int,4>a = {10, 20, 30, 40};
   array<int,4>a1 = {50, 60, 70, 90};
   cout << "The size of array is : ";
   //size of the array using size()
   cout << a.size() << endl;
   //maximum no of elements of the array
   cout << "Maximum elements array can hold is : ";
   cout << a.max_size() << endl;
   // Printing array elements using at()
   cout << "The array elements are (using at()) : ";
   for ( int i=0; i<4; i++)
      cout << a.at(i) << " ";
      cout << endl;
   // Printing array elements using get()
   cout << "The array elements are (using get()) : ";
   cout << get<0>(a) << " " << get<1>(a) << " "<<endl;
   cout << "The array elements are (using operator[]) : ";
   for ( int i=0; i<4; i++)
      cout << a[i] << " ";
      cout << endl;
   // Printing first element of array
   cout << "First element of array is : ";
   cout << a.front() << endl;
   // Printing last element of array
   cout << "Last element of array is : ";
   cout << a.back() << endl;
   cout << "The second array elements before swapping are : ";
   for (int i=0; i<4; i++)
      cout << a1[i] << " ";
      cout << endl;
   // Swapping a1 values with a
   a.swap(a1);
   // Printing 1st and 2nd array after swapping
   cout << "The first array elements after swapping are : ";
   for (int i=0; i<4; i++)
      cout << a[i] << " ";
      cout << endl;
      cout << "The second array elements after swapping are : ";
   for (int i = 0; i<4; i++)
      cout << a1[i] << " ";
      cout << endl;
   // Checking if it is empty
   a1.empty()? cout << "Array is empty":
   cout << "Array is not empty";
   cout << endl;
   // Filling array with 1
   a.fill(1);
   // Displaying array after filling
   cout << "Array content after filling operation is : ";
   for ( int i = 0; i<4; i++)
      cout << a[i] << " ";
      return 0;
}

ผลลัพธ์

The size of array is : 4
Maximum elements array can hold is : 4
The array elements are (using at()) : 10 20 30 40
The array elements are (using get()) : 10 20
The array elements are (using operator[]) : 10 20 30 40
First element of array is : 10
Last element of array is : 40
The second array elements before swapping are : 50 60 70 90
The first array elements after swapping are : 50 60 70 90
The second array elements after swapping are : 10 20 30 40
Array is not empty
Array content after filling operation is : 1 1 1 1