Thuta Learning
C++
IntermediateProgrammingအခြေခံ

Arrays

C++သင်ခန်းစာ 1325 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • std::array တစ်ခု ကြေညာပြီး index နဲ့ element တွေကို ဖတ်/ရေး လုပ်ကာ range-based for နဲ့ loop ပတ်နိုင်ဖို့
  • နောက်ဆုံး valid index က ဘာကြောင့် size() - 1 ဖြစ်လဲ၊ အဲဒီထက် ကျော်ရင် [] နဲ့ .at() မှာ ဘာဖြစ်လဲ ရှင်းပြနိုင်ဖို့
  • အလုပ်တစ်ခုအတွက် C array၊ std::array နဲ့ std::vector ထဲက ဘယ်ဟာ သင့်တော်လဲ ရွေးနိုင်ဖို့
  • std::span<const T> parameter ယူတဲ့ function ရေးပြီး array ရော vector ပါ လက်ခံနိုင်အောင် လုပ်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Array ဆိုတာ memory ထဲမှာ ဘေးချင်းကပ်ရက် စီထားတဲ့ နံပါတ်တပ် သေတ္တာတန်းလေး တစ်ခုပါ၊ သေတ္တာတိုင်းမှာ type တူတဲ့ value တွေကိုပဲ ထည့်ပါတယ်။ အခန်း ခုနစ်ခန်းပါတဲ့ ဆေးလုံးထည့်ဘူးလေးကို မြင်ကြည့်ပါ — အခန်းအရေအတွက်ကို ဘူးလုပ်တုန်းကတည်းက သတ်မှတ်ထားပြီး အခန်းတိုင်းမှာ တနင်္လာကနေ တနင်္ဂနွေအထိ တံဆိပ်ကပ်ထားပါတယ်။ C++ မှာတော့ အဲဒီ တံဆိပ်တွေက 0 ကနေ စတဲ့ နံပါတ်တွေ ဖြစ်လို့ သေတ္တာ သုံးခုပါတဲ့ array မှာ 0, 1, 2 ဆိုတဲ့ သေတ္တာတွေ ရှိပါတယ်။

Modern C++ မှာ ဒီအလုပ်အတွက် tool သုံးမျိုး ရှိပါတယ်။ std::array<T, N> က သေတ္တာ N ခု ပုံသေပါတဲ့ တန်းပါ။ std::vector<T> ကတော့ program run နေရင်း ရှည်လာနိုင်တဲ့ တန်းပါ။ int marks[5] လို ရေးတဲ့ C-style array အဟောင်းလည်း ရှိနေဆဲပါ၊ ဒါပေမယ့် သူ့ size ကို သူ အလွယ်တကူ မေ့သွားတတ်လို့ code အသစ်တွေမှာ ပထမ နှစ်မျိုးကို ပိုသုံးကြပါတယ်။

Problem → Why → Solution

ပြဿနာ: အဆုံးကို ကျော်ဖတ်မိတဲ့ array နဲ့ size မေ့သွားတဲ့ array

အောက်မှာ အမှတ် ငါးခုပါတဲ့ C-style array တစ်ခုနဲ့ helper function နှစ်ခု ရှိပါတယ်။ printAll() က i <= 5 နဲ့ loop ပတ်လို့ တစ်ဆင့် ပိုသွားပြီး နောက်ဆုံး အကြိမ်မှာ မရှိတဲ့ သေတ္တာ marks[5] ကို ဖတ်ပါတယ်။ countMarks() ကတော့ element အရေအတွက်ကို sizeof နည်းလမ်းနဲ့ ရေတွက်ဖို့ ကြိုးစားပေမယ့် ပုံမှန် 64-bit build မှာ 5 အစား 2 ကို print ထုတ်ပါတယ်။ Bug နှစ်ခုလုံးက build ကို မရပ်တန့်ပါဘူး — ဒုတိယ bug အတွက် compiler က warning ပဲ ပေးပါတယ်။

marks.cpp
#include <iostream>

void printAll(int marks[]) {
    for (int i = 0; i <= 5; i++) {          // one step too far
        std::cout << marks[i] << '\n';      // marks[5] does not exist
    }
}

int countMarks(int marks[]) {
    return sizeof(marks) / sizeof(marks[0]); // size of a pointer!
}

int main() {
    int marks[5] = {70, 82, 64, 91, 58};
    printAll(marks);
    std::cout << "count: " << countMarks(marks) << '\n';  // 2, not 5
}

C++ က ဘာကြောင့် ဒါကို ခွင့်ပြုနေတာလဲ

C array ပေါ်က [] operator က bounds check လုံးဝ မလုပ်ပါဘူး — အဆုံးကို ကျော်ဖတ်တာက undefined behaviour ဖြစ်လို့ program က အမှိုက် value print ထုတ်တာ၊ crash ဖြစ်တာ ဒါမှမဟုတ် တစ်နေ့ မှားမယ့်နေ့အထိ ကောင်းနေသလို ထင်ရတာ ဖြစ်နိုင်ပါတယ်။ ပြီးတော့ C array ကို function ဆီ ပို့လိုက်ရင် ပထမ element ကို ညွှန်တဲ့ pointer အဖြစ် decay ဖြစ်သွားလို့ length က ပျောက်သွားပါတယ် — GCC က 'sizeof' on array function parameter 'marks' will return size of 'int*' လို့ warning ပေးတာ ဒါကြောင့်ပါ။ std::array က size ကို type ထဲမှာ သိမ်းထားပြီး std::vector က runtime မှာ သိမ်းထားပါတယ်၊ .at() ကတော့ index တိုင်းကို စစ်ပေးပါတယ်။

Core concept: memory ထဲက တန်းတစ်တန်း

Array ဆိုတာ type တူတဲ့ element N ခုကို ထည့်ထားတဲ့ ဆက်တိုက် (contiguous) memory block တစ်ခုပါ။ Element တိုင်း size တူလို့ element i ရဲ့ address က block အစ ပေါင်း i ကို အဲဒီ size နဲ့ မြှောက်ထားတာပဲ ဖြစ်ပါတယ်။ Array ဘယ်လောက်ရှည်ရှည် index နဲ့ ချက်ချင်း ရောက်နိုင်တာ ဒါကြောင့်ပါ၊ index တွေ 0 ကနေ စတာလည်း ဒါကြောင့်ပါ — element 0 က offset သုည နေရာမှာ ရှိလို့ပါ။ နောက်ဆုံး valid index က အမြဲ size() - 1 ပါ။

text
std::array<int, 4> marks{70, 82, 64, 91};
-----------------------------------------
 index:    [0]    [1]    [2]    [3]
         +------+------+------+------+
 marks   |  70  |  82  |  64  |  91  |   one block, no gaps
         +------+------+------+------+
 offset:   0      4      8      12       bytes (int is 4 bytes here)

 marks[4] would be the box after the last one: it does not exist.

std::array<T, N> က အဲဒီ block ကို type အပြည့်အစုံ တစ်ခုအဖြစ် ထုပ်ပေးပါတယ်။ N က type ရဲ့ အစိတ်အပိုင်း ဖြစ်လို့ std::array<int, 4> နဲ့ std::array<int, 5> က type မတူကြပါဘူး၊ size လည်း ဘယ်တော့မှ မပျောက်ပါဘူး — function ထဲ ရောက်နေရင်တောင် marks.size() ကို ခေါ်လို့ ရပါတယ်။ Copy ကူးလို့ရ၊ == နဲ့ နှိုင်းယှဉ်လို့ရပြီး pointer အဖြစ် decay မဖြစ်ပါဘူး။ ပုံမှန်အားဖြင့် C array နဲ့ size ရော speed ပါ တူတူပဲ ဖြစ်ပါတယ်။

Element တစ်ခုကို ဖတ်ဖို့ ရေးနည်း နှစ်မျိုး ရှိပါတယ်။ marks[i] က ကိုယ့်ကို လုံးဝ ယုံပါတယ် — i က range ပြင်ပ ရောက်နေရင် behaviour က undefined ဖြစ်ပြီး ဘယ်သူကမှ သတိမပေးပါဘူး။ marks.at(i) ကတော့ index ကို အရင်စစ်ပြီး မှားနေရင် std::out_of_range ကို throw လုပ်လို့ catch လုပ်ပြီး ကိုင်တွယ်နိုင်ပါတယ်။ User input ဒါမှမဟုတ် file လို program အပြင်ကနေ လာတဲ့ index ဆိုရင် .at() ကို သုံးပြီး bound က မှန်ကြောင်း အာမခံထားတဲ့ loop ထဲမှာတော့ [] ကို သုံးပါ။

