ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อเข้าใจ containers (ภาชนะ) ใน C++ STL
Containers คือออบเจกต์ที่ใช้เก็บหลายองค์ประกอบ ซึ่งอาจเป็นชนิดเดียวกันหรือต่างกัน แบ่งได้เป็น:
- Sequence containers: array, vector, list
- Associative containers: set, map, multimap
- Unordered associative containers: unordered_set, unordered_map
- Container adapters: stack, queue
ตัวอย่างโค้ด (Array)
#include <iostream>
using namespace std;
int main() {
int array[10] = {1,2,3,4};
for(int i = 0; i < 10; i++) {
cout << array[i] << " ";
}
return 0;
}
เอาต์พุต
1 2 3 4 0 0 0 0 0 0