Skip to content

Instantly share code, notes, and snippets.

@TheEyesightDim
Last active July 1, 2018 08:23
Show Gist options
  • Save TheEyesightDim/5522e82d449a48893347 to your computer and use it in GitHub Desktop.
Save TheEyesightDim/5522e82d449a48893347 to your computer and use it in GitHub Desktop.
Example of working with tuples to return multiple values; Example of using tuples in place of structs
#include <tuple>
#include <iostream>
using std::tuple;
using std::tie;
using std::make_tuple;
using std::cout;
class rectangle
{
int width, height;
public:
rectangle(int _width, int _height) : width(_width), height(_height) {}
//tuple acts as a return type for this function, just as any single-unit return type would.
//make_tuple() provides a means of returning the expected tuple.
tuple<int, int> get_dimensions() {return make_tuple(width, height);}
};
int main()
{
rectangle r(3,4);
int w,h;
//binds the tuple's values to w and h simultaneously with tie()
tie(w,h) = r.get_dimensions();
cout << w << ' ' << h << '\n';
return 0;
}
@TheEyesightDim
Copy link
Author

TheEyesightDim commented Jan 9, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment