Skip to content

11. 컨테이너


1. pair

#include <iostream>
#include <string>
using namespace std;

int main() {
    pair<string, int> data = pair<string, int>("백제 근초고왕", 13);

    cout << data.first << " " << data.second << "대왕" << endl;

    return 0;
}
백제 근초고왕 13대왕


2. tuple 1

#include <iostream>
#include <string>
#include <tuple>
using namespace std;

int main() {
    typedef tuple<string, int, double> Data;

    Data data1 = Data("문자열", 10, 1.2);
    auto data2 = make_tuple("문자열", 10, 1.2);

    cout << get<0>(data1) << ", "
         << get<1>(data1) << ", "
         << get<2>(data1) << endl;

    return 0;
}
문자열, 10, 1.2


3. tuple 2

#include <iostream>
#include <string>
#include <tuple>
using namespace std;

int main() {
    typedef tuple<string, int, double> Data;

    Data data1 = Data("문자열", 10, 1.2);
    string my_str = "";
    int my_int = 0;

    tie(my_str, my_int, ignore) = data1;

    cout << my_str << ", " << my_int << endl;

    return 0;
}
문자열, 10


4. tuple 3

#include <iostream>
#include <string>
#include <tuple>
using namespace std;

int main() {
    tuple<int, double> data1(1, 2.1);
    tuple<double, string> data2(3.4, "문자열1");
    auto data3 = tuple_cat(data1, data2);

    cout << get<0>(data3) << ", " << get<1>(data3) << ", "
         << get<2>(data3) << ", " << get<3>(data3) << endl;

    return 0;
}
1, 2.1, 3.4, 문자열1


5. tuple 4

#include <iostream>
#include <string>
#include <tuple>
using namespace std;

int main() {
    tuple<int> data1(1);
    tuple<int, double> data2(1, 2.3);
    tuple<int, double, char> data3(1, 2.3, 'a');

    cout << sizeof(data1) << ", " << sizeof(data2)
         << ", " << sizeof(data3) << endl;

    return 0;
}
4, 16, 24


6. tuple 5

#include <iostream>
#include <string>
#include <tuple>
using namespace std;

int main() {
    tuple<int, string> data1(1, "def");
    tuple<int, string> data2(1, "abc");

    if (data1 > data2)
        cout << "data1 > data2" << endl;
    else
        cout << "data1 < data2" << endl;

    tuple<double, string> data3(1.0, "def");
    tuple<double, string, int> data4(2.0, "abc", 3);

    // if (data3 > data4)
    //     cout << "data3 > data4" << endl;
    // else
    //     cout << "data3 < data4" << endl;

    return 0;
}
data1 > data2


7. tuple 6

#include <iostream>
#include <tuple>
using namespace std;

struct TupleExam {
    TupleExam(tuple<int, string>) {
        cout << "Tuple로 인자 전달" << endl;
    }
    TupleExam(int n, string s) {
        cout << "piecewis_construct로 인자 전달" << endl;
    }
};

int main() {
    tuple<int, string> data(1, "str");

    pair<TupleExam, TupleExam> data1(data, data);
    pair<TupleExam, TupleExam> data2(piecewise_construct, data, data);
    pair<TupleExam, TupleExam> data3(make_tuple(2, "str"), make_tuple(2, "str"));
    pair<TupleExam, TupleExam> data4(piecewise_construct, make_tuple(2, "str"), make_tuple(2, "str"));

    return 0;
}
Tuple로 인자 전달
Tuple로 인자 전달
piecewis_construct로 인자 전달
piecewis_construct로 인자 전달
Tuple로 인자 전달
Tuple로 인자 전달
piecewis_construct로 인자 전달
piecewis_construct로 인자 전달


8. array

#include <array>
#include <iostream>
using namespace std;

void Print(const array<int, 3> &data) {
    cout << "== Print ==" << endl;
    for (auto iter = data.begin();
         iter != data.end(); ++iter) {
        cout << *iter << ' ';
    }
    cout << endl;
}

int main() {
    array<int, 3> data1{10, 2, 5};
    array<int, 3> data2;
    data2.fill(0);
    data2.at(1) = 200;

    Print(data2);

    copy(data1.begin(), data1.end(), data2.begin());

    Print(data1);
    Print(data2);

    return 0;
}
== Print ==
0 200 0
== Print ==
10 2 5
== Print ==
10 2 5


9. vector 1

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

int main() {
    vector<int> data;

    data.assign(5, 100);

    for (int i = 0; i < data.size(); i++)
        cout << "data " << i << ": " << data.at(i) << endl;

    data.front() = 1;
    data.back() = 2;

    if (data.empty() == true) {
        cout << "벡터가 비어 있습니다." << endl;
    } else {
        for (int i = 0; i < data.size(); i++)
            cout << "data " << i << ": " << data.at(i) << endl;
    }

    return 0;
}
data 0: 100
data 1: 100
data 2: 100
data 3: 100
data 4: 100
data 0: 1
data 1: 100
data 2: 100
data 3: 100
data 4: 2


