Skip to content

4. 배열


1. 배열 초기화

#include <iostream>
using namespace std;

int main() {
    int data1[3] = {0, 1, 2};
    int data2[2][2] = {
        {
            0,
        },
    };
    int data3[2][2];

    cout << "== data1 ==" << endl;
    for (int i = 0; i < 3; i++)
        cout << "data1[" << i << "] = " << data1[i] << endl;

    cout << "== data2 ==" << endl;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++)
            cout << "data2[" << i << "][" << j << "] = "
                 << data2[i][j] << endl;
    }

    cout << "== data3 ==" << endl;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++)
            cout << "data3[" << i << "][" << j << "] = "
                 << data3[i][j] << endl;
    }

    return 0;
}
== data1 ==
data1[0] = 0
data1[1] = 1
data1[2] = 2
== data2 ==
data2[0][0] = 0
data2[0][1] = 0
data2[1][0] = 0
data2[1][1] = 0
== data3 ==
data3[0][0] = 49152112
data3[0][1] = 1
data3[1][0] = 45412224
data3[1][1] = 1


2. 1차원 배열

#include <iostream>
using namespace std;

int main() {
    int data1[3] = {0, 1, 2};

    data1[0] += 10;
    data1[1] = 100;
    data1[2] -= 2;

    cout << "== data1 ==" << endl;
    for (int i = 0; i < 3; i++)
        cout << "data1[" << i << "] = " << data1[i] << endl;

    return 0;
}
== data1 ==
data1[0] = 10
data1[1] = 100
data1[2] = 0


3. 1차원 배열 함수 인자

#include <iostream>
using namespace std;

void Print1(int *);
void Print2(int[]);

int main() {
    int data[3] = {0, 1, 2};

    Print1(data);
    Print2(data);

    cout << "== 결과 ==" << endl;
    cout << data[0] << ", " << data[1] << ", " << data[2] << endl;

    return 0;
}

void Print1(int *arr) {
    cout << "== Print1 ==" << endl;
    cout << arr[0] << ", " << arr[1] << ", " << arr[2] << endl;

    arr[1] = 1000;
}

void Print2(int arr[]) {
    cout << "== Print2 ==" << endl;
    cout << arr[0] << ", " << arr[1] << ", " << arr[2] << endl;

    arr[2] = 2000;
}
== Print1 ==
0, 1, 2
== Print2 ==
0, 1000, 2
== 결과 ==
0, 1000, 2000


4. 2차원 배열

#include <iostream>
using namespace std;

int main() {
    int data1[2][2] = {1, 2, 3};
    int data2[2][3] = {
        {
            1,
        },
    };

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++)
            cout << "data1[" << i << "][" << j << "] = "
                 << data1[i][j] << endl;
    }

    cout << endl;

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++)
            cout << "data2[" << i << "][" << j << "] = "
                 << data2[i][j] << endl;
    }

    return 0;
}
data1[0][0] = 1
data1[0][1] = 2
data1[1][0] = 3
data1[1][1] = 0

data2[0][0] = 1
data2[0][1] = 0
data2[0][2] = 0
data2[1][0] = 0
data2[1][1] = 0
data2[1][2] = 0


5. 2차원 배열 함수 인자

#include <iostream>
using namespace std;

void Func1(int[][2]);
void Func2(int[][2], int);
void Func3(int *, int, int);

int main() {
    int data[2][2] = {{1, 2}, {3, 4}};

    int col = sizeof(data[0]) / sizeof(int);
    int row = sizeof(data) / sizeof(data[0]);

    Func1(data);
    Func2(data, col);
    Func3(*data, row, col);

    cout << "== 결과 ==" << endl;
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++)
            cout << data[i][j] << endl;
    }

    return 0;
}

void Func1(int arr[2][2]) {
    arr[0][0] = 1000;
}

void Func2(int arr[][2], int row) {
    arr[row - 2][1] = 2000;
}

void Func3(int *arr, int row, int col) {
    *((arr + row - 1) + col - 1) = 3000;
}
== 결과 ==
1000
2000
3000
4


6. 배열 일부 변경

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int data1[10]{
        0,
    };
    fill(data1, data1 + 3, 10);
    fill(data1 + 4, data1 + 8, 20);

    cout << "== data1 결과 (fill()) ==" << endl;
    for (int i = 0; i < 10; i++)
        cout << data1[i] << ", ";
    cout << endl;

    vector<int> data2 = vector<int>({0, 1, 2, 3, 4, 5, 6, 7});
    fill(data2.begin(), data2.begin() + 3, 30);

    cout << "== data2 결과 (fill()) ==" << endl;
    for (int i = 0, size = data2.size(); i < size; i++)
        cout << data2.at(i) << ", ";
    cout << endl
         << endl;

    int data3[10]{
        0,
    };
    fill_n(data3, 2, 10);
    fill_n(data3 + 4, 3, 20);

    cout << "== data3 결과 (fill_n()) ==" << endl;
    for (int i = 0; i < 10; i++)
        cout << data3[i] << ", ";
    cout << endl;

    vector<int> data4 = vector<int>({0, 1, 2, 3, 4, 5, 6, 7});
    fill_n(data4.begin(), data4.size() - 1, 30);
    fill_n(data4.begin(), 4, 40);

    cout << "== data4 결과 (fill_n()) ==" << endl;
    for (int i = 0, size = data4.size(); i < size; i++)
        cout << data4.at(i) << ", ";
    cout << endl;

    return 0;
}
== data1 결과 (fill()) ==
10, 10, 10, 0, 20, 20, 20, 20, 0, 0,
== data2 결과 (fill()) ==
30, 30, 30, 3, 4, 5, 6, 7,

== data3 결과 (fill_n()) ==
10, 10, 0, 0, 20, 20, 20, 0, 0, 0,
== data4 결과 (fill_n()) ==
40, 40, 40, 40, 30, 30, 30, 7,