Shweta
Shweta

Reputation: 5446

accessing members of boost:: tuple

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.

Upvotes: 6

Views: 11810

Answers (2)

Loki Astari
Loki Astari

Reputation: 264361

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>();

Upvotes: 6

icecrime
icecrime

Reputation: 76745

#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;
}

Upvotes: 18

Related Questions