10. vector 2

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

int main() {
    vector<int> data1({1, 2, 3});
    vector<int> data2 = {10, 20, 40};

    data1.swap(data2);
    data1.insert(data1.begin() + 1, 20);

    cout << "== data1 ==" << endl;
    for (int i = 0; i < data1.size(); i++)
        cout << "data1 (" << i << "): " << data1.at(i) << endl;

    cout << endl
         << "== data2 ==" << endl;
    for (int i = 0; i < data2.size(); i++)
        cout << "data2 (" << i << "): " << data2.at(i) << endl;

    data2.erase(data2.begin(), data2.begin() + 2);
    data2.push_back(100);
    data2.push_back(200);
    data2.push_back(300);
    data2.pop_back();
    data2.insert(data2.end(), data1.begin(), data1.end());

    cout << endl
         << "== data2 ==" << endl;
    for (int i = 0; i < data2.size(); i++)
        cout << "data2 (" << i << "): " << data2.at(i) << endl;

    return 0;
}
== data1 ==
data1 (0): 10
data1 (1): 20
data1 (2): 20
data1 (3): 40

== data2 ==
data2 (0): 1
data2 (1): 2
data2 (2): 3

== data2 ==
data2 (0): 3
data2 (1): 100
data2 (2): 200
data2 (3): 10
data2 (4): 20
data2 (5): 20
data2 (6): 40


11. 컨테이너와 배열 복사

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

int main() {
    vector<int> number1({1, 2, 3, 4, 5});
    vector<int> number2;

    copy(number1.begin(), number1.end(), back_inserter(number2));

    cout << "== number2 ==" << endl;
    for (auto i : number2)
        cout << i << endl;

    double number3[5] = {1.2, 3.4, 4.5, 5.6, 6.7};
    double number4[5];

    copy(begin(number3), end(number3), begin(number4));

    cout << "== number4 ==" << endl;
    for (auto i : number4)
        cout << i << endl;

    return 0;
}
== number2 ==
1
2
3
4
5
== number4 ==
1.2
3.4
4.5
5.6
6.7


12. iterator

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

int main() {
    vector<int> data({1, 2, 3});

    for (vector<int>::iterator iter = data.begin();
         iter != data.end(); ++iter) {
        cout << *iter << ' ';
    }
    cout << endl;

    for (auto iter = data.cbegin();
         iter != data.cend(); ++iter) {
        cout << *iter << ' ';
    }
    cout << endl;

    for (auto iter = data.rbegin();
         iter != data.rend(); ++iter) {
        cout << *iter << ' ';
    }
    cout << endl;

    data.clear();

    return 0;
}
1 2 3
1 2 3
3 2 1


13. list 1

#include <iostream>
#include <list>
using namespace std;

void Print(const list<int> &data) {
    cout << "== Print ==" << endl;
    for (auto iter = data.begin();
         iter != data.end(); ++iter) {
        cout << *iter << ' ';
    }
    cout << endl;
}

int main() {
    list<int> data1({1, 2, 3, 2});
    data1.push_back(3);
    data1.sort();

    Print(data1);

    data1.unique();

    Print(data1);

    list<int> data2 = {4, 5, 6};
    data2.merge(data1);

    Print(data2);

    return 0;
}
== Print ==
1 2 2 3 3
== Print ==
1 2 3
== Print ==
1 2 3 4 5 6


14. list 2

#include <iostream>
#include <list>
using namespace std;

void Print(const list<int> &data) {
    cout << "== Print ==" << endl;
    for (auto iter = data.begin();
         iter != data.end(); ++iter) {
        cout << *iter << ' ';
    }
    cout << endl;
}

bool IsOdd(int arg) {
    return arg % 2 == 0 ? 0 : 1;
}

int main() {
    list<int> data1({1, 2, 4, 2, 7, 10, 13, 14});

    data1.remove(2);
    data1.remove_if(IsOdd);

    Print(data1);

    data1.reverse();

    Print(data1);

    return 0;
}
== Print ==
4 10 14
== Print ==
14 10 4


15. list 3

#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> data1;
    list<int> data2{3, 4, 5};

    data1.push_back(2);
    data1.push_back(7);

    data1.merge(data2);

    cout << "== List Merge ==" << endl;
    for (list<int>::iterator it = data1.begin();
         it != data1.end(); ++it) {
        cout << *it << ' ';
    }
    cout << endl;

    data2 = {3, 4, 5};
    data1.splice(data1.begin(), data2);

    cout << "== List Splice ==" << endl;
    for (auto it = data1.begin();
         it != data1.end(); ++it) {
        cout << *it << ' ';
    }
    cout << endl;
}
== List Merge ==
2 3 4 5 7
== List Splice ==
3 4 5 2 3 4 5 7


