Vector

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

การประกาศ Vector นั้นสามารถทำได้โดยการใช้คำสั่งดังนี้

// vector<data_type> vector_name;
vector<int> Hallway(9);
vector<int> Hallway;
vector<int> Hallway {1,2,3,4,5};
vector<int> Hallway = {1,2,3,4,5};
vector<double> grade;
vector <char> Alpgrade;
vector<string> cars {"Jame","Jack","John"};

ฟังก์ชันที่เกี่ยวข้องกับเวกเตอร์คือ:

ฟังก์ชันที่ถูกใช้บ่อยที่สุด

  • push_back(): appends an element to the end

  • pop_back() Erases the last element

  • size() provides the number of elements

  • begin() provides reference to last element

  • end() provides reference to end of Vector.

  1. Iterators (ตัววนซ้ำ)

    • begin()arrow-up-right – Returns an iterator pointing to the first element in the vector

    • end()arrow-up-right – Returns an iterator pointing to the theoretical element that follows the last element in the vector

    • rbegin()arrow-up-right – Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element

    • rend()arrow-up-right – Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end)

    • cbegin()arrow-up-right – Returns a constant iterator pointing to the first element in the vector.

    • cend()arrow-up-right – Returns a constant iterator pointing to the theoretical element that follows the last element in the vector.

    • crbegin()arrow-up-right – Returns a constant reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element

    • crend()arrow-up-right – Returns a constant reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end)

Output

2. Capacity (ความจุ)

Output

Reference

Last updated

Was this helpful?