如何创建元组的C清单++ [英] How to create a list of tuples C++
问题描述
我是相当新的C ++,我真的不以任何背景。我绑创建一个元组列表,首先会是int,第二个是一个字符串。
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;
和得到一个错误。我希望能够创建一个列表,补充一点,我可以通过INT排序项,并有一个描述字符串,什么是INT
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?
推荐答案
对于一个简单的列表,用的std ::矢量
而不是的std ::列表
。
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 ::列表
其实是一个双向链表,你可能不需要的实现。
std::list
is actually an implementation of a doubly-linked list which you may not need.
这篇关于如何创建元组的C清单++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!