ရိုးရိုးလေး ရှင်းပြမယ်
Algorithm၊ lambda နဲ့ range ကို ရိုးရိုးလေး ပြောရရင်
သင်ရေးတဲ့ loop အများစုက အလုပ် အနည်းငယ်ထဲက တစ်ခုခုကိုပဲ လုပ်ပါတယ်: တစ်ခုခု ရှာ၊ ရေတွက်၊ sort လုပ်၊ item တိုင်းကို ပြောင်း၊ အားလုံးကို ပေါင်း။ Standard library မှာ ဒီအလုပ်တစ်ခုစီအတွက် စမ်းသပ်ပြီးသား နာမည်ပေးထားတဲ့ function တွေ <algorithm> နဲ့ <numeric> ထဲမှာ ရှိပြီးသားပါ။ std::ranges::count_if ကို ခေါ်လိုက်တာက ရည်ရွယ်ချက်ကို line တစ်ကြောင်းတည်းနဲ့ ပြောပြီး loop ကတော့ ဖတ်တဲ့သူကို ပြန်ခွဲခြမ်းစိတ်ဖြာခိုင်းပါတယ်။
Lambda က လိုအပ်တဲ့နေရာမှာတင် ရေးတဲ့ နာမည်မဲ့ function လေးပါ၊ ဥပမာ 'ဒီ order က ကြီးလား' ဆိုတဲ့ စည်းမျဉ်း။ View ကတော့ sequence တစ်ခုအတွက် ချက်ပြုတ်နည်းပါ: 'ကြီးတဲ့ order တွေရဲ့ customer နာမည်များ'။ သင် တကယ် ဖြတ်လျှောက်ဖတ်မှသာ ဘာမဆို တွက်ချက်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: loop သုံးခု၊ ယာယီ vector နှစ်ခု၊ bug တစ်ခု
Report တစ်ခုက 100 နဲ့ အထက် order တွေရဲ့ customer နာမည်တွေကို total အလိုက် စီပြီး လိုချင်ပါတယ်။ ပထမ version က loop သုံးခု၊ ယာယီ vector နှစ်ခုနဲ့ ကိုယ်တိုင်ရေးထားတဲ့ bubble sort ပါ။ Bug က loop bound မှာပါ: < အစား <= ကို သုံးမိလို့ vector အဆုံးကို တစ်ခု ကျော်ဖတ်ပါတယ်။ သင့်စက်မှာ pass ဖြစ်ပြီး production မှာ crash ဖြစ်တတ်တဲ့ bug မျိုးပါ။
std::vector<Order> large;
for (std::size_t i = 0; i <= orders.size(); ++i) { // <= reads past the end
if (orders[i].total >= 100) large.push_back(orders[i]);
}
for (std::size_t i = 0; i < large.size(); ++i) { // hand-written bubble sort
for (std::size_t j = 0; j + 1 < large.size() - i; ++j) {
if (large[j].total > large[j + 1].total) std::swap(large[j], large[j + 1]);
}
}
std::vector<std::string> names;
for (const Order& o : large) names.push_back(o.customer);Loop တွေ ဘာကြောင့် မှားလွယ်တာလဲ
ကိုယ်တိုင်ရေးတဲ့ loop တိုင်းက သူ့ကိုယ်ပိုင် bound၊ index variable နဲ့ မှားနိုင်ခြေ ရှိတဲ့ program အသေးလေး တစ်ခုပါ။ 'ကြီးတဲ့ order တွေရဲ့ နာမည်များ၊ total အလိုက်' ဆိုတဲ့ ရည်ရွယ်ချက်က အသေးစိတ် လုပ်ဆောင်ချက်တွေအောက်မှာ မြုပ်နေပါတယ်။ Standard algorithm တွေမှာ မှားစရာ index မရှိဘဲ ရည်ရွယ်ချက်ကို နာမည်နဲ့ ပြောပြီး C++ programmer တိုင်းက စမ်းသပ်ပြီးသား ဖြစ်ပါတယ်။ Range နဲ့ view တွေက ယာယီ vector တွေကိုပါ ဖယ်ရှားပေးပါတယ်။
Core concept: verb၊ စည်းမျဉ်း နဲ့ ချက်ပြုတ်နည်း
Classic algorithm တွေက iterator တစ်စုံကို ယူပါတယ်: std::sort(v.begin(), v.end())။ C++20 std::ranges version တွေကတော့ container ကို တိုက်ရိုက် ယူပြီး projection ကိုလည်း လက်ခံပါတယ်: std::ranges::sort(orders, {}, &Order::total) က total အလိုက် စီပြီး {} က default comparison ဖြစ်တဲ့ std::ranges::less ကို ဆိုလိုပါတယ်။ sort က random-access iterator လိုလို့ vector၊ array၊ deque တွေမှာ အလုပ်လုပ်ပြီး std::list မှာ မလုပ်ပါဘူး။ Comparator က strict weak ordering ဖြစ်ရပါမယ်: < ကို သုံးပါ၊ <= ကို ဘယ်တော့မှ မသုံးပါနဲ့။
Lambda ရဲ့ ပုံစံက [captures](parameters) { body } ဖြစ်ပြီး return type ကို များသောအားဖြင့် deduce လုပ်ပါတယ်။ Lambda က object တစ်ခုပါ: [x] က lambda ဖန်တီးတဲ့အချိန်မှာ x ကို copy ကူးပြီး အထဲမှာ သိမ်းထားလို့ နောက်ပိုင်း x ပြောင်းလည်း lambda က မမြင်ပါဘူး။ [&x] ကတော့ မူလ variable ကို ညွှန်တဲ့အတွက် lambda သုံးနေသမျှ x ရှင်နေရပါမယ်။ Copy ကို lambda ထဲမှာ ပြင်ချင်ရင် mutable ကို ထည့်ပါ။
| Capture | အဓိပ္ပာယ် | လုံခြုံတဲ့အချိန် |
|---|---|---|
| [threshold] | ဖန်တီးချိန်မှာ copy | အမြဲ၊ lambda က ကိုယ်ပိုင် copy ကို ပိုင် |
| [&sum] | sum ဆီ reference | sum ရှင်နေစဉ် lambda ကို သုံးတဲ့အခါ |
| [=] / [&] | သုံးသမျှ အားလုံး copy / reference | ချက်ချင်း ခေါ်တဲ့ lambda တိုလေးတွေ |
| [this] | လက်ရှိ object | object က lambda ထက် ကြာကြာ ရှင်တဲ့အခါ |
<ranges> ထဲက view တွေ (std::views::filter၊ transform၊ take၊ reverse) ကို | နဲ့ ဆက်ချိတ်ပါတယ်။ View က element တွေကို မပိုင်ဘဲ ဖန်တီးတဲ့အချိန်မှာလည်း ဘာမှ မတွက်ပါဘူး၊ ဒါကို lazy လို့ ခေါ်ပါတယ်: loop က element တစ်ခု တောင်းမှ အဲဒီ element ကို ထုတ်ပေးပြီး တောင်းသလောက်ပဲ ထုတ်ပါတယ်။ View က အောက်က container ကို ညွှန်နေတဲ့အတွက် container က view ထက် ကြာကြာ ရှင်ရပါမယ်။
ရလဒ်ကို အခုချက်ချင်း လိုရင် (sort တစ်ခု၊ count တစ်ခု) algorithm ကို ရွေးပါ။ တစ်ခါ ဖြတ်ဖတ်မယ့် sequence ကို ယာယီ vector မဆောက်ဘဲ ဖော်ပြချင်ရင် view ကို ရွေးပါ။ C++20 မှာ view ကို vector အဖြစ် ပြောင်းဖို့ loop ပတ်ပြီး push_back လုပ်ရပါတယ်၊ C++23 က std::ranges::to ကို ထပ်ထည့်ထားပါတယ်။
- Verb ကို ရွေး: sort၊ count_if၊ find_if
- စည်းမျဉ်းကို lambda အဖြစ် ပေး
- View တွေ ချိတ်ပြီး sequence ကို ဖော်ပြ
- Loop ပတ်တဲ့အခါမှ value တွေ တွက်
View နဲ့ reference capture လုပ်တဲ့ lambda နှစ်ခုလုံးက သူတို့ မပိုင်တဲ့ data ကို ညွှန်နေလို့ အဲဒီ data က သူတို့ထက် ကြာကြာ ရှင်ရပါမယ်။
အထွေထွေ ပုံစံ
std::ranges::sort(range); // default <
std::ranges::sort(range, comp, &Type::member); // comparator + projection
auto n = std::ranges::count_if(range, predicate);
auto it = std::ranges::find_if(range, predicate); // range.end() if none
auto rule = [capture](const Type& x) { return expression; };
auto view = range | std::views::filter(predicate)
| std::views::transform(function)
| std::views::take(n);
for (auto&& value : view) { /* computed here */ }Algorithm က range တစ်ခုလုံးကို ယူပြီး လိုရင် comparator နဲ့ projection ပေးပါတယ်။ Lambda က စည်းမျဉ်းကို သယ်ဆောင်ပြီး view pipeline ကတော့ loop က တောင်းမှသာ element တွေ ထုတ်ပေးပါတယ်။
Worked example: order report တစ်ခု
Problem section ထဲက report ကို algorithm တွေနဲ့ ပြန်ရေးထားပါတယ်: index variable မရှိ၊ ကိုယ်တိုင်ရေးထားတဲ့ sort မရှိ၊ ယာယီ vector မရှိပါဘူး။ isLarge ဆိုတဲ့ စည်းမျဉ်းကို တစ်ခါ ရေးပြီး count_if နဲ့ filter view နှစ်ခုလုံးမှာ ပြန်သုံးထားတာကို သတိထားကြည့်ပါ။
#include <algorithm>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
struct Order {
std::string customer;
int total;
};
int main() {
std::vector<Order> orders{{"Aye", 120}, {"Ko", 45}, {"Su", 300}, {"Min", 80}};
std::ranges::sort(orders, {}, &Order::total); // sort by total, ascending
for (const Order& o : orders) std::cout << o.customer << ' ';
std::cout << '\n';
int threshold = 100;
auto isLarge = [threshold](const Order& o) { return o.total >= threshold; };
std::cout << "large orders: " << std::ranges::count_if(orders, isLarge) << '\n';
auto largeNames = orders | std::views::filter(isLarge)
| std::views::transform(&Order::customer);
for (const std::string& name : largeNames) std::cout << name << ' ';
std::cout << '\n';
int sum = 0;
std::ranges::for_each(orders, [&sum](const Order& o) { sum += o.total; });
std::cout << "sum: " << sum << '\n';
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Sort by a member
std::ranges::sort(orders, {}, &Order::total);{} က default comparator (<) ပါ၊ &Order::total က projection ဖြစ်လို့ order တစ်ခုလုံးကို မနှိုင်းယှဉ်ဘဲ total ကိုပဲ နှိုင်းယှဉ်ပါတယ်။ Comparator lambda ရေးစရာ မလိုပါဘူး။
- Capture by value
auto isLarge = [threshold](const Order& o) { return o.total >= threshold; };[threshold] က lambda ဖန်တီးချိန်မှာ 100 ကို copy ကူးထားပါတယ်။ နောက်ပိုင်း threshold ကို ပြောင်းလည်း isLarge က 100 ကိုပဲ သုံးနေဆဲဖြစ်ပြီး lambda ကို ဘယ်နေရာ ပို့ပို့ လုံခြုံပါတယ်။
- Count with the same rule
std::ranges::count_if(orders, isLarge)count_if က element တိုင်းကို lambda နဲ့ စစ်ပြီး true ပြန်တဲ့ အရေအတွက်ကို ပြန်ပေးလို့ 2 ရပါတယ် (120 နဲ့ 300)။
- A lazy pipeline
auto largeNames = orders | std::views::filter(isLarge)ဒီ line က ဘာမှ မတွက်ပါဘူး၊ 'ကြီးတဲ့ order တွေ၊ customer နာမည်အဖြစ် ပြောင်း' ဆိုတဲ့ ချက်ပြုတ်နည်းကိုပဲ တည်ဆောက်ပါတယ်။ နောက်က for loop က element တောင်းမှ filter နဲ့ transform run ပါတယ်။
- Capture by reference to accumulate
std::ranges::for_each(orders, [&sum](const Order& o) { sum += o.total; });Lambda က sum ကို ပြင်ရမှာမို့ [&sum] နဲ့ reference capture လုပ်ပါတယ်။ for_each က ချက်ချင်း run ပြီး sum ကလည်း ရှင်နေဆဲမို့ ဒီနေရာမှာ လုံခြုံပါတယ်။
Ko Min Aye Su
large orders: 2
Aye Su
sum: 545Lazy ဖြစ်တာကို မျက်စိနဲ့ မြင်ကြည့်ခြင်း
အောက်က transform lambda က value တစ်ခု တွက်တိုင်း message တစ်ကြောင်း print ထုတ်ပါတယ်။ Vector မှာ number ခြောက်ခု ရှိပေမဲ့ pipeline က take(2) နဲ့ ဆုံးလို့ နှစ်ခုပဲ တွက်ပါတယ်၊ ပြီးတော့ အဲဒါတွေကိုလည်း view တည်ဆောက်တဲ့အချိန်မှာ မဟုတ်ဘဲ loop ပတ်တဲ့အချိန်မှာမှ တွက်ပါတယ်။
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> numbers{1, 2, 3, 4, 5, 6};
auto squares = numbers
| std::views::transform([](int n) {
std::cout << "(computing " << n << ") ";
return n * n;
})
| std::views::take(2);
std::cout << "view built, nothing computed yet\n";
for (int s : squares) std::cout << s << '\n';
}Output က "view built, nothing computed yet" ပြီးမှ "(computing 1) 1" နဲ့ "(computing 2) 4" ပါ။ squares ကို နောက်တစ်ခါ loop ပတ်ရင် lambda က ထပ် run ပါတယ်၊ view က ရလဒ်တွေကို သိမ်းမထားလို့ပါ။ တွက်ရခက်တဲ့ value တွေကို နှစ်ခါ လိုရင် vector တစ်ခုထဲ တစ်ခါ သိမ်းထားပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ algorithm နဲ့ lambda
Modern C++ codebase တွေမှာ ကိုယ်တိုင်ရေးတဲ့ loop ထက် algorithm ခေါ်တာ ပိုများပါတယ်၊ code review တွေမှာလည်း 'ဒါ std::ranges::any_of ပဲ မဟုတ်လား' ဆိုတဲ့ comment ကို မကြာခဏ တွေ့ရပါတယ်:
- Data processing: log line တွေကို level အလိုက် filter လုပ်၊ message ကို transform လုပ်ပြီး ပထမ အခု ၅၀ ကိုပဲ take လုပ်တာမျိုးကို ယာယီ copy မရှိဘဲ view pipeline တစ်ခုနဲ့ ရေးပါတယ်။
- UI table တွေ: user က column တစ်ခုကို နှိပ်တဲ့အခါ projection နဲ့ sort လုပ်ပါတယ်၊ ဥပမာ std::ranges::sort(rows, {}, &Row::price)။
- Callback နဲ့ event handler တွေ: button၊ timer၊ network reply တွေက lambda ကို သိမ်းထားပြီး နောက်မှ ခေါ်ပါတယ်။ ဒီနေရာမှာ capture ရဲ့ lifetime က အရေးအကြီးဆုံးပါ၊ object ပျက်ပြီးမှ [this] capture လုပ်ထားတဲ့ callback run ရင် undefined behaviour ဖြစ်ပြီး crash ဖြစ်လေ့ရှိပါတယ်။
- Performance: classic algorithm အများစုက C++17 ကစပြီး std::execution::par လို execution policy ကို လက်ခံလို့ loop ကို ပြန်မရေးဘဲ parallel run နိုင်ပါတယ်။
အမှားများ
Algorithm၊ lambda၊ range တွေမှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ
အောက်က code မှာ အမှားနှစ်ခုစလုံးက GCC ရဲ့ default optimisation level မှာ -Wall -Wextra နဲ့ပါ warning မပြဘဲ compile ဖြစ်ပါတယ်:
auto makeAdder(int amount) {
return [&amount](int x) { return x + amount; }; // reference to a parameter
}
auto evenNumbers() {
std::vector<int> local{1, 2, 3, 4, 5, 6};
return local | std::views::filter([](int n) { return n % 2 == 0; });
} // local is destroyed here, but the view still points at itပျက်သွားမယ့် variable ကို reference နဲ့ capture လုပ်ခြင်း
makeAdder ပြီးတာနဲ့ amount ပျက်သွားပေမဲ့ ပြန်ပေးလိုက်တဲ့ lambda က သူ့ကို ညွှန်နေဆဲပါ၊ နောက်မှ ခေါ်ရင် undefined behaviour ပါ။ Lambda က ဖန်တီးတဲ့ scope ထက် ကြာကြာ ရှင်မယ်ဆိုရင် [amount] နဲ့ value capture လုပ်ပါ။
Local container ပေါ်က view ကို return ပြန်ခြင်း
View က element တွေကို မပိုင်လို့ evenNumbers ပြီးတာနဲ့ local ပျက်ပြီး view က dangling ဖြစ်ပါတယ်။ Container ကိုယ်တိုင်ကို return ပြန်ပါ၊ ဒါမှမဟုတ် data ကို ပိုင်တဲ့ ခေါ်သူဆီကနေ reference နဲ့ လက်ခံပြီး view ကို အဲဒီပေါ်မှာ တည်ဆောက်ပါ။
Comparator မှာ <= ကို သုံးခြင်း
[](int a, int b) { return a <= b; } က strict weak ordering မဟုတ်ပါဘူး (တူညီတဲ့ element နှစ်ခုအတွက် a<b ရော b<a ရော true ပြန်လို့)။ sort ကို ပေးရင် undefined behaviour ဖြစ်ပြီး ညီမျှတဲ့ value များတဲ့ input တွေမှာ array ပြင်ပ ဖတ်တာ ဒါမှမဟုတ် crash ဖြစ်နိုင်ပါတယ်။ < ကို သုံးပါ၊ ဒါမှမဟုတ် std::ranges::greater{} လို standard comparator ကို သုံးပါ။
View က ရလဒ်ကို cache လုပ်ထားမယ်လို့ ထင်ခြင်း
Transform view ကို ပတ်တိုင်း function ကို ထပ်ခေါ်ပါတယ်။ Lambda ထဲမှာ side effect ဒါမှမဟုတ် လေးလံတဲ့ အလုပ် ရှိရင် နှစ်ခါ loop ပတ်တာက နှစ်ခါ run တာပါ။ ရလဒ်ကို ပြန်သုံးဖို့ လိုရင် vector တစ်ခုထဲ သိမ်းပါ။
Mini challenge
Mini challenge: word toolkit
Word list တစ်ခုကို ကိုယ်တိုင်ရေးတဲ့ index loop မသုံးဘဲ algorithm၊ lambda နဲ့ view တွေနဲ့ ခွဲခြမ်းစိတ်ဖြာပါ။ minLength ထက် မတိုသော word အရေအတွက်ကို ရေတွက်၊ copy တစ်ခုကို အရှည်အလိုက် (တူရင် alphabetical) စီ၊ ရှည်တဲ့ word ပထမ သုံးလုံးကို uppercase နဲ့ print ထုတ်ပြီး 'b' နဲ့ စတဲ့ ပထမ word ကို ရှာပါ။
- minLength ကို capture လုပ်တဲ့ isLong lambda ကို count_if နဲ့ filter view နှစ်ခုလုံးမှာ သုံးရမယ်
- မူရင်း vector ကို မပြင်ဘဲ copy ကို std::ranges::sort နဲ့ lambda comparator နဲ့ စီရမယ်
- filter၊ transform၊ take(3) ပါတဲ့ view pipeline တစ်ခု သုံးရမယ်
- std::ranges::find_if ရဲ့ ရလဒ်ကို end() နဲ့ စစ်ပြီးမှ dereference လုပ်ရမယ်
#include <algorithm>
#include <cctype>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
std::string toUpper(std::string word) {
// TODO: uppercase every character (std::ranges::transform + std::toupper)
return word;
}
int main() {
std::vector<std::string> words{"pear", "fig", "banana", "kiwi", "apricot", "plum"};
std::size_t minLength = 4;
// TODO: isLong lambda capturing minLength; print how many words are long
// TODO: sort a copy by length, then alphabetically; print it
// TODO: filter | transform(toUpper) | take(3); print the result
// TODO: find_if the first word starting with 'b'; check before printing
}ရလဒ်: long words: 5၊ ပြီးရင် fig kiwi pear plum banana apricot၊ PEAR BANANA KIWI၊ first b-word: banana နဲ့ original first: pear (မူလ vector မပြောင်းကြောင်း ပြတာ) ကို print ထုတ်ပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
Comparator lambda မှာ a.size() != b.size() ဆိုရင် a.size() < b.size() ကို ပြန်ပေးပြီး မဟုတ်ရင် a < b ကို ပြန်ပေးပါ၊ နှစ်ခုလုံး strict ဖြစ်ပါတယ်။ std::toupper ကို unsigned char နဲ့ ခေါ်ပြီး ရလဒ်ကို char ဆီ static_cast လုပ်ပါ။ w.starts_with('b') က C++20 မှာ ရှိပါတယ်။
အဖြေ ကြည့်ရန်
#include <algorithm>
#include <cctype>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
std::string toUpper(std::string word) {
std::ranges::transform(word, word.begin(), [](unsigned char c) {
return static_cast<char>(std::toupper(c));
});
return word;
}
int main() {
std::vector<std::string> words{"pear", "fig", "banana", "kiwi", "apricot", "plum"};
std::size_t minLength = 4;
auto isLong = [minLength](const std::string& w) { return w.size() >= minLength; };
std::cout << "long words: " << std::ranges::count_if(words, isLong) << '\n';
std::vector<std::string> sorted = words; // sort a copy, keep the original
std::ranges::sort(sorted, [](const std::string& a, const std::string& b) {
if (a.size() != b.size()) return a.size() < b.size();
return a < b; // same length: alphabetical
});
for (const std::string& w : sorted) std::cout << w << ' ';
std::cout << '\n';
for (const std::string& w : words | std::views::filter(isLong)
| std::views::transform(toUpper)
| std::views::take(3)) {
std::cout << w << ' ';
}
std::cout << '\n';
auto it = std::ranges::find_if(words, [](const std::string& w) {
return w.starts_with('b');
});
if (it != words.end()) std::cout << "first b-word: " << *it << '\n';
std::cout << "original first: " << words.front() << '\n';
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- std::ranges algorithm တွေက container ကို တိုက်ရိုက် ယူပြီး projection နဲ့ member တစ်ခုအလိုက် sort၊ ရှာ၊ ရေတွက်နိုင်ပါတယ်။
- Lambda ရဲ့ [x] က ဖန်တီးချိန်က copy ပါ၊ [&x] က မူလ variable ပါ၊ lambda က scope ထက် ကြာကြာ ရှင်မယ်ဆိုရင် value နဲ့ capture လုပ်ပါ။
- View တွေက lazy ဖြစ်ပြီး data ကို မပိုင်ပါဘူး၊ loop ပတ်မှ တွက်ပြီး အောက်က container က view ထက် ကြာကြာ ရှင်ရပါမယ်။
- Comparator က strict weak ordering ဖြစ်ရပါမယ်: < ဒါမှမဟုတ် > ကို သုံးပြီး <= ကို ဘယ်တော့မှ မသုံးပါနဲ့။
The Current ISO C++ Standard (C++23) — Standard C++ Foundation