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

โปรแกรม C++ สำหรับการใช้งาน Vector ใน STL

Vector เป็นคอนเทนเนอร์ที่สามารถปรับขนาดได้โดยอัตโนมัติเหมือนอาร์เรย์แบบไดนามิก เมื่อมีการแทรกหรือลบองค์ประกอบ คอนเทนเนอร์จะจัดการหน่วยความจำโดยอัตโนมัติ องค์ประกอบใน vector จัดเก็บในหน่วยความจำที่ติดกัน ทำให้เข้าถึงและวนลูปได้ผ่าน iterator การแทรกหรือลบข้อมูลได้ที่ตำแหน่งต้นกลางหรือท้าย

ฟังก์ชันและคำอธิบาย

  • v.size() – คืนขนาดของ vector
  • v.push_back() – ใช้แทรกองค์ประกอบเข้า vector จากท้าย
  • v.pop_back() – นำค่าออกจาก vector จากท้าย
  • v.capacity() – คืนขนาดพื้นที่จัดเก็บที่จัดสรรไว้ปัจจุบันเป็นจำนวนองค์ประกอบ
  • v.clear() – ล้าง vector ให้ว่างเปล่า

โค้ดตัวอย่าง (โปรแกรมโต้ตอบแบบเมนู)

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

int main() {
    vector<int> v;
    vector<int>::iterator it;
    int c, i;

    while (true) {
        cout << "1.Size of the Vector" << endl;
        cout << "2.Insert Element into the Vector" << endl;
        cout << "3.Delete Last Element of the Vector" << endl;
        cout << "4.Display the capacity of vector" << endl;
        cout << "5.Display by Iterator" << endl;
        cout << "6.Clear the Vector" << endl;
        cout << "7.Exit" << endl;
        cout << "Enter your Choice: ";
        cin >> c;

        switch(c) {
            case 1:
                cout << "Size of Vector: " << v.size() << endl;
                break;
            case 2:
                cout << "Enter value to be inserted: ";
                cin >> i;
                v.push_back(i);
                break;
            case 3:
                cout << "Delete Last Element Inserted:" << endl;
                if (!v.empty()) v.pop_back();
                else cout << "Vector is empty!" << endl;
                break;
            case 4:
                cout << "Displaying capacity of vector: " << v.capacity() << endl;
                break;
            case 5:
                cout << "Displaying Vector by Iterator: ";
                for (it = v.begin(); it != v.end(); it++)
                    cout << *it << " ";
                cout << endl;
                break;
            case 6:
                v.clear();
                cout << "Vector Cleared" << endl;
                break;
            case 7:
                exit(0);
            default:
                cout << "Wrong Choice" << endl;
        }
    }
    return 0;
}

ตัวอย่างผลลัพธ์

1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Size of Vector: 0
...
Enter your Choice: 2
Enter value to be inserted: 7
...
Enter your Choice: 4
Displaying capacity of vector: 8
Enter your Choice: 5
Displaying Vector by Iterator: 7 6 4 3 5
Enter your Choice: 3
Delete Last Element Inserted:
Enter your Choice: 5
Displaying Vector by Iterator: 7 6 4 3
Enter your Choice: 6
Vector Cleared
Enter your Choice: 7
Exit.

หมายเหตุ: โค้ดต้นฉบับมีปัญหาเล็กน้อยที่ case 3 ไม่ตรวจสอบว่า vector ว่างก่อน pop_back() ซึ่งอาจทำให้เกิดข้อผิดพลาดรันไทม์ โค้ดด้านบนได้แก้ไขเพิ่มการตรวจสอบ !v.empty() แล้ว