How to create a list of tuples C++

后端 未结 4 794
南方客
南方客 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 
    #include 
    #include 
    #include "boost/tuple/tuple.hpp"
    
    using namespace std;
    using boost::tuple;
    
    typedef vector< tuple > tuple_list;
    
    int main(int arg, char* argv[]) {
        tuple_list tl;
        tl.push_back( tuple(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.

提交回复
热议问题