16. map 1

#include <iostream>
#include <map>
#include <string>
using namespace std;

void Print(const map<int, string> &data) {
    cout << "== Print ==" << endl;
    for (auto iter = data.begin();
         iter != data.end(); ++iter) {
        cout << iter->first << ": " << iter->second << ", ";
    }
    cout << endl;
}

int main() {
    map<int, string> data;
    data.insert({0, "첫 번째"});
    data.insert({1, "두 번째"});

    Print(data);

    auto ret = data.insert({1, "세 번째"});

    if (ret.second == true)
        cout << "== 데이터 추가 성공 ==" << endl;
    else
        cout << "== 데이터 추가 실패 ==" << endl;

    data.at(0) = "수정1";

    Print(data);

    return 0;
}
== Print ==
0: 첫 번째, 1: 두 번째,
== 데이터 추가 실패 ==
== Print ==
0: 수정1, 1: 두 번째,


17. map 2

#include <iostream>
#include <map>
#include <string>
using namespace std;

void Print(const map<int, string> &data) {
    cout << "== Print ==" << endl;
    for (auto iter = data.begin();
         iter != data.end(); ++iter) {
        cout << iter->first << ": " << iter->second << ", ";
    }
    cout << endl;
}

int main() {
    map<int, string> data;
    data.insert({5, "test1"});
    data.insert({3, "test2"});
    data.insert({11, "test3"});

    data[3] = "test 수정";

    auto result = data.find(11);

    if (result != data.end())
        cout << "== 검색 성공 ==" << endl
             << result->first << ", " << result->second << endl;
    else
        cout << "== 검색 실패 ==" << endl;

    data.erase(5);

    Print(data);

    return 0;
}
== 검색 성공 ==
11, test3
== Print ==
3: test 수정, 11: test3,


18. bitset 1

#include <bitset>
#include <iostream>
#include <string>
using namespace std;

int main() {
    bitset<8> data1(100);
    bitset<8> data2(0x78);
    bitset<8> data3(string("11110000"));

    cout << "data1: " << data1 << endl;
    cout << "data2: " << data2 << endl;
    cout << "data3: " << data3 << endl;

    data1.set();

    cout << endl
         << "== data1 ==" << endl;
    cout << "data1 all: " << data1.all() << endl;
    cout << "data1 any: " << data1.any() << endl;
    cout << "data1 none: " << data1.none() << endl;

    data2.reset();

    cout << endl
         << "== data2 ==" << endl;
    cout << "data2 all: " << data2.all() << endl;
    cout << "data2 any: " << data2.any() << endl;
    cout << "data2 none: " << data2.none() << endl;

    return 0;
}
data1: 01100100
data2: 01111000
data3: 11110000

== data1 ==
data1 all: 1
data1 any: 1
data1 none: 0

== data2 ==
data2 all: 0
data2 any: 0
data2 none: 1


19. bitset 2

#include <bitset>
#include <iostream>
#include <string>
using namespace std;

int main() {
    bitset<8> data1(1);
    bitset<8> data2(0x01);

    cout << "data1: " << data1 << endl;
    cout << "data2: " << data2 << endl;

    data1.set(0, false);
    data1.set(2, 1);

    cout << endl
         << "== data1 ==" << endl;
    cout << "data1: " << data1 << endl;

    data2[4] = false;
    data2.flip(5);

    cout << endl
         << "== data2 ==" << endl;
    cout << "data2: " << data2 << endl;

    string data1_str = data1.to_string();
    int data2_int = data2.to_ullong();

    cout << endl
         << "== 변환 ==" << endl;
    cout << "data1: " << data1_str << endl;
    cout << "data2: " << data2_int << endl;

    return 0;
}
data1: 00000001
data2: 00000001

== data1 ==
data1: 00000100

== data2 ==
data2: 00100001

== 변환 ==
data1: 00000100
data2: 33


20. bitset 3

#include <bitset>
#include <iostream>
using namespace std;

int main() {
    bitset<4> data1(6);     // 0110
    bitset<4> data2(0x09);  // 1001

    auto result = data1 | data2;
    cout << "data1 | data2 = " << result << endl;

    result = data1 & data2;
    cout << "data1 & data2 = " << result << endl;

    result = data1 ^ data2;
    cout << "data1 ^ data2 = " << result << endl;

    result = data1 << 1;
    cout << "data1 << 1 = " << result << endl;

    result = data1 >> 1;
    cout << "data1 >> 1 = " << result << endl;

    result = ~data1;
    cout << "~data1 = " << result << endl;

    return 0;
}
data1 | data2 = 1111
data1 & data2 = 0000
data1 ^ data2 = 1111
data1 << 1 = 1100
data1 >> 1 = 0011
~data1 = 1001