profile-img
์ง€๋ฐ์ด์˜ ํ‹ฐ์Šคํ† ๋ฆฌ
images/slide-image

ใ…Žใ…Ž 

 

์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ 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' Related Articles +