ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++ ์‘์šฉ

Structured Bindings์˜ ๋ชจ๋“  ๊ฒƒ

์„œ์•„๋ž‘๐Ÿ˜ 2024. 8. 27. 09:34

 

 

Structured Bindings๋Š” C++17์—์„œ ๋„์ž…๋œ ๊ธฐ๋Šฅ์œผ๋กœ, ๋ณตํ•ฉ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ(์˜ˆ: std::tuple, std::pair, ๋ฐฐ์—ด, ๊ตฌ์กฐ์ฒด ๋“ฑ)์—์„œ ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋™์‹œ์— ์ถ”์ถœํ•˜๊ณ , ๊ฐ๊ฐ์„ ๋ณ„๋„์˜ ๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉํ•  ์ˆ˜ ์žˆ๋Š” ํŽธ๋ฆฌํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํŠนํžˆ ํŠœํ”Œ์ด๋‚˜ ์‚ฌ์šฉ์ž ์ •์˜ ๊ตฌ์กฐ์ฒด์—์„œ ์œ ์šฉํ•˜๋ฉฐ, ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ํŽธ์˜์„ฑ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

1. ๊ธฐ๋ณธ ๊ฐœ๋…

Structured Bindings๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๋ณตํ•ฉ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์—์„œ ๊ฐ ์š”์†Œ๋ฅผ ์‰ฝ๊ฒŒ ๋ถ„ํ•ดํ•˜์—ฌ ๊ฐ๊ฐ์˜ ๋ณ€์ˆ˜์— ๋‹ด์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” auto ํ‚ค์›Œ๋“œ์™€ ์œ ์‚ฌํ•œ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„๋ฉ๋‹ˆ๋‹ค.

  • ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•:
auto [var1, var2, var3] = someTuple;

์—ฌ๊ธฐ์„œ someTuple์€ ๋ณตํ•ฉ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ด๊ณ , [var1, var2, var3]๋Š” ๊ฐ๊ฐ์˜ ์š”์†Œ์— ๋ฐ”์ธ๋”ฉ๋  ๋ณ€์ˆ˜๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

2. ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

Structured Bindings๋Š” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์˜ˆ์‹œ๋กœ std::tuple, std::pair, ๋ฐฐ์—ด, ๊ตฌ์กฐ์ฒด ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ 1: std::tuple๊ณผ std::pair์—์„œ์˜ ์‚ฌ์šฉ

#include <iostream>
#include <tuple>
#include <utility> // for std::pair

int main() {
    std::tuple<int, double, std::string> data = {1, 2.5, "Hello"};

    // Structured Bindings๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠœํ”Œ์˜ ์š”์†Œ๋ฅผ ๊ฐ๊ฐ ๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉ
    auto [id, value, text] = data;

    std::cout << "id: " << id << ", value: " << value << ", text: " << text << std::endl;

    // std::pair์—์„œ์˜ ์‚ฌ์šฉ
    std::pair<int, std::string> p = {42, "Answer"};
    auto [number, answer] = p;

    std::cout << "number: " << number << ", answer: " << answer << std::endl;

    return 0;
}

์ถœ๋ ฅ:

id: 1, value: 2.5, text: Hello
number: 42, answer: Answer

์„ค๋ช…:

  • std::tuple์ด๋‚˜ std::pair์˜ ์š”์†Œ๋ฅผ ๊ฐ๊ฐ id, value, text์™€ ๊ฐ™์€ ๊ฐœ๋ณ„ ๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ด๋ฅผ ํ†ตํ•ด ํŠœํ”Œ์ด๋‚˜ ํŽ˜์–ด์—์„œ ํŠน์ • ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๊ณผ์ •์„ ๊ฐ„์†Œํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ 2: ๋ฐฐ์—ด์—์„œ์˜ ์‚ฌ์šฉ

#include <iostream>

int main() {
    int myArray[3] = {10, 20, 30};

    // ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ๊ฐ๊ฐ์˜ ๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉ
    auto [a, b, c] = myArray;

    std::cout << "a: " << a << ", b: " << b << ", c: " << c << std::endl;

    return 0;
}

์ถœ๋ ฅ:

a: 10, b: 20, c: 30

์„ค๋ช…:

  • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๊ฐœ๋ณ„ ๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํŠน์ • ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๋Š” ๋Œ€์‹ , ๋ช…์‹œ์ ์ธ ์ด๋ฆ„์„ ํ†ตํ•ด ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ 3: ์‚ฌ์šฉ์ž ์ •์˜ ๊ตฌ์กฐ์ฒด์—์„œ์˜ ์‚ฌ์šฉ

#include <iostream>
#include <string>

struct Person {
    std::string name;
    int age;
    double height;
};

int main() {
    Person person = {"John Doe", 30, 5.9};

    // ๊ตฌ์กฐ์ฒด์˜ ๋ฉค๋ฒ„๋ฅผ ๊ฐ๊ฐ์˜ ๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉ
    auto [name, age, height] = person;

    std::cout << "Name: " << name << ", Age: " << age << ", Height: " << height << std::endl;

    return 0;
}

์ถœ๋ ฅ:

