Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a list of tuples C++

I am fairly new to c++, I dont really have any background in it. I am tying to create a list of tuples, the first will be an int, the second will be a string.

  #include <string>
  #include <list>
  #include <boost/tuple/tuple.hpp>
  ....
  list< tuple<int,string> > time;

And getting an error. I want to be able to create a list, add entries that I can sort by the int, and have the string that describes, what the int was.

How would I create this list?

like image 763
Jim Avatar asked Jun 26 '11 05:06

Jim


1 Answers

For a simple list use std::vector instead of std::list.

You probably just want something simple such as:

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

using namespace std;
using boost::tuple;

typedef vector< tuple<int,string> > tuple_list;

int main(int arg, char* argv[]) {
    tuple_list tl;
    tl.push_back( tuple<int, string>(21,"Jim") );

    for (tuple_list::const_iterator i = tl.begin(); i != tl.end(); ++i) {
        cout << "Age: " << i->get<0>() << endl;
        cout << "Name: " << i->get<1>() << endl;
    }
}

std::list is actually an implementation of a doubly-linked list which you may not need.

like image 58
Peter McG Avatar answered Oct 19 '22 05:10

Peter McG