How to create a list of tuples C++

后端 未结 4 807
南方客
南方客 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 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 
    #include 
    #include 
    
    std::list > 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 
    #else
    #  include 
    #endif
    

    Then, above example will look like follows:

    #include 
    #include 
    #include "tuple.h"
    
    std::list > time;
    

    This should work on nearly all recent compilers.

提交回复
热议问题