C++ STL का परिचय
In this page:
#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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
STL Iterators
Iterators smart-pointer-जैसे objects हैं जो एक container के elements में एक बार में एक चलने के लिए इस्तेमाल होते हैं, हर container type को — चाहे यह internally कितने भी अलग तरीके से implemented हो — algorithms से traverse होने का एक consistent, uniform तरीका देते हुए।
उदाहरण: STL Iterators
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- इसका header include किए बिना एक container इस्तेमाल करना, जैसे
<vector>या<map>। - एक
vectorको एक fixed-size array से confuse करना। - एक iterator type इस्तेमाल करना जो container से match न करे।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: