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

STL: Vector

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

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

  • Vector တစ်ခု ဖန်တီး၊ push_back နဲ့ emplace_back နဲ့ element ထည့်ပြီး const& range-for နဲ့ ဖတ်နိုင်ဖို့
  • [] နဲ့ at() ကြား ရွေးချယ်ပြီး index မှားတဲ့အခါ ဘာဖြစ်လဲ ရှင်းပြနိုင်ဖို့
  • size နဲ့ capacity ကွာခြားချက်ကို ရှင်းပြပြီး နောက်ဆုံး အရွယ်အစား သိတဲ့အခါ reserve() ကို သုံးနိုင်ဖို့
  • ဘယ် operation တွေက iterator၊ pointer၊ reference တွေကို invalid ဖြစ်စေလဲ သိပြီး std::erase / std::erase_if နဲ့ လုံခြုံစွာ ဖယ်ရှားနိုင်ဖို့

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

Vector ကို ရိုးရိုးလေး ပြောရရင်

ရိုးရိုး array ရဲ့ အရွယ်အစားကို code ရေးတဲ့အချိန်မှာ သတ်မှတ်ပြီးသား ဖြစ်ပါတယ်။ လက်တွေ့ list တွေက အဲလို မဟုတ်ပါဘူး: to-do app က user ဘယ်နှခု ထည့်မလဲ မသိသလို file reader ကလည်း line ဘယ်နှကြောင်း လာမလဲ မသိပါဘူး။ std::vector က item ထည့်သလောက် ကြီးထွားလာပြီး element တွေကို memory ထဲမှာ ဘေးချင်းကပ် ဆက်တိုက် ထားပေးတဲ့ list ပါ၊ ဒါကြောင့် item နံပါတ် 5 ကို ဖတ်တာက ရိုးရိုး array လိုပဲ မြန်ပါတယ်။

နံပါတ်တပ်ထားတဲ့ locker တန်းတစ်တန်းကို စဉ်းစားကြည့်ပါ။ Locker တွေ ပြည့်သွားရင် vector က ပိုရှည်တဲ့ locker တန်းအသစ် ငှားပြီး ပစ္စည်းအားလုံးကို ရွှေ့ကာ တန်းအဟောင်းကို ပြန်အပ်ပါတယ်။ ဒီ lesson ရဲ့ အချက်အများစုက အဲဒီ ရွှေ့ပြောင်းမှုကနေ လာတာပါ။

Problem → Why → Solution

ပြဿနာ: ရုတ်တရက် ပျောက်သွားတဲ့ task

လက်ရှိ လုပ်နေတဲ့ task ကို အမြန် ပြန်ရောက်ဖို့ ပထမ element ဆီ reference တစ်ခု သိမ်းထားတယ် ဆိုပါစို့။ ပြီးတော့ task အသစ် အများကြီး ထည့်ပြီး current ကို print ထုတ်ပါတယ်။ တစ်ခါတလေ မှန်ကန်တဲ့ စာ ပေါ်ပြီး တစ်ခါတလေ အမှိုက် character တွေ ပေါ်တယ်၊ တစ်ခါတလေ crash ဖြစ်ပါတယ်။ Code က compile ဖြစ်ပြီး warning တစ်ခုမှ မပြပါဘူး။

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

int main() {
    std::vector<std::string> tasks{"Write report"};
    std::string& current = tasks[0];            // reference into the vector

    for (int i = 0; i < 100; ++i) {
        tasks.push_back("Task " + std::to_string(i));   // may move every element
    }
    std::cout << "Working on: " << current << '\n';   // undefined behaviour
}

current က ဘာကြောင့် ပျက်သွားတာလဲ

Vector က element တွေကို ဆက်တိုက်ဖြစ်တဲ့ memory block တစ်ခုထဲမှာ ထားပါတယ်။ push_back က block ထဲမှာ ရှိတဲ့ နေရာထက် ပိုလိုလာရင် vector က ပိုကြီးတဲ့ block အသစ်ကို allocate လုပ်၊ element တိုင်းကို ရွှေ့ပြီး block အဟောင်းကို free လုပ်ပါတယ်။ current ကတော့ free လုပ်ပြီးသား block ထဲကို ညွှန်နေဆဲပါ။ Byte အဟောင်းတွေ ကျန်နေသေးရင် program က အဆင်ပြေသလို ထင်ရပေမဲ့ တကယ်တော့ undefined behaviour ဖြစ်ပြီး vector နဲ့ ပတ်သက်တဲ့ အဖြစ်အများဆုံး bug ပါ။

Core concept: size၊ capacity နဲ့ ဆက်တိုက်ရှိတဲ့ block တစ်ခု

std::vector<T> တစ်ခုက heap ပေါ်က block တစ်ခုကို ပိုင်ဆိုင်ပြီး အချက် သုံးခုကို မှတ်ထားပါတယ်: data ဘယ်မှာ စလဲ၊ element ဘယ်နှခု သုံးနေလဲ (size)၊ reallocate မလုပ်ခင် ဘယ်နှခု ဆံ့လဲ (capacity)။ Vector က value type ပါ: copy လုပ်ရင် element အားလုံး copy ကူးပြီး scope ပြီးရင် သူ့ memory ကို ကိုယ်တိုင် free လုပ်တဲ့အတွက် delete ရေးစရာ မလိုပါဘူး (RAII)။

push_back(value) က value ကို နောက်ဆုံးမှာ copy ဒါမှမဟုတ် move လုပ်ပြီး emplace_back(args...) ကတော့ element ကို အဲဒီနေရာမှာတင် တိုက်ရိုက် construct လုပ်ပါတယ်။ Block ပြည့်သွားတဲ့အခါ capacity ကို ဆတိုး တိုးပေးလို့ push_back အများစုက စျေးပေါပါတယ် (amortized constant time)။ ဘယ်နှဆ တိုးလဲဆိုတာ implementation-defined ဖြစ်ပြီး standard library တစ်ခုနဲ့ တစ်ခု မတူတဲ့အတွက် ဘယ် number ကိုမှ အားမကိုးပါနဲ့။ Element ဘယ်နှခု လာမလဲ သိရင် reserve(n) နဲ့ တစ်ခါတည်း allocate လုပ်ပါ။

Element တွေကို ဖတ်တဲ့အခါ v[i] က ဘာမှ မစစ်ပါဘူး၊ range ပြင်ပ index က undefined behaviour ပါ။ v.at(i) က စစ်ပြီး std::out_of_range ကို throw လုပ်ပါတယ်။ Vector အလွတ်ပေါ်မှာ front() နဲ့ back() ကလည်း undefined ပါ။ size() က unsigned ဖြစ်တဲ့ std::size_t ကို ပြန်ပေးတဲ့အတွက် vector အလွတ်မှာ v.size() - 1 က -1 မဖြစ်ဘဲ အလွန်ကြီးတဲ့ number တစ်ခု ဖြစ်သွားပါတယ်။ Function တွေဆီ ဖတ်ရုံသက်သက်ဆိုရင် const& နဲ့၊ ပြင်မယ်ဆိုရင် & နဲ့ ပေးပါ၊ by value ပေးရင် element အားလုံး copy ကူးပါတယ်။

Operationဘာတွေ invalid ဖြစ်လဲ
push_back / emplace_backReallocate ဖြစ်ရင် အားလုံး၊ မဖြစ်ရင် end() တစ်ခုတည်း
အလယ်မှာ insertထည့်တဲ့နေရာနဲ့ နောက်ကအားလုံး (reallocate ဖြစ်ရင် အားလုံး)
erase(it)ဖျက်တဲ့ element နဲ့ နောက်က အားလုံး
reserve(n) (n > capacity)၊ clear()အားလုံး
[]၊ at()၊ range-for နဲ့ ဖတ်ခြင်းဘာမှ မဖြစ်

