[c++] ๋ฐฐ์—ด ๋ฐ (STL) vector ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ• ์ •๋ฆฌ

์ปจํ…Œ์ด๋„ˆ์— ๊ธฐ๋ณธ๊ฐ’ ์ฑ„์šฐ๊ธฐ

๋ฐฐ์—ด

1์ฐจ์› ๋ฐฐ์—ด

๊ธฐ์กด ์ฝ”๋“œ

#include <iostream>

using namespace std;

int main() {
    int arr[3];

    for (int i = 0; i < 3; i++) {
            cout << arr[i] << " ";
    }
}
<๊ฒฐ๊ณผ>
-858993460 -858993460 -858993460

์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์“ฐ๋ ˆ๊ธฐ๊ฐ’์ด ๋‚˜์˜จ๋‹ค.

1. fill_n ํ•จ์ˆ˜ ์ด์šฉ

int main() {
    int arr[3];

    fill_n(arr, 3, -1);

    for (int i = 0; i < 3; i++) {
            cout << arr[i] << " ";
    }
}
<๊ฒฐ๊ณผ>
-1 -1 -1

2. memset ํ•จ์ˆ˜ ์ด์šฉ

int arr[3];
    memset(arr, -1, sizeof(arr));

    for (int i = 0; i < 3; i++) {
            cout << arr[i] << " ";
    }
}
<๊ฒฐ๊ณผ>
-1 -1 -1

 

๋‹ค์ฐจ์› ๋ฐฐ์—ด

๊ธฐ์กด ์ฝ”๋“œ

#include <iostream>

using namespace std;

int main() {
    int arr[3][3];

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}
<๊ฒฐ๊ณผ>
-858993460 -858993460 -858993460
-858993460 -858993460 -858993460
-858993460 -858993460 -858993460

์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•„ ์“ฐ๋ ˆ๊ธฐ๊ฐ’์ด ๋‚˜์˜จ๋‹ค.

1. fill ํ•จ์ˆ˜ ์ด์šฉ

int main() {
    int arr[3][3];
    fill(&arr[0][0], &arr[2][3], -1);
    // ์ฑ„์šฐ๊ณ ์žํ•˜๋Š” ํ–‰ ์ˆ˜๋Š” -1, ์—ด ์ˆ˜๋Š” ๊ทธ๋Œ€๋กœ

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}
<๊ฒฐ๊ณผ>
-1 -1 -1
-1 -1 -1
-1 -1 -1
์‹คํ—˜
์‹œ๋„ ์ฝ”๋“œ ๊ฒฐ๊ณผ
1 fill(&arr[0][0], &arr[2][2], -1); -1 -1 -1
-1 -1 -1
-1 -1 -858993460
2 fill(&arr[0][0], &arr[2][1], -1); -1 -1 -1
-1 -1 -1
-1 -858993460 -858993460
3 fill(&arr[0][0], &arr[1][3], -1); -1 -1 -1
-1 -1 -1
-858993460 -858993460 -858993460

์‹œ๋„ 1, 2๋ฅผ ํ†ตํ•ด

์—ด์€ ๋งˆ์ง€๋ง‰ ํ–‰์— ๋ฐ˜์˜๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

2๋ฅผ ์ž…๋ ฅํ–ˆ์œผ๋ฉด ๋งˆ์ง€๋ง‰ ํ–‰์˜ index 0~1๊นŒ์ง€๋งŒ

1์„ ์ž…๋ ฅํ–ˆ์œผ๋ฉด ๋งˆ์ง€๋ง‰ ํ–‰์˜ index 0~0๊นŒ์ง€๋งŒ

์‹œ๋„ 3์„ ํ†ตํ•ด

ํ–‰์€ ๊ทธ๋Œ€๋กœ ๋‘๋ฒˆ์งธ์ธ์ž์— ์ž…๋ ฅํ•œ ๊ทธ๋Œ€๋กœ ๋ฐ˜์˜๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

1์„ ์ž…๋ ฅํ–ˆ์œผ๋ฉด index 0 ~ 1๊นŒ์ง€, 2๋ฅผ ์ž…๋ ฅํ–ˆ์œผ๋ฉด index 0 ~ 2๊นŒ์ง€