Element အရေအတွက်ကို program run မှ သိရမယ်ဆိုရင် std::vector<T> ကို သုံးပါ — layout က contiguous အတိုင်းပဲ ဖြစ်ပေမယ့် push_back() နဲ့ ရှည်လာနိုင်ပါတယ်။ Container နှစ်မျိုးလုံးက for (const auto& x : marks) ဆိုတဲ့ range-based for နဲ့လည်း အလုပ်လုပ်ပါတယ်၊ အဲဒီ loop က element တစ်ခုကို တစ်ကြိမ်တည်း ဝင်ကြည့်ပြီး အဆုံးကို ကျော်မသွားနိုင်ပါဘူး။

ToolSizeဘယ်အချိန် သုံးမလဲ
C array (int a[5])ပုံသေ၊ function ဆီ ပို့ရင် ပျောက်C API အဟောင်းတွေနဲ့ ချိတ်ရတဲ့အခါ
std::array<int, 5>ပုံသေ၊ type ထဲမှာ ပါCode ရေးချိန်မှာ အရေအတွက် သိပြီးသား
std::vector<int>Runtime မှာ ကြီးလာနိုင်အရေအတွက်က input ပေါ် မူတည်
Mental model: တန်းတစ်တန်းတည်းက နံပါတ်တပ် သေတ္တာများ
  1. Type တူ သေတ္တာ N ခု ဘေးချင်းကပ်
  2. Index i = အစ + i * element size
  3. Valid index က 0 ကနေ size() - 1 အထိ
  4. [] က ယုံတယ်၊ .at() က စစ်ပြီး throw

Index မှားနိုင်ခြေ ရှိရင် .at() ကို စစ်ခိုင်းပါ၊ loop က မှန်ကြောင်း အာမခံထားရင်တော့ [] သုံးလို့ ရပါတယ်။

အထွေထွေ ပုံစံ

cpp
std::array<Type, N> name{value0, value1, value2};  // fixed size N
std::vector<Type> list{value0, value1};            // can grow

name[index] = newValue;     // no bounds check
name.at(index);             // throws std::out_of_range if index >= size()
name.size();                // number of elements
list.push_back(value);      // vector only: add at the end

for (const Type& item : name) {
    // read each element once
}

N က compile time မှာ သိတဲ့ constant ဖြစ်ရပြီး value တိုင်းရဲ့ type က Type ဖြစ်ရပါမယ်။ [] ရော .at() ပါ element ရဲ့ reference ကို ပြန်ပေးလို့ နှစ်ခုလုံးနဲ့ ဖတ်လို့ရော assign လုပ်လို့ပါ ရပါတယ်။ std::array က size မပြောင်းနိုင်လို့ push_back() က vector မှာပဲ ရှိပါတယ်။

Example: std::array ထဲက course စာရင်း

ဒီ program က course နာမည် သုံးခုကို သိမ်းပြီး ပထမ course ကို ဖတ်၊ တတိယ course ကို အစားထိုးပြီး အားလုံးကို print ထုတ်ပါတယ်။ နောက်ဆုံးမှာ .at(3) နဲ့ စတုတ္ထ course ကို တောင်းကြည့်ပြီး index က range ပြင်ပ ရောက်နေတဲ့အခါ checked access က ဘာလုပ်လဲ ပြထားပါတယ်။

courses.cpp
#include <array>
#include <iostream>
#include <stdexcept>
#include <string>