Element တွေကို ဖယ်ရှားတဲ့အခါ erase က နောက်က element တွေကို ဘယ်ဘက် ရွှေ့ရလို့ element အရေအတွက်နဲ့ အချိုးကျ အချိန်ယူပါတယ်။ C++20 မှာ std::erase(v, value) နဲ့ std::erase_if(v, predicate) တို့က ကိုက်ညီတာ အားလုံးကို pass တစ်ခါတည်းနဲ့ ဖယ်ပြီး ဘယ်နှခု ဖယ်ခဲ့လဲ ပြန်ပေးပါတယ်။

Vector ကို ရွှေ့ပြောင်းနိုင်တဲ့ locker တန်းလို မြင်ကြည့်ပါ
  1. Element တွေ block တစ်ခုထဲ ဆက်တိုက်
  2. size: သုံးထားတာ၊ capacity: ဆံ့တာ
  3. ပြည့်ရင် block ကြီး allocate၊ အကုန်ရွှေ့
  4. Reference အဟောင်းတွေ ဘာမှ မညွှန်တော့

push_back တွေ ကြားမှာ element တစ်ခုကို မှတ်ထားဖို့ လိုရင် reference ဒါမှမဟုတ် iterator ကို မဟုတ်ဘဲ index ကို မှတ်ပါ။

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

cpp
std::vector<Type> items;               // empty
std::vector<Type> items{a, b, c};      // with initial elements
items.push_back(value);                // add to the end
items.reserve(n);                      // room for n, size unchanged
items[i];         items.at(i);         // unchecked / checked access
items.size();     items.empty();
for (const Type& item : items) { /* read */ }
std::erase_if(items, predicate);       // C++20: remove every match

Element type ကို angle bracket ထဲမှာ ရေးပြီး နောက်ဆုံးမှာ ထည့်၊ index နဲ့ ဖတ်၊ const& နဲ့ loop ပတ်ပါ။ ပထမဆုံး ထည့်ခင် element ဘယ်နှခု လာမလဲ သိရင် reserve ကို ခေါ်ပါ။

Worked example: task list တစ်ခု

ဒီ program က task သုံးခု ထည့်၊ အားလုံးကို print ထုတ်၊ တစ်ခုကို ပြင်ပြီး ပထမဆုံး task ကို ဖယ်ပါတယ်။ Loop variable ရဲ့ type ကို သတိထားကြည့်ပါ: const std::string& ဖြစ်လို့ ပတ်တိုင်း string ကို copy မကူးပါဘူး။ နောက်ဆုံး line နှစ်ကြောင်းက erase လုပ်ပြီးနောက် element တွေ ဘယ်ဘက် ရွှေ့သွားကြောင်း ပြပါတယ်။

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