Name: John Doe, Age: 30, Height: 5.9

์„ค๋ช…:

  • ์‚ฌ์šฉ์ž ์ •์˜ ๊ตฌ์กฐ์ฒด์˜ ๋ฉค๋ฒ„๋“ค์„ ๊ฐ๊ฐ์˜ ๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉํ•˜์—ฌ ๋” ๋ช…ํ™•ํ•˜๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


3. ์ฃผ์˜ ์‚ฌํ•ญ ๋ฐ ์ œํ•œ ์‚ฌํ•ญ

  1. ๋ฐฐ์—ด ํฌ๊ธฐ: ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•  ๋•Œ, ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ์ •ํ™•ํžˆ ๋งž์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํฌ๊ธฐ๊ฐ€ 3์ธ ๋ฐฐ์—ด์—์„œ 3๊ฐœ์˜ ๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋” ์ ๊ฑฐ๋‚˜ ๋งŽ์€ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  2. ๋ณ€์ˆ˜ ํƒ€์ž…: ๋ฐ”์ธ๋”ฉ๋œ ๋ณ€์ˆ˜๋“ค์€ ์›๋ž˜ ์š”์†Œ๋“ค์˜ ํƒ€์ž…์„ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, auto ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํƒ€์ž… ์ถ”๋ก ์ด ์ž๋™์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.
  3. ์บก์ฒ˜๋œ ๋ฉค๋ฒ„: ์‚ฌ์šฉ์ž ์ •์˜ ๊ตฌ์กฐ์ฒด์—์„œ ์ƒ์ˆ˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์บก์ฒ˜ํ•  ๊ฒฝ์šฐ, ๋ฐ”์ธ๋”ฉ๋œ ๋ณ€์ˆ˜๋„ ์ƒ์ˆ˜๋กœ ์ทจ๊ธ‰๋ฉ๋‹ˆ๋‹ค.
  4. ์Šค์ฝ”ํ”„: Structured Bindings์—์„œ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋“ค์€ ํ•ด๋‹นํ•˜๋Š” ์ฝ”๋“œ ๋ธ”๋ก ๋‚ด์—์„œ๋งŒ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ๋ณ€์ˆ˜์˜ ์Šค์ฝ”ํ”„๊ฐ€ ๋ช…ํ™•ํžˆ ์ œํ•œ๋˜์–ด ๋ถˆํ•„์š”ํ•œ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


4. ์žฅ์  ๋ฐ ํ™œ์šฉ

์žฅ์ :

  • ์ฝ”๋“œ ๊ฐ„๊ฒฐํ™”: ๋ณตํ•ฉ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์—์„œ ํŠน์ • ์š”์†Œ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ณผ์ •์ด ๋งค์šฐ ๊ฐ„๋‹จํ•ด์ง‘๋‹ˆ๋‹ค.
  • ๊ฐ€๋…์„ฑ ํ–ฅ์ƒ: ๋ณ€์ˆ˜์— ๋ช…์‹œ์ ์ธ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜์—ฌ ์ฝ”๋“œ ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์œ ์—ฐ์„ฑ: ๋‹ค์–‘ํ•œ ๋ณตํ•ฉ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์—์„œ ์ผ๊ด€๋œ ๋ฐฉ์‹์œผ๋กœ ์š”์†Œ๋ฅผ ๋ถ„ํ•ดํ•  ์ˆ˜ ์žˆ์–ด, ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค.

ํ™œ์šฉ:

  • ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์˜ ์ฒ˜๋ฆฌ: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์กฐํšŒ ๊ฒฐ๊ณผ๋‚˜ JSON ํŒŒ์‹ฑ ๋“ฑ์—์„œ ๋ณต์žกํ•œ ๊ตฌ์กฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฒ”์œ„ ๊ธฐ๋ฐ˜ ๋ฃจํ”„์™€ ๊ฒฐํ•ฉ: Structured Bindings๋Š” ๋ฒ”์œ„ ๊ธฐ๋ฐ˜ for ๋ฃจํ”„์™€ ๊ฒฐํ•ฉํ•˜์—ฌ, ๋” ๊ฐ„๊ฒฐํ•˜๊ณ  ๋ช…ํ™•ํ•œ ๋ฐ˜๋ณต๋ฌธ์„ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ค๋‹ˆ๋‹ค.
#include <vector>
#include <tuple>
#include <iostream>

int main() {
    std::vector<std::tuple<int, std::string>> vec = {
        {1, "one"}, {2, "two"}, {3, "three"}
    };

    for (const auto& [num, word] : vec) {
        std::cout << "Number: " << num << ", Word: " << word << std::endl;
    }

    return 0;
}

์ถœ๋ ฅ:

Number: 1, Word: one
Number: 2, Word: two
Number: 3, Word: three

์ด ์˜ˆ์ œ์—์„œ ๋ณด๋“ฏ, Structured Bindings์™€ ๋ฒ”์œ„ ๊ธฐ๋ฐ˜ for ๋ฃจํ”„๋ฅผ ๊ฒฐํ•ฉํ•˜๋ฉด ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ๋ฐ˜๋ณต ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.