|
-
November 6th, 2015, 12:21 PM
#1
Need Help: Tuple - element access
Hi guys,
In my ongoing quest to learn (slowly) C++, I've now come across Tuples.
There is something about accessing the elements using std::get which I can't wrap my head around.
Here's my test code:
Code:
#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
int funcInitialize ();
int funcPrintData ();
typedef tuple <string, string, int> tupData;
vector <tupData> tupEmployees;
int main()
{
funcInitialize ();
funcPrintData ();
return 0;
}
// Functions //
int funcInitialize ()
{
tupEmployees.push_back(tupData("Smith", "John", 22));
tupEmployees.push_back(tupData("Jones", "Jack", 24));
tupEmployees.push_back(tupData("Smith", "Jane", 23));
return 0;
}
int funcPrintData ()
{
// Print all elements //
for (unsigned int row = 0; row < tupEmployees.size(); row++)
{
cout << get<0>(tupEmployees[row]) << ", ";
cout << get<1>(tupEmployees[row]) << " ";
cout << get<2>(tupEmployees[row]) << endl;
}
cout << endl << endl;
// Print single element //
int row = 1;
const int col = 0;
cout << get<col>(tupEmployees[row]) << " " << endl;
cout << endl << endl;
return 0;
}
Result:
Code:
Smith, John 22
Jones, Jack 24
Smith, Jane 23
Jones
Now, while the result is as expected, I can't figure out how to get around the limitation of the template parameter (in my example the const int col) having to be a compile-time const value. Is there a work-around for this using pointers or references, so that the value in/of col can be incremented? All my attempts have failed (not surprising!).
As always, any help would be appreciated. Thanks in advance.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|