2. memset ํ•จ์ˆ˜ ์ด์šฉ

int main() {
    int arr[3][3];
    memset(arr, -1, sizeof(arr));

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}
<๊ฒฐ๊ณผ>
-1 -1 -1
-1 -1 -1
-1 -1 -1

 

vector ์ดˆ๊ธฐํ™”

STL ์˜ ์ปจํ…Œ์ด๋„ˆ ์ค‘ ํ•˜๋‚˜์ธ vector๋ฅผ ์ดˆ๊ธฐํ™” ํ•ด๋ณด์ž.

1์ฐจ์› vetor

๊ธฐ์กด ์ฝ”๋“œ

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> vec;

    vector<int>::iterator iter;
    for (iter = vec.begin(); iter != vec.end(); iter++)
        cout << *iter << " ";
}
<๊ฒฐ๊ณผ>

์•„๋ฌด ๊ฐ’๋„ ์•„์ง ๋„ฃ์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ์•„๋ฌด๊ฒƒ๋„ ๋‚˜์˜ค์ง€ ์•Š๋Š”๋‹ค.

1. ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” 1

//vector<int> vec;
vector<int> vec = { -1,-1,-1 };

๋ฐฐ์—ด๊ณผ ๊ฐ™์ด ์›์†Œ๋ฅผ ์ง์ ‘ ํ• ๋‹นํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

2. ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” 2

//vector<int> vec = { -1,-1,-1 };
vector<int> vec(3);

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟ”๋ณด์ž.

<๊ฒฐ๊ณผ>
0 0 0

ํฌ๊ธฐ 3์งœ๋ฆฌ intํ˜• vector๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋ฉฐ 0์œผ๋กœ ์ž๋™ ์ดˆ๊ธฐํ™” ๋œ๋‹ค.

//vector<int> vec(3);
vector<int> vec(3,-1);

๋˜ ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟ”๋ณด์ž.

<๊ฒฐ๊ณผ>
-1 -1 -1

ํฌ๊ธฐ 3์งœ๋ฆฌ intํ˜• vector๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋ฉฐ -1๋กœ ์ž๋™ ์ดˆ๊ธฐํ™” ๋œ๋‹ค.

3. fill ํ•จ์ˆ˜ ์ด์šฉ

vector<int> vec(3);
fill(vec.begin(),vec.end(),-1);
<๊ฒฐ๊ณผ>
-1 -1 -1

4. assign ํ•จ์ˆ˜ ์ด์šฉ

vector<int> vec;
    vec.assign(3,-1);

    for (int i = 0; i < 3; i++) {
        cout << vec[i] << " ";
    }
<๊ฒฐ๊ณผ>
-1 -1 -1

5. ๋ฐฐ์—ด๋กœ ์ดˆ๊ธฐํ™”

int main() {
    int arr[3];
    memset(arr, -1, sizeof(arr));
    vector<int> vec(arr, arr + sizeof(arr)/sizeof(int));

    vector<int>::iterator iter;
    for (iter = vec.begin(); iter != vec.end(); iter++)
        cout << *iter << " ";
}

์œ„์—์„œ ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ ํ›„, vector๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ ๋ฐฐ์—ด๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ sizeof ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ์ด๋‹ค.

memset ํ•จ์ˆ˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ byte ๋‹จ์œ„๋กœ ์ƒ๊ฐํ•˜์—ฌ arr์˜ size๋ฅผ ๊ทธ๋Œ€๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด ๋œ๋‹ค. ๋”ฐ๋ผ์„œ 3๊ฐœ์˜ int๊ฐ€ ์žˆ๋Š” arr ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” 3*4byte = 12byte๊ฐ€ ๋˜์–ด ํ•ด๋‹น ๋ฉ”๋ชจ๋ฆฌ์— -1๊ฐ’์ด ํ• ๋‹น๋  ๊ฒƒ์ด๋‹ค.

