如何创建元组列表 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6482338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:13:03  来源:igfitidea点击:

How to create a list of tuples C++

c++listboosttuples

提问by Jim

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.

我对 C++ 相当陌生,我真的没有任何背景。我想创建一个元组列表,第一个是一个 int,第二个是一个字符串。

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

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.

并得到一个错误。我希望能够创建一个列表,添加可以按 int 排序的条目,并拥有描述 int 是什么的字符串。

How would I create this list?

我将如何创建此列表?

回答by Peter McG

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

对于简单的列表,请使用std::vector而不是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::listis actually an implementation of a doubly-linked list which you may not need.

std::list实际上是您可能不需要的双向链表的实现。

回答by arekolek

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

这里的答案有点过时,并没有告诉您如何对列表进行排序。

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

C++11 开始,您可以使用标准tuplevector例如:

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

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

要添加条目,您可以使用 的emplace_back方法vector。这是从标准输入读取的示例:

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

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

要排序,使用标准sort函数就足够了,因为int在我们的例子中是元组的第一个元素,默认排序顺序将int首先按s排序元素,然后按strings:

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

You can retrieve values from a tupleby 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.

我整理了一个完整的例子,你可以在 ideone 上看到

回答by Juho

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:

在这里可能不相关,但如果“创建部分”包含用元素填充列表,则 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");
}

Depending on your scenario ofcourse.

当然,这取决于您的情况。

回答by Markus Pilman

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.

新的 C++ 标准引入了可变参数模板以及元组。gcc 和 Visual Studio(至少)支持这些。因此,如果您有可能(即,如果所有受支持的编译器都支持元组,这已经很可能了),您可以使用它。

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:

唯一的问题是,一些编译器在 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 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).

例如,使用 cmake,您可以生成一个头文件,它使您支持所有支持元组的编译器(并且稍微多一些工作,甚至使用 boost 作为后备)。

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

为此,您将创建类似 tuple.h.cmake 文件的内容:

#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.

这应该适用于几乎所有最近的编译器。