Vector in C++ STL
C++ में std::vector क्या है?
std::vector<T> growable sequence container है। Ordinary types में elements contiguous रहते हैं, array जैसी indexing मिलती है और memory/lifetime automatic manage होती है।
#include <vector>
std::vector<int> marks{78,91,84};| Property | Guarantee |
|---|---|
| Index access | O(1) |
| End insertion | Amortized O(1) |
| Middle insert/erase | O(n) |
| Storage | vector<bool> छोड़कर contiguous |
Vector Create और Initialize करें
std::vector<int> empty;
std::vector<int> five_zeros(5);
std::vector<int> four_sevens(4,7);
std::vector<int> values{5,10,15};
std::vector copy=values;vector<int>(5,10) पांच बार 10 रखता है, जबकि {5,10} दो values रखता है। Related fields के parallel vectors के बजाय record रखें:
struct Student{std::string name;int mark;};
std::vector<Student> students{{"Aman",78},{"Sara",91}};Direct headers include करें और headers/examples में using namespace std; avoid करें।
Elements Safely Access करें
| Method | Use |
|---|---|
v[i] | No bounds check |
v.at(i) | Invalid पर out_of_range |
front() | First, vector non-empty हो |
back() | Last, vector non-empty हो |
data() | Contiguous range pointer |
if(!marks.empty()) std::cout<<marks.front();
try{ std::cout<<marks.at(user_index); }
catch(const std::out_of_range&){
std::cerr<<"Invalid index\n";
}Last valid index non-empty vector में size()-1 है। Unsigned reverse loop में underflow से बचें।
Essential Vector Methods
| Method | Effect |
|---|---|
| push_back | End पर value |
| emplace_back | End पर construct |
| pop_back | Last remove; empty नहीं |
| insert | Position से पहले insert |
| erase | Element/range remove |
| clear | All elements destroy, capacity रह सकती है |
| swap | Contents/capacity exchange |
marks.push_back(96);
marks.emplace_back(88);
marks.insert(marks.begin()+1,80);
marks.erase(marks.begin()+2);
auto removed=std::erase_if(marks,
[](int m){return m<40;}); // C++20pop_back value return नहीं करता। Already-created object में move और emplace के बीच automatic speed assumption न करें।
Size, Capacity, reserve और resize
std::vector<int> values;
values.reserve(100); // size 0
values.push_back(7); // size 1
values.resize(4); // 7 0 0 0size(): live elements।capacity(): reallocation से पहले storage limit।reserve(n): capacity request, size unchanged।resize(n): element count change।shrink_to_fit(): non-binding request।
Known final size पर एक बार reserve उपयोगी है; हर push से पहले reserve growth strategy खराब कर सकता है। Doubling guaranteed नहीं है।
Iteration और Invalidation
for(int mark:marks) std::cout<<mark<<' ';
for(const Student& s:students) std::cout<<s.name;
std::sort(marks.begin(),marks.end());- Reallocation सभी iterators/pointers/references invalid करती है।
- No reallocation में insert point से आगे handles invalid।
- Erase position और उसके बाद handles invalid।
- reserve केवल reallocation होने पर invalidates।
auto it=marks.begin();
marks.push_back(99); // it invalid हो सकता है
it=marks.begin();iterators और algorithms आगे पढ़ें।
Complete Practical Program और Output
std::vector<int> marks;
marks.reserve(6);
for(int m:{72,88,65,91,88}) marks.push_back(m);
int total=std::accumulate(marks.begin(),marks.end(),0);
int highest=*std::max_element(marks.begin(),marks.end());
std::sort(marks.begin(),marks.end());Reserve ने elements नहीं बनाए, केवल insertion के लिए capacity दी। Total 404 है और floating-point division से average 80.8 आया।
Mistakes, Practice और Review
| Mistake | Correction |
|---|---|
| Index == size | Out of range |
| reserve के बाद index | push/resize करें |
| Empty front/back/pop | empty check |
| Growth के बाद iterator | Reacquire |
| clear frees capacity | Capacity रह सकती है |
| Raw owning pointers | Values/smart owners |
Practice: duplicates remove करें; Student records mark के अनुसार sort करें; reserve से पहले/बाद size-capacity दिखाएं; negative values erase करें; at और [] compare करें।
- Header included?
- Index valid?
- Empty case?
- Size/capacity clear?
- Invalidation checked?
- Algorithm preconditions valid?
Authoritative संदर्भ
Complexity, reserve/resize और invalidation current working draft से verified हैं। Complete STL guide भी पढ़ें।
