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

C++ STL का परिचय

STL containers, algorithms, और iterators का C++ का built-in toolbox है, ready-made storage boxes और tools से भरे एक store की तरह। यह आपको common चीज़ें scratch से लिखने से बचाता है।
Syntax
cpp
#include <container_header>

std::container_name<data_type> variable_name;

std::algorithm_name(variable_name.begin(), variable_name.end());

STL क्या है?

Standard Template Library (STL) C++ में built-in template classes और functions का एक बड़ा set है जो pre-written, highly optimized data structures और algorithms देता है, sorting या dynamic arrays जैसी basic utilities को scratch से लिखने से आपको बचाते हुए।

उदाहरण: What is the STL?

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 = {3, 1, 2};
	// Print to the console with cout
	std::cout << nums[0] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Containers का Overview

Containers template classes हैं जो data के collections store और manage करने के लिए design किए गए हैं, broadly vector और deque जैसे sequence containers में बंटे हुए, जो insertion order preserve करते हैं, और set और map जैसे associative containers, जो elements को key से organize करते हैं।

उदाहरण: Containers Overview

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <map>
#include <string>

// Program execution starts in main()
int main() {
	// Create a vector named sequence (a resizable array)
	std::vector<int> sequence = {1, 2, 3};
	std::map<std::string, int> lookup = {{"a", 1}};
	// Print to the console with cout
	std::cout << sequence[0] << " " << lookup["a"] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

STL Algorithms का Intro

STL algorithms free template functions हैं — containers के member functions नहीं — जो elements की ranges पर operate करके find, sort, copy, या search करते हैं, जिसका मतलब है same algorithm कई अलग container types में uniformly काम करता है।

उदाहरण: STL Algorithms Intro

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {3, 1, 2};
	std::sort(nums.begin(), nums.end());
	// Print to the console with cout
	std::cout << nums[0] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

STL Iterators

Iterators smart-pointer-जैसे objects हैं जो एक container के elements में एक बार में एक चलने के लिए इस्तेमाल होते हैं, हर container type को — चाहे यह internally कितने भी अलग तरीके से implemented हो — algorithms से traverse होने का एक consistent, uniform तरीका देते हुए।

उदाहरण: STL Iterators

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 (auto it = nums.begin(); it != nums.end(); ++it) {
		// Print to the console with cout
		std::cout << *it << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

STL के Benefits

hand-rolled utilities की बजाय STL पर depend करना productivity और reliability दोनों improve करता है: क्योंकि यह compiler और library specialists से लिखी और maintained है, इसके containers और algorithms extensively tested, memory-safe, और highly optimized हैं।

उदाहरण: Benefits of the STL

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {5, 3, 1, 4};
	std::sort(nums.begin(), nums.end());
	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. इसका header include किए बिना एक container इस्तेमाल करना, जैसे <vector> या <map>।
  2. एक vector को एक fixed-size array से confuse करना।
  3. एक iterator type इस्तेमाल करना जो container से match न करे।

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.