STL Algorithms
STL Algorithm Model
Container elements own करता है, iterators range बताते हैं और algorithm operation करता है। इससे same find/count_if कई suitable containers पर reusable होता है।
std::vector<int> values{7,2,9,4};
std::sort(values.begin(),values.end());| Family | Examples |
|---|---|
| Queries | find, count, all_of |
| Transform | copy, transform, replace |
| Reorder | reverse, rotate, shuffle |
| Sort/search | sort, lower_bound |
| Numeric | accumulate, iota |
Most algorithms के लिए <algorithm>, numeric के लिए <numeric> direct include करें। STL guide देखें।
Ranges और Iterator Requirements
[first,last) first include और last exclude करती है; end को dereference न करें।
| Algorithm | Iterator |
|---|---|
| find/count | Input |
| copy | Readable + writable output |
| reverse | Bidirectional |
| sort | Random access |
इसलिए std::sort vector पर चलता है, list पर नहीं। C++20 range algorithms whole range और projection ले सकते हैं:
std::ranges::sort(students,std::greater{},&Student::mark);Lazy views source को refer कर सकती हैं; source lifetime पर्याप्त हो। iterators पढ़ें।
Search, Count और Predicates
auto found=std::find(values.begin(),values.end(),9);
if(found!=values.end()) std::cout<<*found;
auto evens=std::count_if(values.begin(),values.end(),
[](int n){return n%2==0;});
bool positive=std::all_of(values.begin(),values.end(),
[](int n){return n>0;});- find/find_if first match या end।
- count/count_if match count।
- all_of सभी, any_of कोई एक, none_of कोई नहीं।
- mismatch first different pair।
Predicate range invalidate न करे; simple, stable lambda रखें।
Transform, Copy और Remove
std::vector<int> doubled(values.size());
std::transform(values.begin(),values.end(),doubled.begin(),
[](int n){return n*2;});
std::vector<int> result;
std::copy_if(values.begin(),values.end(),
std::back_inserter(result),[](int n){return n>=0;});Destination में space हो या insertion iterator लें। Erase-remove:
auto end=std::remove_if(values.begin(),values.end(),
[](int n){return n<0;});
values.erase(end,values.end());
// C++20: std::erase_if(values,predicate);remove_if vector size नहीं घटाता; logical end देता है।
Sorting और Binary Search
std::sort(values.begin(),values.end());
bool found=std::binary_search(values.begin(),values.end(),75);
auto first=std::lower_bound(values.begin(),values.end(),75);- sort equal order preserve नहीं करता।
- stable_sort equivalent order preserve करता है।
- partial_sort selected first part sort करता है।
- nth_element nth को sorted position पर रखता है।
- lower/upper_bound equivalent range की boundaries देते हैं।
Comparator strict weak ordering दे; <= गलत। Binary search के लिए compatible ordering/partition जरूरी है।
Numeric Algorithms और Types
long long total=std::accumulate(values.begin(),values.end(),0LL);
std::vector<int> ids(5);
std::iota(ids.begin(),ids.end(),101);accumulate का initial value result type तय करता है: large integer sum में 0LL और floating case में 0.0 सोचकर चुनें।
| Algorithm | Use |
|---|---|
| accumulate | Fold/sum |
| iota | Increasing fill |
| inner_product | Pairwise fold |
| partial_sum | Running totals |
| reduce | May reorder operations |
Complete Score Analysis और Output
std::vector<int> scores{64,91,75,52,88};
int total=std::accumulate(scores.begin(),scores.end(),0);
auto passed=std::count_if(scores.begin(),scores.end(),
[](int s){return s>=75;});
std::sort(scores.begin(),scores.end(),std::greater<>{});Total 370 है। Pass predicate 75 include करता और comparator descending order देता है।
Mistakes, Practice और Review
| Mistake | Correction |
|---|---|
| Unsorted binary search | Compatible order |
| list पर std::sort | list::sort |
| Returned end dereference | Check first |
| Destination no space | Resize/back_inserter |
| remove shrinks vector | Erase tail |
| Comparator <= | Strict weak order |
| Initial type wrong | 0LL/0.0 |
Practice: Student two-key sort, first failing mark, invalid score removal, running totals, top three और ranges comparison।
- Header?
- Valid range?
- Iterator capability?
- Precondition?
- Comparator?
- Destination?
- Return checked?
- Numeric type?
Authoritative संदर्भ
Requirements और preconditions current draft से verified हैं। vector तथा sorting/searching पर practice करें।
