C++/c++
[STL] ์ฐ์ ์์ ํ priority_queue
์ฐ์ ์์ ํ priority_queue๋ ํ์ ์๋ ๋ชจ๋ ์์ ์ค์์ ๊ฐ์ฅ ํฐ ๊ฐ์ด Top์ ์ ์งํ๋๋ก, ์ฐ์ ์์๊ฐ ์ค์ ๋์ด ์๋ค. ์ฐ์ ์์ ํ๋ ์๋ฃ๊ตฌ์กฐ Heap์ผ๋ก ๊ตฌํ๋์ด ์์ด pop๊ณผ push๋ฅผ ํ ๋ ๋ง๋ค ์ ๋ ฌ๋๋ค. priority_queue pq; ์ด๋ ๊ฒ ์ฐ๋ฉด ๋ด๋ฆผ์ฐจ์์ผ๋ก queue์ push๋๊ณ priority_queue pq; ์ด๋ ๊ฒ ์ฐ๋ฉด ์ค๋ฆ์ฐจ์์ผ๋ก queue์ push๋๋ค. push() pop() top์ ์์ ์ ๊ฑฐ top() ์ฐ์ ์์ ํ์์ top์ ์๋ ์์ ์ฆ ์ฐ์ ์์๊ฐ ๋์ ์์๋ฅผ ๋ฐํ empty() ๋น์ด์์ผ๋ฉด true๋ฅผ ๋ฐํํ๊ณ ๊ทธ๋ ์ง ์์ผ๋ฉด false๋ฅผ ๋ฐํ size() ์์์ ์๋ฅผ ๋ฐํ ๋ด๋ฆผ์ฐจ์์ผ๋ก queue์ push #include #include using namesp..
[STL] set
Set์ ํน์ง - ์์๊ฐ ์ฝ์ ๋๋ฉด ์๋์ผ๋ก ์ ๋ ฌ sets; // ์ค๋ฆ์ฐจ์ sets; // ๋ด๋ฆผ์ฐจ์ - ์ค๋ณต๊ฐ์ ํ์ฉํ์ง ์๋๋ค. set์ ์์๊ฐ ๋ค์ด๊ฐ๋ฉด ๊ทธ ์์์ ๊ฐ์ ํค(key)๋ผ๊ณ ํ๋ฉฐ ์ค๋ณต์์๋ฅผ ํ์ฉํ์ง ์๊ธฐ ๋๋ฌธ์ ๊ฐ ํค๋ ์ ๋ถ ๊ณ ์ ํ๋ค. Set์ ์ฌ์ฉํ๊ธฐ ์ข์ ๊ฒฝ์ฐ 1. ์ฝ์ ๊ณผ ๋์์ ์ ๋ ฌ์ด ํ์ํ ๋ 2. ์ค๋ณต์ ์ ๊ฑฐํ ์์์ ์งํฉ์ด ํ์ํ ๋ 3. ์๋ก ์ฝ์ ํ๋ ค๋ ์์๊ฐ ์ค๋ณต์ธ์ง ํ์ธํ ๋ sets; std::pair ret; ret = s.insert("a"); // s={a} cout
[STL] unique๋ก vector์์ ์ค๋ณต ์์ ์ ๊ฑฐํ๊ธฐ : ์ sort ํ unique!
์ฐ์ ํค๋๊ฐ ํ์ํ๋ค. sort์ unique๋ฅผ ํจ๊ป ์จ์ฃผ์ด์ผ ์๋ ํ ๋ฐ๋๋ก unique๋ก vector์์ ์ค๋ณต๋๋ ์์๋ฅผ ์ ๊ฑฐํ ์ ์๋ค. ๋ ๊ฐ์ง ๋ก์ง์ด ๊ฐ๋ฅํ๋ค. ์ฒซ๋ฒ์งธ sort(result.begin(),result.end()); result.resize(unique(result.begin(),result.end())-result.begin()); ๋๋ฒ์งธ sort(result.begin(),result.end()); result.erase(unique(result.begin(), result.end()),result.end()); ๋ ๊ตฌ๋ฌธ์ ๋ณด๋ฉด unique๋ ์ค๋ณต๋๋ ์์๋ค์ด ์ ์ธ๋ ์ํ์ vector์ ๋ง์ง๋ง ์์ ๋ค์ iterator๊ฐ์ ๋ฆฌํดํ๋ค๋ ๊ฒ์ ๋์น์ฑ ์ ์๋ค. ์ฆ, unique๋..
![[STL] map์์ Value๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ณ ์ถ์ ๊ฒฝ์ฐ](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbxKBBj%2FbtrqMcPn5QV%2FbJP0xmrFWzlFldpI3ncUn0%2Fimg.png)
[STL] map์์ Value๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ณ ์ถ์ ๊ฒฝ์ฐ
์ฐธ๊ณ ๋งํฌ: C++ STL์ ๊ฐ๋ณ๋ก ๋งต ์ ๋ ฌ - GeeksforGeeks ํด๋น ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ๋ฉด ๋๋ค. ๊ฒฐ๋ก ์ map STL ๋จ๋ ์ผ๋ก๋ Value ๊ธฐ์ค์ผ๋ก sortํ ์ ์๋ค. ๋ฐ๋ผ์, Pair ๋ฅผ ๋ด๋ vector์ ์ ์ธํ์ฌ map iteratior์ ๋ฐ๋ผ ์ฝ์ ํด์ฃผ๊ณ vector์ Pair second๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ค์ sortํด์ผํ๋ค. Sorting a Map by value in C++ STL - GeeksforGeeks A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/c..
[STL] Vector๋ด ์ต๋๊ฐ, ์ต์๊ฐ๊ณผ ๊ทธ ์ธ๋ฑ์ค ์์๋ด๊ธฐ
vector ์ปจํ ์ด๋์์ ์ต๋๊ฐ, ์ต์๊ฐ์ ๊ตฌํ๊ธฐ ์ํด #include ๋ฅผ ์จ์ฃผ๊ณ max_element(๋ฒกํฐ.begin(), ๋ฒกํฐ.end())-๋ฒกํฐ.begin() ๊ตฌ๋ฌธ์ ์์ฑํ๋ฉด ์ต๋๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ๊ตฌํ ์ ์๋ค. max_element์ ๊ฒฐ๊ณผ๋ก ์ต๋๊ฐ์ ๊ฐ๋ฆฌํค๋ ๋ฐ๋ณต์๋ฅผ ๋ฐํํ๊ธฐ ๋๋ฌธ์ด๋ค. ์๋ฆฌ : (์ต๋๊ฐ์ ๊ฐ๋ฆฌํค๋ ๋ฐ๋ณต์ - ๋งจ ์ฒ์์ ๊ฐ๋ฆฌํค๋ v.begin()) ๋๋ถ์ด * ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ฉด ์ต๋๊ฐ์ ๊ตฌํ ์ ์๋ค. ๋ํ, max_element๋ฅผ ์ฌ์ฉํ๋ฉด ์ต๋๊ฐ์ ์ธ๋ฑ์ค ๊ฐ๋ ๊ตฌํด๋ผ ์ ์๋ค. ์ต์๊ฐ๊ณผ ๊ทธ ์ธ๋ฑ์ค๋ฅผ ๊ตฌํ๊ธฐ ์ํด์๋ min_element๋ก ๋ฐ๊ฟ์ ์ฌ์ฉํด์ฃผ๊ธฐ๋ง ํ๋ฉด ๋๋ค. #include #include #include using namespace std; int main() { vecto..
[STL] key ์ค๋ณต๊ฐ๋ฅํ multimap๊ณผ make_pair ์กฐํฉ
#include #include #include #include #include using namespace std; int main(void) { multimap mm; multimap ::iterator mmiter; vectorkind_name1; vectorkind_name2; vectorkind_name3; vector clothes; kind_name1.push_back("yellowhat"); kind_name1.push_back("headgear"); kind_name2.push_back("bluesunglasses"); kind_name2.push_back("eyewear"); kind_name3.push_back("green_turban"); kind_name3.push_back("he..
[STL] unordered_map๊ณผ make_pair ๊ฐ์ด ์ฐ๊ธฐ
unordered_map์ hash ๊ธฐ๋ฐ์ผ๋ก map์ tree ๊ธฐ๋ฐ์ผ๋ก ๊ตฌํ๋๊ธฐ ๋๋ฌธ์ unordered_map์ key๋ค์ ์ ๋ ฌ๋์ด ์์ง ์๊ณ , map์ key๋ค์ ์ ๋ ฌ๋์ด์๋ค. ํ์๋ฐฉ๋ฒ index๋ก ์ ๊ทผํ ์ ์๊ณ iterator๋ก ์ ๊ทผ ์์ : begin( ), ๋ : end( ) key : iter->first, value : iter->second ๋ฐ๋ณต๋ฌธ ์ฌ์ฉ ์ auto ํ์ฉ or pair ์ฌ์ฉ #include #include #include #include #include using namespace std; int main(void) { unordered_map hs; vectorkind_name1; vectorkind_name2; vectorki..
[STL] ์ด์ค Vector : ์์ vector<vector<string>>, ์ฝ๋์ฒจ๋ถ
vector clothes; ์ push_back() ๊ฐ๋ฅํ ๊ฒ์, vector kind_name1; vector kind_name2; vector kind_name3; ๋ฑ์ vectorํ ๋ฟ์ด๋ค. ๊ฐ๋ น, kind_name1,kind_name2,kind_name3์ ๊ฐ๊ฐ ์ ๋นํ string์ push_back()ํด์ฃผ๊ณ ์ด๊ฒ๋ค์ ๋ vector clothes; ์ push_back() ํ๋ค๋ฉด... kind_name1.push_back("yellowhat"); kind_name1.push_back("headgear"); kind_name2.push_back("bluesunglasses"); kind_name2.push_back("eyewear"); kind_name3.push_back("green_turba..
[STL] multimap์ ํ์ ๋ฐฉ๋ฒ
ํ์๋ฐฉ๋ฒ index๋ก ์ ๊ทผํ ์ ์๊ณ iterator๋ก ์ ๊ทผ ์์ : begin( ), ๋ : end( ) key : iter->first, value : iter->second ๋ฐ๋ณต๋ฌธ ์ฌ์ฉ ์ auto ํ์ฉ or pair ์ฌ์ฉ #include #include #include #include #include //multimap์ ์ค๋ณต key๋ฅผ ํ์ฉ using namespace std; int main(void) { multimap mm; multimapmultimapstr; multimapstr.insert(make_pair("0","cry")); multimapstr.insert(make_pair("0", "343")); for (pair tempmap : m..