π‘ λ³Έ λ¬Έμλ 'std 컨ν μ΄λ: μλ£κ΅¬μ‘°λ₯Ό μ΄ν΄νκ³ μ¬μ©νμ!'μ λν΄ μ 리ν΄λμ κΈμ λλ€.
C++ νμ€ λΌμ΄λΈλ¬λ¦¬μ ꡬνλμ΄ μλ 컨ν μ΄λμ λν΄ μ 리νμμΌλ μ°Έκ³ νμκΈ° λ°λλλ€.
1. 컨ν μ΄λ (Container)
Sequence Containers
sequential containers are responsible to store data in sequence. It consists of arrays, vector, deque, forward list and list.
Associative Containers
Associative containers store associated/related data as key-value pairs. It consists of set, map, multi set and multimaps.
Unordered Associative Containers
Unordered elements use a hash function to map keys with their values. These unordered containers include the unordered set, unordered multiset, unordered map and unordered multimap.
2. C++ STL 컨ν μ΄λ (Container)
array
κ³ μ κΈΈμ΄λ₯Ό κ°μ§λ λ°°μ΄. ν¬κΈ°λ μ»΄νμΌ μμ κ³ μ λλ©°, λ³κ²½ν μ μμ΅λλ€. indexμ μ κ·Όν΄μΌλ§ νλ μ΄μ κ° μλ μ΄μ range-based for(e.g. auto const& num : nums)λ₯Ό μ¬μ©νλλ‘ ν©λλ€.
#include "spdlog/fmt/fmt.h"
int main() {
std::array<int, 3> nums{ 1, 2, 3};
for(auto const& num : nums) { fmt::print("{}\n", num); }
return 0;
}
vector
stackκ³Ό κ°μ΄ ν¬κΈ°λ₯Ό μ‘°μ ν μ μλ 컨ν μ΄λ
#include "spdlog/fmt/fmt.h"
int main()
{
std::vector<int> nums{ 1, 2, 3};
names.push_back(4);
for(auto const& num : nums) { fmt::print("{}\n", num); }
return 0;
}
set (unordered_set)
μ§ν©μ λ€λ£¨κΈ° μν΄ μ¬μ©νλ 컨ν μ΄λ.
#include "spdlog/fmt/fmt.h"
#include <set>
int main()
{
std::set<int> primes{2, 3, 5, 7, 5};
for (auto const &prime : primes)
{
fmt::print("{}\n", prime);
}
return 0;
}
map (unordered_map)
νν λ§νλ λμ λ리μ κ°μ κ°λ μΌλ‘ ν€-κ° μμ μ μ₯νκΈ° μν΄ μ¬μ©λλ©°, ν€λ₯Ό ν΅ν΄ κ°μ μ°Ύμ μ μμ΅λλ€.
#include "spdlog/fmt/fmt.h"
#include <unordered_map>
int main()
{
std::unordered_map<int, std::string> primes{
{2, "2"},
{3, "3"},
{5, "5"},
{7, "7"}};
for (auto const &prime : primes)
{
fmt::print("[{}]{}\n", prime.first, prime.second);
}
return 0;
}
3. μ¬μ©ν 컨ν μ΄λ κ³ λ₯΄λ λ°©λ²
- μ§ν©μ΄ νμν κ²½μ° set νΉμ unordered_setμ μ¬μ©ν©λλ€.
- ν€-κ° μ»¨ν μ΄λκ° νμν κ²½μ° mapνΉμ unordered_mapμ μ¬μ©ν©λλ€.
- κ·Έ μΈμ κ²½μ°λ λ€μ λνλ₯Ό μ°Έμ‘°νμΈμ.
μ°Έκ³
- [hacking C++] Std.Library: https://hackingcpp.com/cpp/std/associative_containers.html