ShovelingLife
A Game Programmer
ShovelingLife
전체 방문자
오늘
어제
  • 분류 전체보기 (1079) N
    • 그래픽스 (57)
      • 공통 (19)
      • 수학 물리 (22)
      • OpenGL & Vulkan (1)
      • DirectX (14)
    • 게임엔진 (184)
      • Unreal (69)
      • Unity (104)
      • Cocos2D-X (3)
      • 개인 플젝 (8)
    • 코딩테스트 (221)
      • 공통 (7)
      • 프로그래머스 (22)
      • 백준 (162)
      • LeetCode (19)
      • HackerRank (2)
      • 코딩테스트 알고리즘 (8)
    • CS (237) N
      • 공통 (21)
      • 네트워크 (44)
      • OS & 하드웨어 (57) N
      • 자료구조 & 알고리즘 (98)
      • 디자인패턴 (6)
      • UML (4)
      • 데이터베이스 (7)
    • 프로그래밍 언어 (351) N
      • C++ (168)
      • C# (90)
      • Java (9)
      • Python (35) N
      • SQL (30)
      • JavaScript (8)
      • React (7)
    • 그 외 (20)
      • Math (5)
      • 일상 (5)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • Source Code 좌측 상단에 복사 버튼 추가 완료
  • 언리얼 엔진 C++ 빌드시간 단축 꿀팁
  • 게임 업계 코딩테스트 관련
  • 1인칭 시점으로 써내려가는 글들

인기 글

태그

  • 언리얼
  • 함수
  • 포인터
  • C
  • SQL
  • C++
  • string
  • 파이썬
  • 문자열
  • 유니티
  • 그래픽스
  • 배열
  • 티스토리챌린지
  • 클래스
  • 알고리즘
  • 오블완
  • c#
  • Unity
  • python
  • 백준

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ShovelingLife

A Game Programmer

프로그래밍 언어/C++

C++ std::tuple 여러가지 타입들의 객체를 보관 / Structured binding

2022. 10. 12. 15:50
#include <iostream>
#include <string>
#include <tuple>

int main() {
  std::tuple<int, double, std::string> tp;
  tp = std::make_tuple(1, 3.14, "hi");

  std::cout << std::get<0>(tp) << ", " << std::get<1>(tp) << ", "
            << std::get<2>(tp) << std::endl;
}

// 실행 결과
// 1, 3.14, hi

Structured binding c++ 17

#include <iostream>
#include <string>
#include <tuple>

std::tuple<int, std::string, bool> GetStudent(int id) {
  if (id == 0) {
    return std::make_tuple(30, "철수", true);
  } else {
    return std::make_tuple(28, "영희", false);
  }
}

int main() {
  auto student = GetStudent(1);

  int age = std::get<0>(student);
  std::string name = std::get<1>(student);
  bool is_male = std::get<2>(student);

  std::cout << "이름 : " << name << std::endl;
  std::cout << "나이 : " << age << std::endl;
  std::cout << "남자 ? " << std::boolalpha << is_male << std::endl;
}

// 실행 결과
// 이름 : 영희
// 나이 : 28
// 남자 ? false

모든 원소들이 포함 되어야한다.

auto& [age, name, is_male] = student;

각각 데이터형이 다른 세 개의 변수를 하나로 묶어서 할당 가능하다.

struct Data {
  int i;
  std::string s;
  bool b;
};

Data d;
auto [i, s, b] = d;

아래와 같이 범위 기반 for문으로도 사용 가능하다

std::map<int, std::string> m = {{3, "hi"}, {5, "hello"}};
for (auto& [key, val] : m) {
  std::cout << "Key : " << key << " value : " << val << std::endl;
}

출처 : C++ 기초 개념 17-5 : std::optional, variant, tuple (koreanfoodie.me)

저작자표시 (새창열림)

'프로그래밍 언어 > C++' 카테고리의 다른 글

C 난수 생성  (0) 2022.10.24
C++ 데이터 타입(data type)  (0) 2022.10.23
std::variant와 std::monostate 여러가지 타입 중 한 가지의 객체를 보관  (0) 2022.10.10
C extern (변수 / 함수 외부 선언)  (0) 2022.09.27
C++ auto 타입 추론  (0) 2022.09.21
    '프로그래밍 언어/C++' 카테고리의 다른 글
    • C 난수 생성
    • C++ 데이터 타입(data type)
    • std::variant와 std::monostate 여러가지 타입 중 한 가지의 객체를 보관
    • C extern (변수 / 함수 외부 선언)
    ShovelingLife
    ShovelingLife
    Main skill stack => Unity C# / Unreal C++ Studying Front / BackEnd, Java Python

    티스토리툴바