Last active
July 1, 2018 08:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example lifted from http://blog.paphus.com/blog/2012/07/25/tuple-and-tie/