How to create a list of tuples C++

后端 未结 4 791
南方客
南方客 2021-02-14 08:06

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.

          


        
相关标签:
4条回答
  • 2021-02-14 08:46

    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.

    0 讨论(0)
  • 2021-02-14 08:59

    The answers here are a bit outdated, and don't tell you how to sort the list.

    Since C++11 you can use the standard tuple, with a vector for example:

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

    To add entries you can use the emplace_back method of vector. Here's an example reading from the standard input:

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

    To sort, it is sufficient to use the standard sort function, since int is the first element of the tuple in our case, the default sorting order will sort the elements by ints first, then by strings:

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

    You can retrieve values from a tuple by index:

    get<0>(data[i])
    

    or by type:

    get<int>(data[i])
    

    I've put together a full example that you can see live at ideone.

    0 讨论(0)
  • 2021-02-14 09:04

    Just as a side note:

    The new C++ standard introduces variadic templates and with that also tuples. gcc and Visual Studio (at least) support these. So if it is possible for you (i.e. if all supported compilers support tuples which is already very likely) you could use this.

    The only problem is, that some compilers still have tuple in the std::tr1 namespace and others already have it in the std namespace. Also sometimes you need to include and sometimes . But you can configure your build system to define some macros which helps you to support several schemes. If you, for example only need to support Visual Studio 10 and/or a quite new gcc version you could do the following:

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

    For example with cmake you could generate a header file, which brings you support for all compilers, that support tuples (and with slightly more work even use boost as a fall back).

    To do this, you would 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, above example will look like follows:

    #include <string>
    #include <list>
    #include "tuple.h"
    
    std::list<TR1_NS::tuple<int, std::string> > time;
    

    This should work on nearly all recent compilers.

    0 讨论(0)
  • 2021-02-14 09:05

    Might not be relevant here, but if the "creation part" contains filling up the list with elements, Boost.Assign could be useful. You can do something like this:

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

    Depending on your scenario ofcourse.

    0 讨论(0)
提交回复
热议问题