ใ ใ
์ฐ๊ด ์ปจํ ์ด๋ Associative Container
automatically sorted
์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋จ (<)
Sets and Multisets
- automatically sorted ( By default, in ascending order)
- multisets์ ์ค๋ณตํ์ฉ, sets๋ ํ์ฉ ์ํจ !
๊ธฐ๋ณธ์์ฑ์ default constructor
set<int> set1;
set1.insert(6);
set1.insert(2);
set1.insert(4); // set is: 2 4 6
-> ๊ธฐ๋ณธ์ ์ผ๋ก ์ฌ์ด์ฆ๊ฐ 0์ธ set์ด ๋ง๋ค์ด์ง
๋ณต์ฌ์์ฑ์ copy constructor
set<int> set3(set1);
set<int> set4 = set1;
์ด๊ธฐํ ๋ฆฌ์คํธ ์์ฑ์ initializer list constructor
set<int> set5 = {10, 30, 20};
//Elements will be stored in ascending order
๋ฒ์ ์์ฑ์ Range constructor
set<int>::iterator iter = set3.begin(); ++iter;
// ๋๋ auto iter = set3.begin()์ผ๋ก ์จ๋๋จ
set<int> set4(iter, set3.end());
// set4 is: 20 30 40
set::erase(value)
size_type erase(const value_type& val);
// erase() ํจ์๋ ์ญ์ ๋ ์์์ ๊ฐฏ์๋ฅผ ๋ฐํํจ!
multiset<int> aMultiSet =
{ 14, 12, 13, 17, 14, 19, 18, 16, 14, 14, 19, 11, 15 };
size_t itemsDeleted = aMultiSet.erase(14);
cout << itemsDeleted << "\n"; //4, ์ญ์ ๋ 14์ ๊ฐฏ์
for (auto elem : aMultiSet) cout << elem << " "; // 11 12 13 15 16 17 18 19 19
set::erase(position)
iterator erase(const_iterator position);
multiset<int> aMultiset = { 11, 12, 13, 15, 17 };
auto iterSet = ++aMultiset.begin();
cout << *iterSet << endl; //12
iterSet = aMultiset.erase(iterSet); //-> ์ญ์ ๋ ์์์ ๋ค์์์๋ฅผ ๊ฐ๋ฅดํด
cout << *iterSet << endl; // 13
for(auto elem : aMultiSet)
cout << elem << endl; // 11 13 15 17
set::erase(range)
iterator erase(const_iterator first, const_iterator last);
set<int> aSet = {11, 12, 13, 15, 17, 18, 19};
auto iterSet = aSet.begin();
for (int i = 1; i < 6; ++i)
{
++iterSet; //5๋ฒ๋ฐ๋ณต
}
cout << *iterSet << endl; // 18 (6th element)
iterSet = aSet.erase(++aSet.begin(), iterSet); // 12 ~ 18 ์ฌ์ด์ ์๋๊ฑฐ ๋ค์ง์
cout << *iterSet << endl; // 18(์ญ์ ๋ ์์ ๋ค์ ์๋ ๋ฐ๋ก ์ฒซ ์์
for(auto elem:aSet)
cout << elem << endl; // 11 18 19
-> ์ ๋ง์ง๋ง์ด 11 12 18 19 ๊ฐ ์๋์ง??
set::find(value)
set<int> aSet = { 23, 34, 48, 57, 68, 71, 83 };
auto iter = aSet.find(57); //iter์ 57์ ๊ฐ๋ฆฌํค๋ ๋ฐ๋ณต์๊ฐ๋จ
if (iter != aSet.end())
cout << "Element found."; // set์์ 57์ ์ฐพ์ผ๋ฉด ์ถ๋ ฅ !
else
cout << "Element not found.";
* find ํจ์๋ True and False, Boolean Value๋ก ๋ฆฌํดํ ์ ์๋ค !
Maps and Multimaps
- elements๋ฅผ key/value์์ ํํ๋ก ๊ด๋ฆฌํจ
- automatically sorted ( By default, in ascending order)
- multimaps์ ์ค๋ณตํ์ฉ, maps๋ ํ์ฉ ์ํจ !
์ด๊ธฐํ ๋ฆฌ์คํธ ์์ฑ์ initializer list constructor
-> When data is immediately available !!
map<int, string> aMap = {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}};
auto iterEnd = aMap.cend();
for (auto iter = aMap.cbegin(); iter != iterEnd; ++iter)
cout << iter->first << " " << iter->second << "\n";
๊ธฐ๋ณธ์์ฑ์์ ๋๊ดํธ(?) Default Constructor + Subscript
-> When data is NOT immediately available !!
map<char, int> aMap;
aMap['A'] = 90;
aMap['b'] = 80;
aMap['C'] = 70;
aMap['D'] = 60;
auto iterEnd = aMap.cend();
for (auto iter = aMap.cbegin(); iter != iterEnd; ++iter)
cout << iter->first << " " << iter->second << "\n";
// Output : A 90, C 70, D 60, b 80
// ASCII code ์ ๋ฐ๋ผ์ b 80์ด ๋ค๋ก ๊ฐ
๊ธฐ๋ณธ์์ฑ์ Default Constructor + map::insert
-> insert / make_pair ์ฌ์ฉ !!
map<int, int> intMap;
for (int i = 1; i < 10; ++i) // insert integers
intMap.insert (make_pair (i, (100 / i)));
auto itEnd = intMap.cend();
for (auto it = intMap.cbegin(); it != itEnd; ++it)
cout << it->first << " " << it->second << "\n";
// map is: {(1,100),(2,50),(3,33),...(8,12),(9,11)}
map::find(value)
const_iterator find(const value_type& k) const;
map<string, int> states = {{"AK", 1959}, {"CA", 1850}, {"FL", 1845}, {"IL", 1818}, {"IO", 1846}, {"OR", 1859}};
auto iter = states.find("CA");
if (iter != states.end())
cout << "Year: " << iter->second;
else
cout << "State not found.";
'เซฎโหถแต แต แตหถโแโก > coding' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] SLL(3)!! ์ฐ์ตํ๊ธฐ ^ใ ^ (0) | 2024.04.01 |
---|---|
[C++] STL Pair, Nested Containers ์ค์ฒฉ ์ปจํ ์ด๋ (0) | 2024.03.31 |
[C++] STL ์์ฐจ ์ปจํ ์ด๋ STL Sequence Container (Vector, List) (0) | 2024.03.29 |
[C++] ostream iterator ์ถ๋ ฅ์คํธ๋ฆผ ๋ฐ๋ณต์ + etc... (0) | 2024.03.29 |
[C++] Cycling Iterators ์ํ ๋ฐ๋ณต์ (0) | 2024.03.29 |