C++ का vector
In this page:
#include <vector>
std::vector<data_type> vector_name = {value1, value2};
vector_name.push_back(value);
vector_name.pop_back();
vector_name[index];
vector क्या है?
एक vector C++ का dynamic array template class है: एक fixed-size raw array के उलट, एक vector elements add या remove होने पर automatically बढ़ता या सिकुड़ता है, अपनी underlying heap memory खुद manage करते हुए बिना आपको खुद new या delete call करने की ज़रूरत के।
उदाहरण: What is a vector?
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums;
nums.push_back(1);
nums.push_back(2);
// Print to the console with cout
std::cout << nums.size() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Elements Add और Remove करना
आप push_back() से एक vector के अंत में एक element add करते हैं और pop_back() से last element remove करते हैं; दोनों operations automatically vector की reported size adjust करते हैं, इसलिए आप कभी manually track नहीं करते कि इसमें currently कितने elements हैं।
उदाहरण: Adding and Removing Elements
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {1, 2};
nums.push_back(3);
nums.pop_back();
// Print to the console with cout
std::cout << nums.size() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Elements Access करना
आप vec[i] जैसे plain index brackets से, या at() method से एक element access कर सकते हैं, जो safer है क्योंकि यह bounds checking perform करता है और एक bad index के लिए चुपचाप invalid memory पढ़ने की बजाय एक out_of_range exception throw करता है।
उदाहरण: Accessing Elements
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {10, 20, 30};
// Print to the console with cout
std::cout << nums[1] << " " << nums.at(2) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Vector Capacity बनाम Size
Size elements की संख्या है जो एक vector actually currently रखता है, जबकि capacity इसने reserve की total memory है, जो अक्सर बड़ी होती है।
Vectors जानबूझकर capacity पहले से over-allocate करते हैं ताकि ज़्यादातर push_back() calls को एक expensive reallocation trigger करने की ज़रूरत न पड़े।
उदाहरण: Vector Capacity vs Size
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {1, 2, 3};
// Print to the console with cout
std::cout << "size=" << nums.size() << " capacity=" << nums.capacity() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Vectors पर Iterate करना
आप एक vector के elements पर एक traditional index-based loop, cleaner syntax के लिए एक range-based for loop, या explicit STL iterators से iterate कर सकते हैं — तीनों interchangeably काम करते हैं और choice ज़्यादातर readability और आपको index खुद चाहिए या नहीं इस पर आता है।
उदाहरण: Iterating over Vectors
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {1, 2, 3};
// Loop: repeat while the condition holds
for (int i = 0; i < nums.size(); i++) std::cout << nums[i] << " ";
for (int n : nums) std::cout << n << " ";
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- अंत से आगे
v[i]इस्तेमाल करना, जो undefined behavior है;v.at(i)इसकी बजाय throw करता है। push_backके बाद एक iterator या reference रखना vector को reallocate कराता है, जो इसे invalidate कर देता है।size()(stored elements) कोcapacity()(reserved space) से confuse करना।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: