cpp_vector - 8BitsCoding/RobotMentor GitHub Wiki
- ์ด๋ค ์๋ฃํ๋ ๋ฃ์ ์ ์๋ ๋์ ๋ฐฐ์ด(๋ฉ๋ชจ๋ฆฌ๋ฅผ ์๋์ผ๋ก ๋๋ ค์ค๋ค.)
- ๊ธฐ๋ณธ ๋ฐ์ดํฐ
- ํด๋์ค
- ํฌ์ธํฐ
- ๊ทธ ์์ ์ ์ฅ๋ ๋ชจ๋ ์์๋ค์ด ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์์น
- ์์ ์๊ฐ ์ฆ๊ฐํจ์ ๋ฐ๋ผ ์๋์ผ๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ด๋ฆฌํด ์ค
- ์ด๋ค ์์์๋ ์์๋ก ์ ๊ทผ ๊ฐ๋ฅ
๋ฉ๋ชจ๋ฆฌํฌ๊ธฐ๋ฅผ ์๋์ผ๋ก ์กฐ์ ํด์ฃผ๋ ๋ฐฐ์ด์ด๋ผ ์๊ฐํ์.
- Example
- ์ฉ๋(capacity)์ ํฌ๊ธฐ(size)์ ์ฐจ์ด?
- ์ฉ๋๋๋ฆฌ๊ธฐ(reserve)
- ์์ํ๋์ ์ ๊ทผํ๊ธฐ
- Example2
- iterator์์ begin/end
- ์ญ๋ฐฉํฅ iterator
- ํน์ ์์น์ ์์ ์ฝ์
- ํน์ ์์น์ ์๋ ์์ ์ญ์
- ๋ฒกํฐ ๊ตํํ๊ธฐ
- ๋ฒกํฐ ์ถ์ํ๊ธฐ
- ๋ฒกํฐ์ ๋ชจ๋ ์์ ์ ๊ฑฐํ๊ธฐ
- ๊ฐ์ฒด๋ฅผ ์ง์ ๋ณด๊ดํ๋ ๋ฒกํฐ์ ๋ฌธ์ ์
- ๊ฐ์ฒด ์ง์ ๋ณด๊ด ๋ฒกํฐ์ ํด๊ฒฐ์ฑ ์ ์๋?
- 2์ฐจ์ vector ์ฌ์ฉํ๊ธฐ
- for(int i = 0; i < my2vector.size(); i++) { for(int j = 0; j < my2vector[i].size(); j++) cout << my2vector[i][j] << " "; cout << endl; }
- ๋ฒกํฐ์ ์ฅ์ ๊ณผ ๋จ์
#include <iostream>
#include <vector>
int main(){
std::vector<int> scores;
scores.reserve(2); // 2๊ฐ์ ๊ณต๊ฐ์ ์ก์๋ฌ๋ผ.
scores.push_back(30);
scores.push_back(50);
scores.pop_back();
std::vector<int> scores1(scores); // ๋ณต์ฌ ์์ฑ์์ ํ๋๋ ๊ฐ๋ฅ
}
- capacity : vector์ ํ ๋น๋ ์์ ๊ณต๊ฐ ์
- size : vector์ ์ค์ ๋ก ๋ค์ด ์๋ ์์ ์
.reserve
- ์ฉ๋์ด ์ฆ๊ฐํด์ผ ํ๋ฉด ์๋ก์ด ์ ์ฅ๊ณต๊ฐ์ ์ฌํ ๋นํ๊ณ ๊ธฐ์กด์์๋ค์ ๋ชจ๋ ์ ๊ณต๊ฐ์ผ๋ก ๋ณต์ฌํด์ค๋ค.
- ๋ถํ์ํ ์ฌํ ๋น์ ๋ง๊ธฐ ์ํด vector๋ฅผ ์์ฑํ ์งํ์ ์ด ํจ์๋ฅผ ํธ์ถํ๋ ๊ฒ์ ์ถ์ฒํ๋ค.
operator[](size_t n)
// OR
scores[i]
#include <iostream>
#include <vector>
int main(){
std::vector<int> scores;
scores.reserve(2); // 2๊ฐ์ ๊ณต๊ฐ์ ์ก์๋ฌ๋ผ.
scores.push_back(30);
scores.push_back(50);
for(size_t i = 0; i < scores.size(); ++i)
{
std::cout << scores[i] << " ";
}
}
๋จ, ์ ๋ฐฉ์์ vector์๋ง ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
๊ทธ ์ด์ธ์ ์ปจํ ์ด๋๋ iterator๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ค.!
for(std::vector<int>::iterator iter = scores.begin(); iter != scores.end(); ++iter) { std::cout << *iter << " "; }
- begin : vector์ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ฐ๋ณต์
- end : vector์ ๋ง์ง๋ง ์์ ๋ฐ๋ก ๋ค์ ์์๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ฐ๋ณต์ ๋ฐํ
- rbegin(), rend() ์ด์ฉ
std::vector<int> scores;
scores.reserve(4);
scores.push_back(10);
scores.push_back(50);
scores.push_back(38);
scores.push_back(100);
std::vector<int>::iterator it = scores.begin();
// 10, 50, 38, 100
it = scroes.insert(it, 80);
// 80, 10, 50, 38, 100
// it์์น์ insertํ๋ผ๋ ๋ง.
๋จ, ๋ณต์ฌ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค. ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ๋์ฉ ๋ค ๋ฐ์ด์ค์ผํ๋ค.
๋ ํฐ ๋ฌธ์ ๋ ๋ฉ๋ชจ๋ฆฌ ์ฌํ ๋น์ด ๋ฐ์ํ์๋๊ฐ ๋ ํฐ ๋ฌธ์ ์ด๋ค.
std::vector<int> scores;
scores.reserve(4);
scores.push_back(10);
scores.push_back(50);
scores.push_back(38);
scores.push_back(100);
std::vector<int>::iterator it = scores.begin();
// 10, 50, 38, 100
it = scores.erase(it);
// 50, 38, 100
vector<int> scores;
scores.reserve(2);
scores.push_back(85);
scores.push_back(73);
vector<int> anotherScores;
anotherScores.assign(7, 100); // 100, 100, 100, 100, 100, 100, 100
scores.swap(anotherSores);
// scores : 100, 100 ...
// anotherScores : 85, 73
swap์ฐ์ฐ์ ์ปดํจํ ํ์๊ฐ ๊ทธ๋ฆฌ ํฐ ์ฐ์ฐ์ ์๋๋ค.
vector๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ผ๋ก ํ ๋น๋์ด ์๊ธฐ์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์ ๊ตํ๊ณผ capacity, size์ ๋๋ง ๋ณต์ฌํ๊ธฐ ๋๋ฌธ์ด๋ค.
.resize(2);
.clear();
vecor<Score> scores;
scores.reserve(1);
scores.push_back(Score(10, "C++"));
scores.push_back(Score(41, "Android"));
- ์์ ๊ฐ์ด ๋ฉ๋ชจ๋ฆฌ ์ฌ ํ ๋น์ด ์ผ์ด๋๋ฉด Score๋ผ๋ ๊ฐ์ฒด๋ฅผ ๋ณต์ฌํด์ ๋ฉ๋ชจ๋ฆฌ ์ฌ ํ ๋น์ ํด์ผํ๋ค.
- ์ ์์ ๋ ๊ฐ์ฒด์ ํฌ๊ธฐ๊ฐ ์์ง๋ง ๊ฐ์ฒด์ ํฌ๊ธฐ๊ฐ ํฌ๋ค๋ฉด?? ์์ฒญ๋ ๋ณต์ก๋๋ฅผ ์ง๋๊ฒ ๋๋ค.
- ํฌ์ธํฐ์ ์ฌ์ฉ
vecor<Score> scores;
scores.reserve(1);
scores.push_back(new Score(10, "C++"));
scores.push_back(new Score(41, "Android"));
scores.clear();
ํ ... ๋ญ๊ฐ ์ด์ํ๋ฐ...?
newํ๋ค๋ฉด? delete๋??
vecor<Score> scores;
scores.reserve(1);
scores.push_back(new Score(10, "C++"));
scores.push_back(new Score(41, "Android"));
for(vector<Score*>::iterator iter = scores.begin(); iter != scores.end(); ++iter)
{
delete *iter;
}
scores.clear();
// ์ฐ์ ๊ฐ๋จํ๊ฒ๋ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
vector<vector<int>> my2vector{{1, 2, 3},
{4, 5, 6,},
{7, 8, 9,} };
for(int i = 0; i < my2vector.size(); i++) {
for(int j = 0; j < my2vector[i].size(); j++)
cout << my2vector[i][j] << " ";
cout << endl;
}
// ์ด๋ ๊ฒ๋ ์ฝ์
์ด ๊ฐ๋ฅ.
vector<vector<int>> my2vector2;
my2vector2.reserve(3);
for(int i = 0; i < my2vector2.capacity(); i++) {
my2vector2.push_back({1,2,3});
}
for(int i = 0; i < my2vector2.size(); i++) {
for(int j = 0; j < my2vector2[i].size(); j++)
cout << my2vector2[i][j] << " ";
cout << endl;
}
swap(my2vector[1][1], my2vector2[1][1]);
- ์์์ ์๊ด์์ด ์์์ ์์์ ์ผ๋ก ์ ๊ทผ์ด ๊ฐ๋ฅ
- ์ ์ผ ๋ง์ง๋ง ์์น์ ์์๋ฅผ ๋น ๋ฅด๊ฒ ์ฝ์ ๋ฐ ์ญ์ ๊ฐ๋ฅ
- ์ค๊ฐ์ ์์ ์ฝ์ ๋ฐ ์ญ์ ๊ฐ ๋๋ฆฌ๋ค.
- ์ฌํ ๋น ๋ฐ ์์ ๋ณต์ฌ์ ๋๋ ๋น์ฉ์ด ํฌ๋ค.