int main() {
    std::vector<std::string> tasks;              // starts empty: size() == 0

    tasks.push_back("Learn variables");
    tasks.push_back("Practice loops");
    tasks.emplace_back("Build a mini project");  // constructs the string in place

    for (const std::string& task : tasks) {      // const& reads without copying
        std::cout << "- " << task << '\n';
    }

    tasks[1] = "Practice for and while loops";   // [] does no bounds check
    std::cout << "second: " << tasks.at(1) << '\n';   // at() checks the index

    tasks.erase(tasks.begin());                  // later elements shift left
    std::cout << "first now: " << tasks.front() << '\n';
    std::cout << "Total tasks: " << tasks.size() << '\n';
}

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

  1. Start emptystd::vector<std::string> tasks;

    Element မရှိဘဲ size() က 0 ဖြစ်တဲ့ vector ပါ။ Type က std::string လို့ ပြောထားလို့ int ကို ထည့်ဖို့ ကြိုးစားရင် compile error ဖြစ်ပါတယ်။

  2. Construct in placetasks.emplace_back("Build a mini project");

    emplace_back က argument ကို std::string ရဲ့ constructor ဆီ တိုက်ရိုက် ပေးပြီး vector ထဲမှာတင် string ကို တည်ဆောက်ပါတယ်။ ဒီလောက် ရိုးရှင်းတဲ့ type အတွက် push_back နဲ့ ရလဒ် တူတူပါပဲ။

  3. Read without copyingfor (const std::string& task : tasks) {

    const& က vector ထဲက element ကိုယ်တိုင်ကို ကြည့်ပြီး ပြင်ခွင့် မပေးပါဘူး။ std::string task လို့ ရေးရင် ပတ်တိုင်း copy ကူးပြီး task ကို ပြင်တာက vector ကို မပြောင်းပါဘူး။

  4. Unchecked write, checked readtasks[1] = "Practice for and while loops";

    [] နဲ့ ရေးတာက index 1 ရှိကြောင်း ကိုယ်တိုင် သေချာတဲ့အတွက်ပါ။ နောက် line က at(1) ကို သုံးပြီး index မရှိခဲ့ရင် undefined behaviour အစား std::out_of_range ကို throw လုပ်မှာပါ။

  5. Erase shifts the resttasks.erase(tasks.begin());

    begin() က ပထမ element ကို ညွှန်ပါတယ်။ ဖျက်ပြီးနောက် ကျန်တဲ့ element တွေ ဘယ်ဘက် တစ်နေရာ ရွှေ့သွားလို့ ပြင်ထားတဲ့ task က front() ဖြစ်လာပြီး size က 2 ဖြစ်ပါတယ်။

You should see
- Learn variables
- Practice loops
- Build a mini project
second: Practice for and while loops
first now: Practice for and while loops
Total tasks: 2

Capacity နဲ့ reserve ကို ကိုယ်တိုင် စမ်းကြည့်ခြင်း

push_back တစ်ခုက reallocate လုပ်ခဲ့လား သိဖို့ capacity() ပြောင်းသွားလား စစ်ကြည့်ရုံပါပဲ။ အောက်က program က vector နှစ်ခုထဲ number တစ်ထောင်စီ ထည့်ပါတယ်: တစ်ခုက ရိုးရိုး ကြီးထွားပြီး နောက်တစ်ခုက reserve(1000) နဲ့ စပါတယ်။ ဘယ်နှခါ reallocate လုပ်လဲဆိုတဲ့ အတိအကျ number က library ပေါ် မူတည်လို့ program က အဲဒါကို print မထုတ်ဘဲ ဖြစ်ခဲ့လား မဖြစ်ခဲ့လား ကိုပဲ ပြပါတယ်။

reserve_demo.cpp
#include <iostream>
#include <vector>

int countReallocations(std::vector<int>& v, int items) {
    int moves = 0;
    std::size_t lastCapacity = v.capacity();
    for (int i = 0; i < items; ++i) {
        v.push_back(i);
        if (v.capacity() != lastCapacity) {      // capacity changed = new block
            ++moves;
            lastCapacity = v.capacity();
        }
    }
    return moves;
}

int main() {
    std::vector<int> grown;
    std::vector<int> reserved;
    reserved.reserve(1000);                      // one allocation up front

    std::cout << std::boolalpha;
    std::cout << "grown reallocated: " << (countReallocations(grown, 1000) > 0) << '\n';
    std::cout << "reserved reallocated: " << countReallocations(reserved, 1000)
              << " times\n";
    std::cout << "capacity >= size: " << (grown.capacity() >= grown.size()) << '\n';
}

Output က grown reallocated: true၊ reserved reallocated: 0 times နဲ့ capacity >= size: true ပါ။ Standard က reserve(n) ပြီးနောက် size က n မကျော်မချင်း insert တွေက reallocate မလုပ်ရဘူးလို့ အာမခံထားလို့ ဒုတိယ line က ဘယ် compiler မှာမဆို 0 ပါ။ Capacity ရဲ့ တန်ဖိုးအတိအကျကတော့ ဘယ် library မှာမဆို size ထက် မနည်းဘူးဆိုတာပဲ သေချာပါတယ်။

လုံခြုံစွာ ဖယ်ရှားခြင်းနဲ့ စစ်ဆေးထားတဲ့ access

std::erase_if က vector နဲ့ "ဒီ element ကို ဖယ်မလား" လို့ ဖြေပေးတဲ့ function ကို ယူပါတယ်။ ဒီမှာ ရိုးရိုး function နာမည်ကို ပေးထားပြီး lesson 28 မှာ lambda နဲ့ အစားထိုးပုံကို တွေ့ရပါမယ်။ at() က index မှားရင် program ကို undefined behaviour ထဲ မပို့ဘဲ catch လုပ်လို့ရတဲ့ exception ကို throw လုပ်ကြောင်းလည်း ပြထားပါတယ်။

erase_demo.cpp
#include <iostream>
#include <stdexcept>
#include <vector>

bool isFailing(int score) { return score < 40; }

int main() {
    std::vector<int> scores{72, 35, 90, 18, 64};

    auto removed = std::erase_if(scores, isFailing);   // C++20, <vector>
    std::cout << "removed " << removed << ", left:";
    for (int s : scores) std::cout << ' ' << s;
    std::cout << '\n';

    try {
        int value = scores.at(10);               // index 10 does not exist
        std::cout << value << '\n';
    } catch (const std::out_of_range&) {
        std::cout << "at(10) threw std::out_of_range\n";
    }
}

ဒီ program က "removed 2, left: 72 90 64" နဲ့ "at(10) threw std::out_of_range" ကို print ထုတ်ပါတယ်။ ကျန်တဲ့ element တွေရဲ့ အစီအစဉ် မပြောင်းကြောင်း သတိပြုပါ။

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

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

C++ Core Guidelines က သီးခြား အကြောင်းပြချက် မရှိရင် std::vector ကို default container အဖြစ် သုံးဖို့ အကြံပြုပါတယ်။ Element တွေ ဘေးချင်းကပ် ရှိနေတာက CPU cache နဲ့ အလွန် ကိုက်ညီလို့ list ဒါမှမဟုတ် map ထက် အများအားဖြင့် ပိုမြန်ပါတယ်:

  • File ဒါမှမဟုတ် API response တစ်ခုက row တွေကို std::vector<Record> ထဲ load လုပ်ပြီး sort၊ filter၊ report လုပ်ပါတယ်။ Row အရေအတွက် header ထဲမှာ ပါရင် reserve နဲ့ စပါတယ်။
  • Game တွေက entity တွေကို vector ထဲ ထားပြီး frame တိုင်း loop ပတ်ပါတယ်။ Frame ကြားမှာ pointer တွေ မသိမ်းဘဲ index ဒါမှမဟုတ် id ကို သိမ်းတာက ဒီ lesson ရဲ့ invalidation စည်းမျဉ်းကြောင့်ပါ။
  • Network နဲ့ file buffer တွေက std::vector<char> ဒါမှမဟုတ် std::vector<std::byte> ကို သုံးပြီး data() က C API တွေဆီ ပေးလို့ရတဲ့ ဆက်တိုက် pointer ကို ပေးပါတယ်။

အမှားများ

Vector နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ

push_back ကြားမှာ reference ဒါမှမဟုတ် iterator ကို ကိုင်ထားခြင်း

Problem section ထဲက bug ပါ။ Reallocation က block အဟောင်းကို free လုပ်ပြီး အဲဒီထဲကို ညွှန်တဲ့ အရာအားလုံး dangling ဖြစ်သွားပါတယ်။ Index ကို သိမ်းပါ၊ ဒါမှမဟုတ် ပြင်ပြီးမှ reference ကို ပြန်ယူပါ၊ ဒါမှမဟုတ် element အရေအတွက် သိရင် အရင် reserve လုပ်ပါ။

Range-for loop ထဲမှာ erase လုပ်ခြင်း

Range-for က iterator တစ်ခုကို ဖုံးကွယ်ပြီး ကိုင်ထားပါတယ်။ Loop ထဲမှာ erase ခေါ်လိုက်ရင် အဲဒီ iterator invalid ဖြစ်ပြီး element တွေ ကျော်သွားတာ ဒါမှမဟုတ် crash ဖြစ်တာ ကြုံရပါတယ်။ std::erase_if ကို သုံးပါ၊ ဒါမှမဟုတ် erase ပြန်ပေးတဲ့ iterator နဲ့ ရိုးရိုး iterator loop ကို သုံးပါ။

Unsigned size() နဲ့ ပတ်သက်တဲ့ အမှားများ

for (std::size_t i = 0; i <= v.size(); ++i) က v[v.size()] ကို ဖတ်ပြီး undefined behaviour ဖြစ်ပါတယ်၊ < ကို သုံးပါ။ v.size() - 1 က vector အလွတ်မှာ 64-bit system ပေါ် 18446744073709551615 ဖြစ်သွားလို့ နုတ်တဲ့ arithmetic မလုပ်ခင် empty() ကို စစ်ပါ။

Vector ကို by value ပေးခြင်း

void print(std::vector<std::string> list) က ခေါ်တိုင်း string အားလုံးကို copy ကူးပြီး function ထဲက ပြင်ဆင်ချက်တွေကလည်း ခေါ်သူရဲ့ vector ကို မထိပါဘူး။ ဖတ်ဖို့ const& ကို၊ ပြင်ဖို့ & ကို သုံးပါ။

Mini challenge

Mini challenge: duplicate မပါတဲ့ playlist

Song title တွေကို std::vector<std::string> ထဲ သိမ်းတဲ့ playlist အတွက် function သုံးခု ရေးပါ။ addSong က title ရှိပြီးသားဆိုရင် မထည့်ဘဲ false ပြန်ပေးပြီး removeSong က title တစ်ခုရဲ့ copy အားလုံးကို ဖယ်ကာ ဘယ်နှခု ဖယ်ခဲ့လဲ ပြန်ပေးပါတယ်။ printPlaylist က နံပါတ်တပ်ပြီး print ထုတ်ပါတယ်။

  • addSong က vector ကို & နဲ့ ယူပြီး ရှာဖို့ std::find ကို သုံးရမယ်
  • removeSong က C++20 std::erase ကို သုံးပြီး ဖယ်ခဲ့တဲ့ အရေအတွက်ကို ပြန်ပေးရမယ်
  • printPlaylist က vector ကို const& နဲ့ ယူပြီး 1 ကနေ စတဲ့ နံပါတ်တွေနဲ့ print ထုတ်ရမယ်
  • Title တစ်ခုကို နှစ်ခါ ထည့်ကြည့်ပြီး ရလဒ်ကို print ထုတ်ပါ
playlist.cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

// Adds title unless it is already in the playlist; returns whether it was added.
bool addSong(std::vector<std::string>& playlist, const std::string& title) {
    // TODO: search with std::find; push_back only when the title is missing
    return false;
}

// Removes every copy of title; returns how many were removed.
std::size_t removeSong(std::vector<std::string>& playlist, const std::string& title) {
    // TODO: one call to std::erase does the work
    return 0;
}

void printPlaylist(const std::vector<std::string>& playlist) {
    // TODO: print "1. title", "2. title", ... with an index loop
}

int main() {
    std::vector<std::string> playlist;
    // TODO: add "Morning Run", "Focus Beats", "Morning Run" again, "Night Drive"
    // TODO: print, remove "Focus Beats", print again and show the size
}

ရလဒ်: ဒုတိယ "Morning Run" ထည့်တာက false ပြန်ပေးပြီး playlist မှာ song သုံးပုဒ်ပဲ ရှိပါတယ်။ "Focus Beats" ကို ဖယ်တော့ removed: 1၊ မရှိတဲ့ title ကို ဖယ်တော့ 0 ရပြီး နောက်ဆုံး size က 2 ပါ။

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

std::find(playlist.begin(), playlist.end(), title) က မတွေ့ရင် playlist.end() ကို ပြန်ပေးပါတယ်။ Print loop မှာ std::size_t index ကို 0 ကနေ size() မတိုင်ခင်ထိ ပတ်ပြီး i + 1 ကို ပြပါ။

အဖြေ ကြည့်ရန်
playlist.cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

bool addSong(std::vector<std::string>& playlist, const std::string& title) {
    if (std::find(playlist.begin(), playlist.end(), title) != playlist.end()) {
        return false;                            // already in the list
    }
    playlist.push_back(title);
    return true;
}

std::size_t removeSong(std::vector<std::string>& playlist, const std::string& title) {
    return std::erase(playlist, title);          // C++20: returns how many were removed
}

void printPlaylist(const std::vector<std::string>& playlist) {
    for (std::size_t i = 0; i < playlist.size(); ++i) {
        std::cout << i + 1 << ". " << playlist[i] << '\n';
    }
}

int main() {
    std::vector<std::string> playlist;
    std::cout << std::boolalpha;
    std::cout << "add Morning Run: " << addSong(playlist, "Morning Run") << '\n';
    std::cout << "add Focus Beats: " << addSong(playlist, "Focus Beats") << '\n';
    std::cout << "add Morning Run again: " << addSong(playlist, "Morning Run") << '\n';
    addSong(playlist, "Night Drive");
    printPlaylist(playlist);

    std::cout << "removed: " << removeSong(playlist, "Focus Beats") << '\n';
    std::cout << "removed missing: " << removeSong(playlist, "Unknown") << '\n';
    printPlaylist(playlist);
    std::cout << "size: " << playlist.size() << '\n';
}

Quick Quiz 1 / 3

std::vector<int> v{10, 20, 30}; v.erase(v.begin()); std::cout << v[0] << ' ' << v.size(); က ဘာ print ထုတ်မလဲ?

Quick Quiz 2 / 3

std::vector<int> v; (အလွတ်) ပြီးနောက် ဘယ် line က လုံခြုံပြီး သတ်မှတ်ထားတဲ့ အပြုအမူ ရှိလဲ?

Quick Quiz 3 / 3

Vector တစ်ခုထဲ reading 10,000 ခု အတိအကျ ထည့်မယ်၊ အဲဒီ အချိန်တစ်လျှောက် ပထမ element ဆီ pointer တစ်ခု ကိုင်ထားမယ်။ Pointer valid ဖြစ်နေအောင် ဘာလုပ်ရမလဲ?

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

  • std::vector က element တွေကို ဆက်တိုက်ဖြစ်တဲ့ block တစ်ခုထဲ သိမ်းပြီး လိုသလို ကြီးထွားကာ scope ပြီးရင် memory ကို ကိုယ်တိုင် free လုပ်ပါတယ်။
  • [] က မစစ်ပါဘူး၊ at() က စစ်ပြီး std::out_of_range ကို throw လုပ်ပါတယ်၊ vector အလွတ်မှာ front() နဲ့ back() က undefined ပါ။
  • size က သုံးထားတဲ့ element အရေအတွက်၊ capacity က ဆံ့တဲ့ အရေအတွက်ပါ၊ ကြီးထွားနှုန်းက implementation-defined ဖြစ်ပြီး reserve က reallocation ကို ရှောင်ပေးပါတယ်။
  • Reallocation၊ insert နဲ့ erase က iterator၊ pointer၊ reference တွေကို invalid ဖြစ်စေလို့ index ကို သိမ်းပါ။
  • C++20 std::erase နဲ့ std::erase_if က ကိုက်ညီတာ အားလုံးကို loop ထဲ erase လုပ်တဲ့ အန္တရာယ် မရှိဘဲ ဖယ်ပေးပါတယ်။

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

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

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

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

STL: Vector | Thuta Learning