الگوریتمهای STL
آشنایی با الگوریتمهای مهم STL برای جستجو، مرتبسازی و پردازش داده
الگوریتمهای STL
الگوریتمهای STL توابع آمادهای برای عملیات رایج روی دادهها هستند که در هدر <algorithm> تعریف شدهاند.
الگوریتمهای مرتبسازی
sort
#include <algorithm>
#include <vector>
using namespace std;
vector<int> numbers = {5, 2, 8, 1, 9};
sort(numbers.begin(), numbers.end());
// {1, 2, 5, 8, 9}
stable_sort
vector<pair<int, string>> data = {{3, "C"}, {1, "A"}, {2, "B"}};
stable_sort(data.begin(), data.end());
// حفظ ترتیب عناصر مساوی
الگوریتمهای جستجو
find
vector<int> numbers = {1, 2, 3, 4, 5};
auto it = find(numbers.begin(), numbers.end(), 3);
if (it != numbers.end()) {
cout << "پیدا شد در موقعیت: " << (it - numbers.begin());
}
binary_search
vector<int> numbers = {1, 2, 3, 4, 5};
bool found = binary_search(numbers.begin(), numbers.end(), 3);
lower_bound / upper_bound
vector<int> numbers = {1, 2, 3, 3, 4, 5};
auto lower = lower_bound(numbers.begin(), numbers.end(), 3);
auto upper = upper_bound(numbers.begin(), numbers.end(), 3);
الگوریتمهای تغییر
reverse
vector<int> numbers = {1, 2, 3, 4, 5};
reverse(numbers.begin(), numbers.end());
// {5, 4, 3, 2, 1}
rotate
vector<int> numbers = {1, 2, 3, 4, 5};
rotate(numbers.begin(), numbers.begin() + 2, numbers.end());
// {3, 4, 5, 1, 2}
shuffle
#include <random>
vector<int> numbers = {1, 2, 3, 4, 5};
random_device rd;
mt19937 g(rd());
shuffle(numbers.begin(), numbers.end(), g);
الگوریتمهای پرس و جو
count
vector<int> numbers = {1, 2, 3, 2, 4, 2};
int count = std::count(numbers.begin(), numbers.end(), 2);
// 3
count_if
vector<int> numbers = {1, 2, 3, 4, 5};
int count = count_if(numbers.begin(), numbers.end(), [](int x) {
return x > 3;
});
// 2
all_of / any_of / none_of
vector<int> numbers = {2, 4, 6, 8, 10};
bool allEven = all_of(numbers.begin(), numbers.end(), [](int x) {
return x % 2 == 0;
});
// true
الگوریتمهای تغییر مقدار
transform
vector<int> numbers = {1, 2, 3, 4, 5};
vector<int> squared;
transform(numbers.begin(), numbers.end(), back_inserter(squared), [](int x) {
return x * x;
});
// {1, 4, 9, 16, 25}
for_each
vector<int> numbers = {1, 2, 3, 4, 5};
for_each(numbers.begin(), numbers.end(), [](int x) {
cout << x << " ";
});
الگوریتمهای مجموع
accumulate
#include <numeric>
vector<int> numbers = {1, 2, 3, 4, 5};
int sum = accumulate(numbers.begin(), numbers.end(), 0);
// 15
min_element / max_element
vector<int> numbers = {5, 2, 8, 1, 9};
auto minIt = min_element(numbers.begin(), numbers.end());
auto maxIt = max_element(numbers.begin(), numbers.end());
cout << "Min: " << *minIt << ", Max: " << *maxIt;
بهترین شیوهها
- از الگوریتمهای STL به جای پیادهسازی دستی استفاده کنید
- از lambda expressions برای سادگی استفاده کنید
- الگوریتمها را روی iteratorها اعمال کنید
- از back_inserter برای افزودن به کانتینر استفاده کنید
نکات مهم
- الگوریتمها روی iteratorها کار میکنند
- بسیاری از الگوریتمها O(n) یا O(n log n) هستند
- میتوانید از lambda expressions استفاده کنید
- همیشه
<algorithm>را include کنید
این مبحث بخشی از دوره آموزش کامل C++ است.