๐Ÿง‘๐Ÿปโ€๐Ÿ’ป

C++ vector

Tags
C++
Algorithm
ID matched
Created
Mar 17, 2023 10:36 PM
Last Updated
Last updated July 15, 2023
ย 
ย 
ย 

1. vector ๊ธฐ๋ณธ ์˜ˆ์ œ

#include <vector> #include <iostream> using namespace std; int main() { vector<int> items; items.push_back(1); items.push_back(8); items.push_back(15); items.push_back(20); items.pop_back(); // items.insert(์‹œ์ž‘์ , ๊ฐœ์ˆ˜, ๊ฐ’); items.insert(items.end(), 2, 30); cout << "[size] : " << items.size() << endl; cout << "[first item] : " << items.front() << endl; cout << "[last item] : " << items.back() << endl; return 0; }
notion image
ย 

2. vector ์ˆœ์—ด

#include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { vector<int> items{3, 4, 6, 8, 12}; int len = items.size(); for (int i = items.size() - 1; i >= 0; i--) { vector<bool> states(i, false); states.insert(states.begin(), len - i, true); do { cout << "[items] "; string value = ""; for (int j = 0; j < states.size(); j++) { cout << states[j] << " "; if (states[j]) { value += to_string(items[j]) + " "; } } cout << ">> " << value << endl; } while (prev_permutation(states.begin(), states.end())); } return 0; }
notion image
ย 
ย 

3. vector ๊ฐœ์ˆ˜ ์„ธ๊ธฐ

#include <vector> #include <iostream> #include <set> #include <algorithm> using namespace std; int main() { vector<int> items{3, 3, 5, 8, 5, 2, 8, 10}; set<int> item_set(items.begin(), items.end()); for (int item : item_set) { int item_count = count(items.begin(), items.end(), item); cout << item << " >> " << item_count << "\n"; } return 0; }
notion image
ย 
ย 

4. vector heap

#include<vector> #include<iostream> #include<algorithm> using namespace std; int main() { int answer = 0; vector<int> items{5, 3, 8, 9, 2}; int count = 4; make_heap(items.begin(), items.end()); while (count > 0) { pop_heap(items.begin(), items.end()); int max_value = items.back(); if (max_value <= 0) break; items.pop_back(); items.push_back(max_value - 1); count--; } for (int i = 0; i < items.size(); i++) { cout << items[i] << "\t"; } cout << endl; return 0; }
notion image
ย