Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing members of boost:: tuple

Tags:

c++

boost

I am trying to implement a vector like vector< boost::tuple<int,int,int> >day; I want to acess tuple's first element to check a condition. can someone please tell me how to do it? I am new to boost. Thanks in advance.

like image 646
Shweta Avatar asked Dec 30 '10 09:12

Shweta


2 Answers

#include <boost/tuple/tuple.hpp>
#include <iostream>
#include <vector>

int main()
{
    std::vector< boost::tuple<int, int, int> > v;
    v.push_back(boost::make_tuple(1, 2, 3));
    std::cout << boost::get<0>(v[0]) << std::endl;
    std::cout << boost::get<1>(v[0]) << std::endl;
    std::cout << boost::get<2>(v[0]) << std::endl;
}
like image 178
icecrime Avatar answered Oct 31 '22 14:10

icecrime


First tupple has a set of types:
Edit (Fixed your post) But using abstract type here to demonstrate how it works better.

std::vector<boost::tuple<A, B, C> >   day;

// Load data into day;

Now you can extract that parts of the tupple using the get method.

A&   aPart = day[0].get<0>();
B&   bPart = day[0].get<1>();
C&   cPart = day[0].get<2>();
like image 45
Martin York Avatar answered Oct 31 '22 13:10

Martin York