Skip to content

Instantly share code, notes, and snippets.

@ola-sk
Last active October 4, 2019 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ola-sk/f5d87fe97a2fc50ea4bcb5f707b0de34 to your computer and use it in GitHub Desktop.
Save ola-sk/f5d87fe97a2fc50ea4bcb5f707b0de34 to your computer and use it in GitHub Desktop.
Structured bindings for unpacking bundled return values
//Practice using structured bindings on custom structures.
#include <iostream>
#include <vector>
//Define & initialize data structures
struct employee {
unsigned int id;
std::string name;
std::string role;
unsigned salary;
} e1, e2, e3;
std::vector<employee> employees {
e1, e2, e3/* Initialized from somewhere */
};
std::vector<std::string> init_names_of_employees (const std::vector<employee> employees) {
std::vector<std::string> names_of_employees;
for (const auto &[id, name, role, salary] : employees) {
// If we don't use all of the binded variables, compiler will optimize it out and will not bind values to unused variables.
names_of_employees.push_back(name);
}
return names_of_employees;
}
const std::vector<std::string> names_of_employees = init_names_of_employees(::employees);
void print_names (std::vector<std::string> names_of_employees) {
for (const auto &name : names_of_employees) {
std::cout << name << std::endl;
}
}
//
//
//
//
int main() {
//notice, we access global scope variable specifically
init_names_of_employees (::employees);
print_names(::names_of_employees);
}
#include <map>
std::map<std::string, size_t> animal_population {
{"humans", 700000000000},
{"chickens", 5802482509235849},
{"camels", 97675678},
"sheep", 742035748955},
/* ... */
};
for (const auto &[species, count] : animal_population) {
std::cout << "There are " << count << " " << species
<< "on this planet.\n";
//function prototype:
std::pair<int, int> divide_remainder (int dividend, int divisor);
//accessing the individual values of the result of a function with STRUCTURED BINDING
auto [fraction, remainder] = divide_remainder(16, 3);
std::cout << "16 / 3 is "
<< fraction << " with a remainder of "
<< remainder << '/n';
// old way of accessing values of an std::pair
const auto result (divide_remainder(16, 3));
std::cout << "16 / 3 is "
<< result.first << " with a remainder of "
<< result.second << '/n';
//function prototype:
std::tuple<std::string, std::chrono::system_clock::time point, unsigned>
stock_info(const std::string &name);
//assigning using structured binding
const auto [name, valid_time, price = stock_info("INTC");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment