Vector က C++ Standard Template Library (STL) ထဲက dynamic array ပါ။ Array လို index နဲ့ access လုပ်နိုင်ပြီး item အရေအတွက်ကို runtime မှာ တိုး/လျော့လုပ်နိုင်ပါတယ်။ User input အရ item အရေအတွက်မသိတဲ့အခါ vector က array ထက် ပိုသင့်တော်ပါတယ်။
cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
vector<string> tasks;
tasks.push_back("Learn variables");
tasks.push_back("Practice loops");
tasks.push_back("Build a mini project");
for (string task : tasks) {
cout << "- " << task << endl;
}
cout << "Total tasks: " << tasks.size();
return 0;
}vector<string> က string item တွေသိမ်းမယ့် vector ပါ။ push_back() က item အသစ်ထည့်တာဖြစ်ပြီး range-based for loop နဲ့ item အားလုံးကို ဖတ်ရလွယ်အောင်ထုတ်ထားပါတယ်။ size() က item အရေအတွက်ကို ပြန်ပေးပါတယ်။
You should see
- Learn variables - Practice loops - Build a mini project Total tasks: 3Info
Modern C++ မှာ fixed-size မလိုတဲ့ list data အတွက် vector ကို အလွန်အသုံးများပါတယ်။ Array ထက် flexible ဖြစ်ပြီး STL algorithm တွေနဲ့လည်း တွဲသုံးလို့ကောင်းပါတယ်။