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

C++ के STL Algorithms

STL algorithms एक collection में items sort, search, और count करने वाले ready-made tools हैं, आपकी list के लिए helpers के एक set की तरह। आपको loops खुद नहीं लिखने पड़ते।
Syntax
cpp
#include <algorithm>

std::sort(container.begin(), container.end());
auto position = std::find(container.begin(), container.end(), value);

std::sort से Sorting

std::sort एक container के elements को default से ascending order में rearrange करता है, अंदर से एक efficient introsort implementation इस्तेमाल करते हुए, और जब आपको अलग ordering चाहिए तो एक optional custom comparison function accept करता है।

उदाहरण: Sorting with std::sort

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, 2, 8, 1};
	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;
}

std::find से Finding

std::find एक दी गई value के equal पहले element के लिए एक range search करता है और इसकी ओर point करने वाला एक iterator return करता है — या अगर कोई match exist न करे तो range के end के बराबर एक iterator, जिसे आप result dereference करने से पहले check करते हैं।

उदाहरण: Finding with std::find

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

std::count से Counting

std::count आपको exactly बताता है कि एक range के अंदर एक specific value कितनी बार appear होती है, जबकि इसका companion count_if आपको एक exact value match करने की बजाय एक custom predicate satisfy करने वाले elements count करने देता है।

उदाहरण: Counting with std::count

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

Vectors Modify करना

std::reverse और std::replace जैसे modifying algorithms आपको एक container के contents को खुद एक manual loop लिखे बिना in place transform करने देते हैं — element order reverse करना, या एक value के हर occurrence को दूसरे के लिए swap करना।

उदाहरण: Modifying Vectors

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 = {1, 2, 3};
	std::reverse(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;
}

Min और Max Functions

Standard library individual values compare करने के लिए simple std::min और std::max functions भी देती है, एक पूरी range में सबसे छोटा या सबसे बड़ा element ढूंढने के लिए std::min_element और std::max_element के साथ।

उदाहरण: Min and Max Functions

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, 2, 8, 1};
	auto maxIt = std::max_element(nums.begin(), nums.end());
	// Print to the console with cout
	std::cout << *maxIt << 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. std::sort या std::find इस्तेमाल करने से पहले #include <algorithm> भूल जाना।
  2. एक std::list पर std::sort call करना, जो compile नहीं होता क्योंकि इसे random access iterators चाहिए।
  3. एक range pass करना जहाँ पहला iterator last के बाद आता है, जो undefined behavior है।

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.