๐Ÿ“˜ C++

[C++] STL Vector ๊ด€๋ จ ํ•จ์ˆ˜ ์ •๋ฆฌ

ji_wonna 2022. 7. 15. 20:59

1. insert(์‚ฝ์ž…ํ•  ์œ„์น˜, ๊ฐ’) : ์ค‘๊ฐ„์— ์‚ฝ์ž…

vector<int> v;
for (int i = 0; i < 10; i++) {
	v.push_back(i);
}
v.insert(v.begin()+3, 123); //v[3]์— 123์ถ”๊ฐ€
for (const auto& i : v) cout << i << endl;
//0, 1, 2, 123, 3, ...


2. pop_back() : ๋งจ ๋ ์›์†Œ ์‚ญ์ œ


3. erase() : ํŠน์ • ์›์†Œ(๋ฒ”์œ„) ์‚ญ์ œ

  • ์ค‘๊ฐ„ ์‚ญ์ œ๋Š” ๋’ค์˜ ์›์†Œ๋“ค์ด ํ•œ ์นธ์”ฉ ์•ž๋‹น๊ฒจ์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋น„ํšจ์œจ์ 
  • erase(์œ„์น˜)
  • erase(first, end) : [first, end) ๋ฒ”์œ„ ์‚ญ์ œ
vector<int> v;
for (int i = 0; i < 10; i++) {
	v.push_back(i);
}
v.insert(v.begin()+3, 100);
for (const auto& i : v) cout << i<<", ";
cout << endl;
v.erase(v.begin()+2);//v[2]์‚ญ์ œ
for (const auto& i : v) cout << i<<", ";
cout << endl;
v.erase(v.begin() + 2, v.begin() + 7);//v[2]~v[6]์‚ญ์ œ
for (const auto& i : v) cout << i << ", ";


4. sort() : ์ •๋ ฌ

  • #include <algorithm>
  • sort(v.begin(), v.end()) : ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ex) 1, 2, 3, 4, 5
  • sort(v.begin(), v.end(), ๋น„๊ตํ•จ์ˆ˜) : ์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜, return ๊ฐ’์ด ์ฐธ์ด ๋˜๋„๋ก ์ •๋ ฌ
vector<int> v;
for (int i = 0; i < 10; i++) {
	v.push_back(i);
}
v.insert(v.begin()+3, 100);
sort(v.begin(), v.end());//๊ธฐ๋ณธ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
sort(v.begin(), v.end(), compare);//compare ํ•จ์ˆ˜๋Œ€๋กœ ์ •๋ ฌ
bool compare(int x, int y) return x>y;//๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ


5. max_element(), min_element() : ์ตœ๋Œ€, ์ตœ์†Œ

  • #include <algorithm>
  • iterator๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— *๋กœ ๊ฐ’์„ ์ฝ์–ด์•ผ ํ•จ(๋ฐฐ์—ด์˜ ํฌ์ธํ„ฐ์™€ ์œ ์‚ฌ)
  • ๋ฒกํ„ฐ โ†’ *max_element(v.begin(), v.end());
  • ์ •์ˆ˜ ๋ฐฐ์—ด โ†’ *max_element(arr, arr+sizeof(arr)/sizeof(int));
cout << *max_element(v.begin(), v.end()) << endl;
cout << *min_element(v.begin(), v.end()) << endl;


6. next_permutation() : ์ˆœ์—ด ์ •๋ ฌ

  • #include <algorithm>
  • bool next_permutation(์ˆœ์—ด์„ ๊ตฌํ•˜๊ณ  ์‹ถ์€ ๋ฒกํ„ฐ.begin(), end())
  • ๋‹ค์Œ ์ˆœ์—ด ์กด์žฌโ†’true return, ๋‹ค์Œ ์ˆœ์—ด ์กด์žฌx โ†’ false return
  • ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆœ์—ด์„ ๊ตฌํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์ •๋ ฌ์„ ๋จผ์ € ํ•ด์•ผ ํ•จ
  • ์ด์ „ ์ˆœ์—ด ๊ตฌํ•˜๊ธฐ : prev_permutation()
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
for (const auto& i : v) cout << i << ", ";
cout << endl;
cout << "-----์ˆœ์—ด-----" << endl;
do {//๋‹ค์Œ ์ˆœ์—ด์„ ๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋จผ์ € ์ถœ๋ ฅํ•ด์ฃผ๊ณ 
	for (const auto& i : v) cout << i << ", ";
	cout << endl;
} while (next_permutation(v.begin(), v.end()));


7. find() : ์š”์†Œ ์ฐพ๊ธฐ

  • find(v.begin(), v.end(), ์ฐพ๊ณ  ์‹ถ์€ ๊ฐ’);
  • ์ฐพ์€ ๊ฐ’์˜ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค iterator๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ *๋กœ ๊ฐ’์„ ์ฝ์–ด์•ผ ํ•จ
//์ •์ˆ˜
vector<int> num = { 1, 2, 3, 4, 5 };
auto i = find(num.begin(), num.end(), 3);
//i๋Š” vector<int>::iteratorํ˜•
if (i == num.end()) {
	cout << "์ฐพ์„ ์ˆ˜ ์—†์Œ" << endl;
}
else {
	cout << "index : " << i - num.begin() << endl;//i=2
	cout << "number : " << *(i+2) << endl;//5
}

//๋ฌธ์ž์—ด
vector<string> alpha = { "abc", "d", "efg", "hijklm", "nopq" };
vector<string>::iterator iter = find(alpha.begin(), alpha.end(), "efg");
cout << *iter << endl;//"efg"
*iter = "abc"; //alpha[2] = "abc"๋กœ ๋ณ€๊ฒฝ
for (const auto& i : alpha) {
	cout << i << endl;
    //"abc","d","abc","hijklm","nopq"
}

'๐Ÿ“˜ C++' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[C++] Pair Container ์‚ฌ์šฉ๋ฒ•  (0) 2022.07.18
[C++] ๋ฌธ์ž์—ด ํ•จ์ˆ˜ ์ •๋ฆฌ  (0) 2022.07.18