int main() {
    std::array<std::string, 3> courses{"HTML", "CSS", "C++"};

    std::cout << "First: " << courses[0] << '\n';
    courses[2] = "JavaScript";

    for (const std::string& course : courses) {
        std::cout << "- " << course << '\n';
    }
    std::cout << "Count: " << courses.size() << '\n';

    try {
        std::cout << courses.at(3) << '\n';
    } catch (const std::out_of_range&) {
        std::cout << "courses.at(3) is out of range\n";
    }
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 7std::array<std::string, 3> courses{"HTML", "CSS", "C++"};

    Type က std::string element သုံးခုလို့ ပြောပါတယ်။ Brace ထဲက value တွေက index 0, 1, 2 ကို အစဉ်လိုက် ဖြည့်ပါတယ်၊ ချန်ထားခဲ့တဲ့ element ရှိရင် default value (string အလွတ်) ဖြစ်သွားပါမယ်။

  2. Line 10courses[2] = "JavaScript";

    [] က element ရဲ့ reference ကို ပြန်ပေးလို့ assign လုပ်လိုက်တာနဲ့ "C++" ကို နေရာမှာတင် အစားထိုးပါတယ်။ Index 2 က တတိယ၊ နောက်ဆုံး valid သေတ္တာပါ။

  3. Line 12for (const std::string& course : courses) {

    Range-based for က element တိုင်းကို တစ်ကြိမ်စီ ဝင်ကြည့်ပါတယ်။ const std::string& ကြောင့် နာမည်တစ်ခုချင်းကို copy မကူးဘဲ ဖတ်ပါတယ်။

  4. Line 15courses.size()

    size() က type ကိုယ်တိုင်ကနေ လာတာဖြစ်လို့ ဒီမှာ အမြဲ 3 ပါ — array ကို function ဆီ ပို့လိုက်ရင်တောင် မှန်နေဆဲပါ။

  5. Lines 17-21courses.at(3)

    Index 3 က အဆုံးထက် တစ်နေရာ ကျော်နေပါတယ်။ .at() က သတိထားမိပြီး std::out_of_range ကို throw လုပ်လို့ array နဲ့ မဆိုင်တဲ့ memory ကို ဖတ်မယ့်အစား catch block က message print ထုတ်ပါတယ်။

You should see
First: HTML
- HTML
- CSS
- JavaScript
Count: 3
courses.at(3) is out of range

std::vector: အရေအတွက်ကို ကြိုမသိတဲ့အခါ

Sign-up စာရင်းကို std::array နဲ့ မလုပ်နိုင်ပါဘူး၊ လူဘယ်နှစ်ယောက် ဝင်လာမလဲ မသိလို့ပါ။ std::vector<T> က array လိုပဲ element တွေကို contiguous block တစ်ခုထဲ သိမ်းပေမယ့် အဲဒီ block ကို heap ပေါ်မှာ ကိုယ်တိုင် ပိုင်ပြီး push_back() လုပ်ရင်း နေရာ မလောက်တော့ရင် ပိုကြီးတဲ့ block ဆီ ပြောင်းပါတယ်။ size()၊ []၊ .at() နဲ့ range-for တွေကို အရင်အတိုင်း သုံးနိုင်ပြီး နောက်ဆုံး element အတွက် back() ပါ ထပ်ရပါတယ်။

signups.cpp
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> signups;
    signups.push_back("Aye");
    signups.push_back("Bo");
    signups.push_back("Chit");

    std::cout << "Signed up: " << signups.size() << '\n';
    std::cout << "Last: " << signups.back() << '\n';
}

ကြီးလာရင် element တွေ နေရာရွှေ့နိုင်ပါတယ်

Vector ကြီးလာတဲ့အခါ element အားလုံးကို block အသစ်ဆီ ရွှေ့နိုင်ပါတယ်။ push_back() မလုပ်ခင်က ယူထားတဲ့ element pointer ဒါမှမဟုတ် reference တွေက free လုပ်ပြီးသား memory ကို ညွှန်နေနိုင်လို့ element အသစ် ထည့်ပြီးရင် index နဲ့ ပြန်ဖတ်ပါ။

std::span: array အမျိုးအစားတိုင်းအတွက် function တစ်ခုတည်း

C++20 မတိုင်ခင်က အမှတ်တွေ ပေါင်းတဲ့ function တစ်ခုဟာ container type တစ်မျိုးကိုပဲ ရွေးရတာ ဒါမှမဟုတ် pointer နဲ့ length ကို သီးခြားစီ ယူရတာ ဖြစ်ပါတယ်။ <span> ထဲက std::span<const T> က ပေါ့ပါးတဲ့ view တစ်ခုပါ — pointer တစ်ခုနဲ့ size တစ်ခုကို တွဲထုပ်ထားတာပါ။ ဘာကိုမှ မပိုင်၊ copy မကူးဘဲ std::array၊ std::vector နဲ့ C array တွေကနေ အလိုအလျောက် ပြောင်းပေးလို့ function တစ်ခုတည်းက အားလုံးကို လက်ခံနိုင်ပါတယ်။ first(n) နဲ့ last(n) က element တွေ အတူတူကိုပဲ ပိုသေးတဲ့ view အဖြစ် ပေးပါတယ်။

average.cpp
#include <array>
#include <format>
#include <iostream>
#include <span>
#include <vector>

double average(std::span<const int> marks) {
    if (marks.empty()) return 0.0;
    int sum = 0;
    for (int mark : marks) sum += mark;
    return static_cast<double>(sum) / marks.size();
}

int main() {
    std::array<int, 3> quiz{70, 80, 90};
    std::vector<int> exams{55, 65};
    int legacy[4] = {100, 90, 80, 70};

    std::cout << std::format("{:.1f}\n", average(quiz));                    // 80.0
    std::cout << std::format("{:.1f}\n", average(exams));                   // 60.0
    std::cout << std::format("{:.1f}\n", average(legacy));                  // 85.0
    std::cout << std::format("{:.1f}\n", average(std::span(legacy).first(2))); // 95.0
}

Span က view တစ်ခုသာ ဖြစ်လို့ သူ ကြည့်နေတဲ့ container အသက်ရှင်နေပြီး reallocate မဖြစ်သေးသရွေ့ပဲ valid ဖြစ်ပါတယ်။ Function parameter အဖြစ် ချက်ချင်း သုံးပြီး ပြီးသွားတာက ဘေးအကင်းဆုံး အသုံးပြုပုံပါ။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Fixed-size array တွေက အရေအတွက်ဟာ ပြဿနာရဲ့ အစိတ်အပိုင်း ဖြစ်နေတဲ့ နေရာတိုင်းမှာ ပေါ်လာပါတယ် — တစ်ပတ်ရဲ့ ၇ ရက်၊ sales report ရဲ့ ၁၂ လ၊ pixel တစ်ခုရဲ့ color channel ၃ ခု၊ chess board ရဲ့ အကွက် ၆၄ ကွက်။ Game နဲ့ graphics code တွေမှာ position အတွက် std::array<float, 3> လို vector နဲ့ matrix အသေးလေးတွေကို std::array နဲ့ ရေးကြပါတယ်၊ size က သိပြီးသား ဖြစ်ပြီး data က CPU မြန်မြန် ဖတ်နိုင်တဲ့ ကျစ်လျစ်တဲ့ block တစ်ခုထဲမှာ ရှိနေလို့ပါ။

File ကနေ load လုပ်တဲ့ row တွေ၊ shopping cart ထဲက item တွေ၊ server ဆီက message တွေလို တခြား စာရင်း အများစုကတော့ C++ code ရဲ့ default container ဖြစ်တဲ့ std::vector ပါ။ Library function တွေက caller ဆီမှာ ရှိတဲ့ container ကို ပို့နိုင်အောင် std::span ကို ပိုပိုပြီး လက်ခံလာကြပါတယ်။ Safety ကို အလေးထားတဲ့ team တွေရဲ့ code review မှာ user input ကနေ လာတဲ့ index နေရာတိုင်းမှာ .at() ဒါမှမဟုတ် range check ကို သေချာ ရှာကြည့်ကြပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. <= size နဲ့ loop ပတ်တာ

Element သုံးခုပါတဲ့ array ပေါ်မှာ for (int i = 0; i <= 3; i++) က လေးကြိမ် run ပြီး အဆုံးကျော်နေတဲ့ courses[3] ကို ဖတ်ပါတယ်။ ဒါက exception မဟုတ်ဘဲ undefined behaviour ဖြစ်လို့ အလုပ်လုပ်နေသလို တောင် ထင်ရနိုင်ပါတယ်။ i < courses.size() နဲ့ loop ပတ်ပါ၊ ဒါမှမဟုတ် အဆုံးကို ကျော်လို့ မရတဲ့ range-based for ကို ပိုသုံးပါ။

2. [] က throw လုပ်မယ်လို့ ထင်တာ

courses[i] ကို try/catch နဲ့ ပတ်ထားလည်း ဘာမှ catch မမိပါဘူး — index ဘယ်လောက်ပဲ ရူးရူး [] က ဘယ်တော့မှ throw မလုပ်ပါဘူး။ Index မှားတာကို std::out_of_range နဲ့ သတင်းပို့တာ .at() တစ်ခုတည်းပါ။ စစ်ဆေးမှု လိုရင် .at() ကို ခေါ်ရပါမယ် (ဒါမှမဟုတ် index ကို size() နဲ့ ကိုယ်တိုင် နှိုင်းယှဉ်ရပါမယ်)။

3. Parameter ရဲ့ element အရေအတွက်ကို sizeof နဲ့ ရေတွက်တာ

void f(int marks[]) ထဲမှာ marks က တကယ်တော့ int* ပါ၊ ဒါကြောင့် sizeof(marks) / sizeof(marks[0]) က length အစစ် ဘာပဲဖြစ်ဖြစ် ပုံမှန် 64-bit build မှာ 2 ကို ပေးပါတယ်။ အဲဒီအစား std::span<const int> ဒါမှမဟုတ် const std::array<int, N>& ကို ယူပြီး .size() ကို မေးပါ။

4. Checked access မလုပ်ခင် print စထုတ်တာ

std::cout << "Day " << d << ": " << temps.at(d); မှာ temps.at(d) က throw မလုပ်ခင် "Day 7: " ကို print ထုတ်ပြီးသား ဖြစ်နေလို့ screen ပေါ်မှာ စာကြောင်း တစ်ဝက်ပဲ ကျန်ခဲ့ပါတယ်။ Element ကို variable ထဲ အရင် ဖတ်ထားပြီးမှ print ထုတ်ပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Default အနေနဲ့ std::vector ကို သုံးပါ၊ အရေအတွက်က ပြဿနာရဲ့ ပုံသေ အချက်ဖြစ်မှ std::array ကို ရွေးပါ။
  • အဆုံးကို ကျော်လို့ မရတဲ့ range-based for loop ကို ဦးစားပေးပါ။
  • Input ကနေ လာတဲ့ index တွေအတွက် .at() ကို၊ bound ကို ကိုယ်တိုင် ထိန်းထားတဲ့ loop တွေထဲမှာ [] ကို သုံးပါ။
  • Sequence ကို ဖတ်ရုံပဲ လုပ်တဲ့ function တွေမှာ std::span<const T> ကို parameter အဖြစ် ယူပါ။

Mini challenge

Mini challenge: တစ်ပတ်စာ အပူချိန်

တစ်ပတ်စာ နေ့စဉ် အပူချိန်တွေနဲ့ နေ့နာမည်တွေကို std::array နှစ်ခုထဲ သိမ်းပြီး အပူဆုံးနေ့၊ ပျမ်းမျှ အပူချိန်တွေနဲ့ စစ်ဆေးထားတဲ့ lookup တစ်ခုကို report ထုတ်ပါ။

  • Value အကြီးဆုံးရဲ့ index ကို ပြန်ပေးတဲ့ int hottestIndex(std::span<const int> temps) ကို ရေးပါ
  • Span အလွတ်ဆိုရင် 0.0 ပြန်ပေးတဲ့ double average(std::span<const int> temps) ကို ရေးပါ
  • တစ်ပတ်လုံး ပျမ်းမျှနဲ့ weekend (နောက်ဆုံး နှစ်ရက်) ပျမ်းမျှကို ဒသမ တစ်နေရာနဲ့ print ထုတ်ပါ
  • Day 2 နဲ့ day 7 ကို .at() နဲ့ ရှာပြီး index မှားရင် "no such day" လို့ print ထုတ်ပါ
temperatures.cpp
#include <array>
#include <format>
#include <iostream>
#include <span>
#include <stdexcept>
#include <string>

int hottestIndex(std::span<const int> temps) {
    // TODO: remember the index of the largest value seen so far
}

double average(std::span<const int> temps) {
    // TODO: 0.0 for an empty span, otherwise sum / size
}

int main() {
    // TODO: a std::array of 7 day names (Mon..Sun) and one of 7 temperatures:
    //       31, 33, 36, 34, 30, 29, 32
    // TODO: print the hottest day, the week average and the weekend average
    // TODO: look up day 2 and day 7 with .at() inside try/catch
}

ရလဒ်: Program က ဒီ စာကြောင်း ငါးကြောင်းကို print ထုတ်ရပါမယ်: Hottest: Wed (36 C) Week average: 32.1 C Weekend average: 30.5 C Day 2: 36 C Day 7: no such day

အရိပ်အမြွက် ကြည့်ရန်

best ကို 0 ကနေ စပြီး index 1 ကနေ နှိုင်းယှဉ်ပါ။ std::span(temps).last(2) က နောက်ဆုံး element နှစ်ခုရဲ့ view ကို ပေးပါတယ်။ Index မှားရင် စာကြောင်း တစ်ဝက် မကျန်ခဲ့အောင် temps.at(day) ကို print မထုတ်ခင် variable ထဲ အရင် ဖတ်ထားပါ။

အဖြေ ကြည့်ရန်
temperatures.cpp
#include <array>
#include <format>
#include <iostream>
#include <span>
#include <stdexcept>
#include <string>

int hottestIndex(std::span<const int> temps) {
    int best = 0;
    for (int i = 1; i < static_cast<int>(temps.size()); ++i) {
        if (temps[i] > temps[best]) best = i;
    }
    return best;
}

double average(std::span<const int> temps) {
    if (temps.empty()) return 0.0;
    int sum = 0;
    for (int t : temps) sum += t;
    return static_cast<double>(sum) / temps.size();
}

int main() {
    const std::array<std::string, 7> days{"Mon", "Tue", "Wed", "Thu",
                                          "Fri", "Sat", "Sun"};
    const std::array<int, 7> temps{31, 33, 36, 34, 30, 29, 32};

    int hot = hottestIndex(temps);
    std::cout << "Hottest: " << days[hot] << " (" << temps[hot] << " C)\n";
    std::cout << std::format("Week average: {:.1f} C\n", average(temps));
    std::cout << std::format("Weekend average: {:.1f} C\n",
                             average(std::span(temps).last(2)));

    for (int day : {2, 7}) {
        try {
            int value = temps.at(day);
            std::cout << "Day " << day << ": " << value << " C\n";
        } catch (const std::out_of_range&) {
            std::cout << "Day " << day << ": no such day\n";
        }
    }
}

Quick Quiz 1 / 3

std::array<int, 4> scores{}; ရှိတယ် ဆိုပါစို့။ နောက်ဆုံး element ကို ဘယ် expression က ဖတ်လဲ။

Quick Quiz 2 / 3

std::array<int, 3> a{1, 2, 3}; try { a[5] = 9; std::cout << "ok"; } catch (const std::out_of_range&) { std::cout << "caught"; } က ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

std::array<int, 7>၊ std::vector<int> နဲ့ int[10] သုံးမျိုးလုံးကို copy မကူးဘဲ ဖတ်နိုင်တဲ့ function parameter က ဘယ်ဟာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Array ဆိုတာ type တူ element N ခုပါတဲ့ contiguous block တစ်ခုဖြစ်ပြီး valid index က 0 ကနေ size() - 1 အထိပါ။
  • std::array<T, N> က သူ့ size ကို သိပြီး pointer အဖြစ် decay မဖြစ်ပါဘူး၊ std::vector<T> ကတော့ runtime မှာ ကြီးလာနိုင်ပါတယ်။
  • [] က ဘာမှ မစစ်ပါဘူး (range ပြင်ပဆို undefined behaviour)၊ .at() က std::out_of_range ကို throw လုပ်ပါတယ်။
  • std::span<const T> က function တစ်ခုတည်းကို contiguous sequence မှန်သမျှ copy မကူးဘဲ ဖတ်နိုင်စေပါတယ်။

The Current ISO C++ Standard (C++23) — Standard C++ Foundation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Arrays | Thuta Learning