How to create a list of C ++ tuples

I am new to C ++, I actually have no background. I link to create a list of tuples, the first will be int, the second will be a string.

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

And get the error. I want to be able to create a list, add entries that I can sort by int, and have a line that describes what int is.

How do I create this list?

+5
source share
4 answers

For a simple list, use std::vectorinstead std::list.

You probably just need something simple, like this:

#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 - In fact, the implementation of a doubly linked list that you might not need.

+9
source

, .

++ 11, tuple, vector, :

#include <tuple>
#include <vector>
// ...
vector<tuple<int, string>> data;

, emplace_back vector. , :

#include <iostream>
// ...
int age;
string name;
while(cin >> age >> name) data.emplace_back(age, name);

sort, int case, int, string s:

#include <algorithm>
// ...
sort(data.begin(), data.end());

:

get<0>(data[i])

:

get<int>(data[i])

, live ideone.

+7

, , " " , Boost.Assign . - :

#include <boost/assign/list_of.hpp>
#include <vector>

int main()
{
    typedef boost::tuple<int, std::string> tuple;

    std::vector<tuple> v = boost::assign::tuple_list_of(1, "foo")(2, "bar");
}

.

+1

:

++ . gcc Visual Studio ( ) . , (.. , ), .

, std:: tr1, std. . , , . , , Visual Studio 10 / gcc, :

#include <list>
#include <string>
#include <tuple>

std::list<std::tuple<int, string> > time;

For example, with cmake, you can generate a header file that provides support for all compilers that support tuples (and with slightly greater efficiency even when using boost).

To do this, you will create something like a tuple.h.cmake file:

#if defined( __GNUC__ ) && (__GNUC__ * 100 + __GNUC_MINOR__ < 430)
# define GCC_OLDER_THAN_430 1
#endif

#if defined( _MSC_VER ) && (_MSC_VER < 1600 /* 2010 */)
# define MSC_OLDER_THAN_2010 1
#endif

#if defined( GCC_OLDER_THAN_430 )
# define TR1_IN_TR1_SUBDIRECTORY 1
#endif

#if defined( ZORBA_GCC_OLDER_THAN_430 ) || defined( ZORBA_MSC_OLDER_THAN_2010 )
# define TR1_NS_IS_STD_TR1 1
#endif

#ifdef TR1_NS_IS_STD_TR1
# define TR1_NS std::tr1
#else
# define TR1_NS std
#endif

#ifdef TR1_IN_TR1_SUBDIRECTORY
#  include <tr1/tuple>
#else
#  include <tuple>
#endif

Then the above example would look like this:

#include <string>
#include <list>
#include "tuple.h"

std::list<TR1_NS::tuple<int, std::string> > time;

This should work with almost all recent compilers.

0
source

All Articles