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

คลาส std::array ใน C++: คู่มือครบถ้วนพร้อมตัวอย่าง

std::array เป็นคอนเทนเนอร์ที่ห่อหุ้มอาร์เรย์แบบ C-style ให้มีอินเตอร์เฟซเหมือน STL containers รู้ขนาดของตัวเอง และมีประสิทธิภาพเท่าอาร์เรย์ดิบ

สมาชิกฟังก์ชันหลัก

ฟังก์ชันคำอธิบาย
size()จำนวนองค์ประกอบ (คงที่)
max_size()จำนวนองค์ประกอบสูงสุด (เท่ากับ size())
at(i)เข้าถึงพร้อม bounds checking (throw out_of_range)
operator[]เข้าถึงโดยไม่ตรวจสอบขอบเขต (รวดเร็วกว่า)
front()อ้างอิงองค์ประกอบแรก
back()อ้างอิงองค์ประกอบสุดท้าย
empty()true ถ้าขนาดเป็น 0
fill(val)เติมค่า val ทุกองค์ประกอบ
swap(other)สลับเนื้อหากับอีก array
data()คืน pointer ไปยังข้อมูลดิบ (สำหรับ C API)

ตัวอย่างโค้ดครบถ้วน

#include <iostream>
#include <array>
#include <algorithm>
#include <iterator>

using namespace std;

int main() {
    // สร้างและเริ่มต้น
    array<int, 5> arr = {10, 20, 30, 40, 50};
    array<int, 5> arr2 = {1, 2, 3, 4, 5};
    
    cout << "=== ข้อมูลพื้นฐาน ===" << endl;
    cout << "Size: " << arr.size() << endl;
    cout << "Max size: " << arr.max_size() << endl;
    cout << "Empty: " << (arr.empty() ? "true" : "false") << endl;
    
    cout << "\n=== การเข้าถึงข้อมูล ===" << endl;
    cout << "Front: " << arr.front() << endl;
    cout << "Back: " << arr.back() << endl;
    cout << "At(2): " << arr.at(2) << endl;
    cout << "[3]: " << arr[3] << endl;
    cout << "Data pointer: " << arr.data() << endl;
    
    cout << "\n=== Iteration ===" << endl;
    cout << "Range-based for: ";
    for (int x : arr) cout << x << " ";
    cout << endl;
    
    cout << "With algorithm: ";
    copy(arr.begin(), arr.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
    
    cout << "\n=== การดัดแปลง ===" << endl;
    arr.fill(99);
    cout << "After fill(99): ";
    for (int x : arr) cout << x << " ";
    cout << endl;
    
    cout << "Before swap - arr: ";
    for (int x : arr) cout << x << " ";
    cout << "\nBefore swap - arr2: ";
    for (int x : arr2) cout << x << " ";
    
    arr.swap(arr2);
    
    cout << "\nAfter swap - arr: ";
    for (int x : arr) cout << x << " ";
    cout << "\nAfter swap - arr2: ";
    for (int x : arr2) cout << x << " ";
    cout << endl;
    
    // Structured binding (C++17)
    cout << "\n=== Structured binding (C++17) ===" << endl;
    array<int, 3> point = {10, 20, 30};
    auto [x, y, z] = point;
    cout << "x=" << x << ", y=" << y << ", z=" << z << endl;
    
    return 0;
}

ผลลัพธ์

=== ข้อมูลพื้นฐาน ===
Size: 5
Max size: 5
Empty: false

=== การเข้าถึงข้อมูล ===
Front: 10
Back: 50
At(2): 30
[3]: 40
Data pointer: 0x7ffc12345670

=== Iteration ===
Range-based for: 10 20 30 40 50 
With algorithm: 10 20 30 40 50 

=== การดัดแปลง ===
After fill(99): 99 99 99 99 99 
Before swap - arr: 99 99 99 99 99 
Before swap - arr2: 1 2 3 4 5 
After swap - arr: 1 2 3 4 5 
After swap - arr2: 99 99 99 99 99 

=== Structured binding (C++17) ===
x=10, y=20, z=30

ข้อดีของ std::array เทียบกับ C-array

  • รู้ขนาดเอง (ไม่ decay เป็น pointer)
  • มี bounds checking ด้วย at()
  • รองรับ STL algorithms และ iterators
  • คัดลอกและมอบหมายได้แบบ value semantics
  • ใช้กับ range-based for loop ได้
  • ไม่มี overhead เพิ่มเติม (zero overhead abstraction)

เมื่อไหร่ควรใช้ std::array

  • ขนาดคงที่และรู้จักที่ compile-time
  • ต้องการประสิทธิภาพสูงสุด (stack allocation)
  • ต้องการ interface แบบ STL container