CPPPROGLIB Telegram 6005
🍿 How to: Structured Bindings: распаковка данных (C++17)

Элегантная работа с multiple return values и сложными структурами.

#include <map>
#include <tuple>
#include <array>

// Функция возвращает несколько значений
std::tuple<int, std::string, double> get_data() {
return {42, "hello", 3.14};
}

struct Point { int x, y; };

int main() {
// 1. Распаковка tuple
auto [id, name, price] = get_data();
std::cout << id << " " << name << " " << price << "\n";

// 2. Распаковка struct
Point p{10, 20};
auto [x, y] = p;
std::cout << "Point: " << x << ", " << y << "\n";

// 3. Распаковка array
std::array<int, 3> arr{1, 2, 3};
auto [first, second, third] = arr;

// 4. Распаковка map::insert результата
std::map<std::string, int> m;
auto [iterator, inserted] = m.insert({"key", 42});
if (inserted) {
std::cout << "Inserted: " << iterator->first << "\n";
}

// 5. Итерация по map с распаковкой
for (const auto& [key, value] : m) {
std::cout << key << " -> " << value << "\n";
}

// 6. Распаковка с модификаторами
auto& [rx, ry] = p; // Ссылки на члены
rx = 100; // Изменяем оригинал
}


💡 Применения:

Более читаемый код при работе с парами, tuple, структурами


✏️ А как вы распаковываете данные? Пишите в комментариях.

Библиотека C/C++ разработчика

#буст
Please open Telegram to view this post
VIEW IN TELEGRAM
10👍3



tgoop.com/cppproglib/6005
Create:
Last Update:

🍿 How to: Structured Bindings: распаковка данных (C++17)

Элегантная работа с multiple return values и сложными структурами.

#include <map>
#include <tuple>
#include <array>

// Функция возвращает несколько значений
std::tuple<int, std::string, double> get_data() {
return {42, "hello", 3.14};
}

struct Point { int x, y; };

int main() {
// 1. Распаковка tuple
auto [id, name, price] = get_data();
std::cout << id << " " << name << " " << price << "\n";

// 2. Распаковка struct
Point p{10, 20};
auto [x, y] = p;
std::cout << "Point: " << x << ", " << y << "\n";

// 3. Распаковка array
std::array<int, 3> arr{1, 2, 3};
auto [first, second, third] = arr;

// 4. Распаковка map::insert результата
std::map<std::string, int> m;
auto [iterator, inserted] = m.insert({"key", 42});
if (inserted) {
std::cout << "Inserted: " << iterator->first << "\n";
}

// 5. Итерация по map с распаковкой
for (const auto& [key, value] : m) {
std::cout << key << " -> " << value << "\n";
}

// 6. Распаковка с модификаторами
auto& [rx, ry] = p; // Ссылки на члены
rx = 100; // Изменяем оригинал
}


💡 Применения:

Более читаемый код при работе с парами, tuple, структурами


✏️ А как вы распаковываете данные? Пишите в комментариях.

Библиотека C/C++ разработчика

#буст

BY Библиотека C/C++ разработчика | cpp, boost, qt


Share with your friend now:
tgoop.com/cppproglib/6005

View MORE
Open in Telegram


Telegram News

Date: |

Telegram desktop app: In the upper left corner, click the Menu icon (the one with three lines). Select “New Channel” from the drop-down menu. Write your hashtags in the language of your target audience. Image: Telegram. Ng Man-ho, a 27-year-old computer technician, was convicted last month of seven counts of incitement charges after he made use of the 100,000-member Chinese-language channel that he runs and manages to post "seditious messages," which had been shut down since August 2020. Find your optimal posting schedule and stick to it. The peak posting times include 8 am, 6 pm, and 8 pm on social media. Try to publish serious stuff in the morning and leave less demanding content later in the day.
from us


Telegram Библиотека C/C++ разработчика | cpp, boost, qt
FROM American