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: |

With the sharp downturn in the crypto market, yelling has become a coping mechanism for many crypto traders. This screaming therapy became popular after the surge of Goblintown Ethereum NFTs at the end of May or early June. Here, holders made incoherent groaning sounds in late-night Twitter spaces. They also role-played as urine-loving Goblin creatures. As five out of seven counts were serious, Hui sentenced Ng to six years and six months in jail. The public channel had more than 109,000 subscribers, Judge Hui said. Ng had the power to remove or amend the messages in the channel, but he “allowed them to exist.” Just at this time, Bitcoin and the broader crypto market have dropped to new 2022 lows. The Bitcoin price has tanked 10 percent dropping to $20,000. On the other hand, the altcoin space is witnessing even more brutal correction. Bitcoin has dropped nearly 60 percent year-to-date and more than 70 percent since its all-time high in November 2021. The main design elements of your Telegram channel include a name, bio (brief description), and avatar. Your bio should be:
from us


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