C++ के STL Algorithms
In this page:
#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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Vectors Modify करना
std::reverse और std::replace जैसे modifying algorithms आपको एक container के contents को खुद एक manual loop लिखे बिना in place transform करने देते हैं — element order reverse करना, या एक value के हर occurrence को दूसरे के लिए swap करना।
उदाहरण: Modifying Vectors
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
std::sortयाstd::findइस्तेमाल करने से पहले#include <algorithm>भूल जाना।- एक
std::listपरstd::sortcall करना, जो compile नहीं होता क्योंकि इसे random access iterators चाहिए। - एक range pass करना जहाँ पहला iterator last के बाद आता है, जो undefined behavior है।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: