เวกเตอร์ (Vector) คืออาร์เรย์แบบไดนามิกที่สามารถปรับขนาดได้ด้วยตนเองเมื่อมีการแทรกหรือลบองค์ประกอบ องค์ประกอบของเวกเตอร์จะถูกเก็บในหน่วยความจำที่ติดกัน และคอนเทนเนอร์จะจัดการหน่วยความจำโดยอัตโนมัติ
โปรแกรมที่สาธิตการใช้งานเวกเตอร์ มีดังนี้:
ตัวอย่าง
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
int ch, val;
vector<int> vec;
cout<<"1)Insert Element into the Vector"<<endl;
cout<<"2)Delete Last Element of the Vector"<<endl;
cout<<"3)Print size of the Vector"<<endl;
cout<<"4)Display Vector elements"<<endl;
cout<<"5)Clear the Vector"<<endl;
cout<<"6)Exit"<<endl;
do {
cout<<"Enter your Choice: "<<endl;
cin>>ch;
switch(ch) {
case 1:
cout<<"Enter value to be inserted: "<<endl;
cin>>val;
vec.push_back(val);
break;
case 2:
cout<<"Last Element is deleted."<<endl;
vec.pop_back();
break;
case 3:
cout<<"Size of Vector: ";
cout<<vec.size()<<endl;
break;
case 4:
cout<<"Displaying Vector Elements: ";
for (int i = 0; i < vec.size(); i++)
cout<<vec[i]<<" ";
cout<<endl;
break;
case 5:
vec.clear();
cout<<"Vector Cleared"<<endl;
break;
case 6:
cout<<"Exit"<<endl;
break;
default:
cout<<"Error....Wrong Choice Entered"<<endl;
}
} while (ch!=6);
return 0;
}ผลลัพธ์
ผลลัพธ์ของโปรแกรมข้างต้นจะเป็นดังนี้:
1)Insert Element into the Vector 2)Delete Last Element of the Vector 3)Print size of the Vector 4)Display Vector elements 5)Clear the Vector 6)Exit Enter your Choice: 1 Enter value to be inserted: 5 Enter your Choice: 1 Enter value to be inserted: 2 Enter your Choice: 1 Enter value to be inserted: 8 Enter your Choice: 1 Enter value to be inserted: 6 Enter your Choice: 3 Size of Vector: 4 Enter your Choice: 4 Displaying Vector Elements: 5 2 8 6 Enter your Choice: 2 Last Element is deleted. Enter your Choice: 3 Size of Vector: 3 Enter your Choice: 4 Displaying Vector Elements: 5 2 8 Enter your Choice: 5 Vector Cleared Enter your Choice: 3 Size of Vector: 0 Enter your Choice: 4 Displaying Vector Elements: Enter your Choice: 9 Error....Wrong Choice Entered Enter your Choice: 6 Exit
ในโปรแกรมนี้ เริ่มต้นด้วยการนิยามเวกเตอร์ จากนั้นจัดทำเมนูให้ผู้ใช้เลือกการดำเนินการกับเวกเตอร์ โค้ดส่วนนี้มีดังนี้:
vector<int> vec; cout<<"1)Insert Element into the Vector"<<endl; cout<<"2)Delete Last Element of the Vector"<<endl; cout<<"3)Print size of the Vector"<<endl; cout<<"4)Display Vector elements"<<endl; cout<<"5)Clear the Vector"<<endl; cout<<"6)Exit"<<endl;
ใช้ลูป do-while เพื่อรับตัวเลือกจากผู้ใช้ และใช้คำสั่ง switch เพื่อดำเนินการตามตัวเลือกที่เลือก การดำเนินการต่างๆ ได้แก่ การแทรกองค์ประกอบ ลบองค์ประกอบ แสดงขนาด แสดงองค์ประกอบ ล้างเวกเตอร์ และออกจากโปรแกรม โค้ดสำหรับส่วนนี้มีดังนี้:
do {
cout<<"Enter your Choice: "<<endl;
cin>>ch;
switch(ch) {
case 1:
cout<<"Enter value to be inserted: "<<endl;
cin>>val;
vec.push_back(val);
break;
case 2:
cout<<"Last Element is deleted."<<endl;
vec.pop_back();
break;
case 3:
cout<<"Size of Vector: ";
cout<<vec.size()<<endl;
break;
case 4:
cout<<"Displaying Vector Elements: ";
for (int i = 0; i < vec.size(); i++)
cout<<vec[i]<<" ";
cout<<endl;
break;
case 5:
vec.clear();
cout<<"Vector Cleared"<<endl;
break;
case 6:
cout<<"Exit"<<endl;
break;
default:
cout<<"Error....Wrong Choice Entered"<<endl;
}
} while (ch!=6);