← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 2 of 15

C++ का vector

एक vector एक list है जो items add या remove करने पर बढ़ती और सिकुड़ती है, एक stretchy shopping list की तरह। आप किसी भी item को इसकी position से पहुँच सकते हैं।
Syntax
cpp
#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?

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. अंत से आगे v[i] इस्तेमाल करना, जो undefined behavior है; v.at(i) इसकी बजाय throw करता है।
  2. push_back के बाद एक iterator या reference रखना vector को reallocate कराता है, जो इसे invalidate कर देता है।
  3. size() (stored elements) को capacity() (reserved space) से confuse करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.