cpp_vector - 8BitsCoding/RobotMentor GitHub Wiki

Vector๋ž€?

  • ์–ด๋–ค ์ž๋ฃŒํ˜•๋„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ๋™์ ๋ฐฐ์—ด(๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ๋Š˜๋ ค์ค€๋‹ค.)
    • ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ
    • ํด๋ž˜์Šค
    • ํฌ์ธํ„ฐ
  • ๊ทธ ์•ˆ์— ์ €์žฅ๋œ ๋ชจ๋“  ์š”์†Œ๋“ค์ด ์—ฐ์†๋œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ์œ„์น˜
  • ์š”์†Œ ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ด€๋ฆฌํ•ด ์คŒ
  • ์–ด๋–ค ์š”์†Œ์—๋„ ์ž„์˜๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ

๋ฉ”๋ชจ๋ฆฌํฌ๊ธฐ๋ฅผ ์ž๋™์œผ๋กœ ์กฐ์ •ํ•ด์ฃผ๋Š” ๋ฐฐ์—ด์ด๋ผ ์ƒ๊ฐํ•˜์ž.


๋ชฉ์ฐจ


Example

#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)์™€ ํฌ๊ธฐ(size)์˜ ์ฐจ์ด?

  • capacity : vector์— ํ• ๋‹น๋œ ์š”์†Œ ๊ณต๊ฐ„ ์ˆ˜
  • size : vector์— ์‹ค์ œ๋กœ ๋“ค์–ด ์žˆ๋Š” ์š”์†Œ ์ˆ˜

์ด๋ฏธ์ง€


์šฉ๋Ÿ‰๋Š˜๋ฆฌ๊ธฐ(reserve)

.reserve
  • ์šฉ๋Ÿ‰์ด ์ฆ๊ฐ€ํ•ด์•ผ ํ•˜๋ฉด ์ƒˆ๋กœ์šด ์ €์žฅ๊ณต๊ฐ„์„ ์žฌํ• ๋‹นํ•˜๊ณ  ๊ธฐ์กด์š”์†Œ๋“ค์„ ๋ชจ๋‘ ์ƒˆ ๊ณต๊ฐ„์œผ๋กœ ๋ณต์‚ฌํ•ด์ค€๋‹ค.
  • ๋ถˆํ•„์š”ํ•œ ์žฌํ• ๋‹น์„ ๋ง‰๊ธฐ ์œ„ํ•ด vector๋ฅผ ์ƒ์„ฑํ•œ ์งํ›„์— ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

์š”์†Œํ•˜๋‚˜์— ์ ‘๊ทผํ•˜๊ธฐ

operator[](size_t n)
// OR
scores[i]

Example2

#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 << " ";
}

iterator์—์„œ begin/end

  • begin : vector์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž
  • end : vector์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ ๋ฐ”๋กœ ๋’ค์˜ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž ๋ฐ˜ํ™˜

์ด๋ฏธ์ง€


์—ญ๋ฐฉํ–ฅ iterator

  • 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();

2์ฐจ์› vector ์‚ฌ์šฉํ•˜๊ธฐ

// ์šฐ์„  ๊ฐ„๋‹จํ•˜๊ฒŒ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
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]);

๋ฒกํ„ฐ์˜ ์žฅ์ ๊ณผ ๋‹จ์ 

์žฅ์ 

  • ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด ์š”์†Œ์˜ ์ž„์˜์ ์œผ๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ
  • ์ œ์ผ ๋งˆ์ง€๋ง‰ ์œ„์น˜์— ์š”์†Œ๋ฅผ ๋น ๋ฅด๊ฒŒ ์‚ฝ์ž… ๋ฐ ์‚ญ์ œ ๊ฐ€๋Šฅ

๋‹จ์ 

  • ์ค‘๊ฐ„์— ์š”์†Œ ์‚ฝ์ž… ๋ฐ ์‚ญ์ œ๊ฐ€ ๋А๋ฆฌ๋‹ค.
  • ์žฌํ• ๋‹น ๋ฐ ์š”์†Œ ๋ณต์‚ฌ์— ๋“œ๋Š” ๋น„์šฉ์ด ํฌ๋‹ค.
โš ๏ธ **GitHub.com Fallback** โš ๏ธ