๋ฐ˜๋ฉด vector์˜ ์ƒ์„ฑ์ž์— ๋„˜๊ฒจ์ฃผ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์‹œ์ž‘ ์ฃผ์†Œ์™€ ๋งˆ์ง€๋ง‰ ์ฃผ์†Œ์ด๋‹ค. ๋งˆ์ง€๋ง‰ ์ฃผ์†Œ๋ฅผ (์‹œ์ž‘ ์ฃผ์†Œ์— ๋ฐฐ์—ด์˜ ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ๋”ํ•œ ๊ฐ’)์œผ๋กœ ์ƒ๊ฐํ•œ๋‹ค๋ฉด ์š”์†Œ์˜ ๊ฐฏ์ˆ˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋‹จ์œ„๊ฐ€ ์•„๋‹ˆ์—ฌ์„œ ๊ณ„์‚ฐ์„ ํ†ตํ•ด ๊ตฌํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค. 12byte๋กœ ๋ฐ˜ํ™˜๋˜๋Š” sizeof(arr)์˜ ๊ฐ’์„ ํ•˜๋‚˜์˜ ์š”์†Œ๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ sizeof(int) ์ฆ‰, 4byte๋กœ ๋‚˜๋ˆ„์–ด์ฃผ๋ฉด ์š”์†Œ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

6. vector๋กœ ์ดˆ๊ธฐํ™”

์ด๋ฏธ ๋งŒ๋“ค์–ด์ ธ ์žˆ๋Š” vector์˜ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ๋Š” ์ƒ์„ฑ์ž์— ํ•ด๋‹น vector์˜ ์ด๋ฆ„๋งŒ ๋„˜๊ฒจ์ฃผ๋ฉด ๋œ๋‹ค.

int main() {
    int arr[3];
    memset(arr, -1, sizeof(arr));
    vector<int> vec1(arr, arr + sizeof(arr)/sizeof(int)); // ์ด๋ฆ„ ๋ณ€๊ฒฝ
    vector<int> vec(vec1); // ์ถ”๊ฐ€

    vector<int>::iterator iter;
    for (iter = vec.begin(); iter != vec.end(); iter++)
        cout << *iter << " ";
}
<๊ฒฐ๊ณผ>
-1 -1 -1

 

๋‹ค์ฐจ์› vector

๊ธฐ์กด ์ฝ”๋“œ

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<vector<int>> vec(3, vector<int>(3));

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << vec[i][j] << " ";
        }
        cout << endl;
    }
}
<๊ฒฐ๊ณผ>
0 0 0
0 0 0
0 0 0

1. ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™”

//vector<vector<int>> vec(3, vector<int>(3));
vector<vector<int>> vec(3, vector<int>(3,-1));
<๊ฒฐ๊ณผ>
-1 -1 -1
-1 -1 -1
-1 -1 -1

2. fill ํ•จ์ˆ˜

int main() {
    vector<vector<int>> vec(3, vector<int>(3));
    vector<vector<int>>::iterator iter;
    for (iter = vec.begin(); iter!= vec.end(); iter++){
        fill(iter->begin(), iter->end(), -1);
    }
}
<๊ฒฐ๊ณผ>
-1 -1 -1
-1 -1 -1
-1 -1 -1

<๋ฐ”๋‘‘์ด๋‹˜์˜ ๋Œ“๊ธ€๋กœ 2021-03-27 ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค>

 

vector<vector<int>> vec(3, vector<int>(3));
fill(vec.begin(), vec.end(), vector<int>(3, -1));
<๊ฒฐ๊ณผ>
-1 -1 -1
-1 -1 -1
-1 -1 -1

fill ํ•จ์ˆ˜๋Š” ์ฑ„์šธ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒํ˜• (๋งค๊ฐœ๋ณ€์ˆ˜ 3๋ฒˆ์งธ)์ด ๊ทธ๋ƒฅ const Type ํ˜•์œผ๋กœ ์ž๋ฃŒํ˜•์ด ์ƒ๊ด€์—†๊ธฐ ๋•Œ๋ฌธ์— vector์ž์ฒด๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ์ œ๋Œ€๋กœ ์ดˆ๊ธฐํ™” ๋œ๋‹ค.

๋ฐ˜์‘ํ˜•