Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建元组列表C++; 我对C++很陌生,我在这方面没有任何背景。我想创建一个元组列表,第一个是int,第二个是字符串 #include <string> #include <list> #include <boost/tuple/tuple.hpp> .... list< tuple<int,string> > time; #包括 #包括 #包括 .... 列表时间;_C++_List_Boost_Tuples - Fatal编程技术网

如何创建元组列表C++; 我对C++很陌生,我在这方面没有任何背景。我想创建一个元组列表,第一个是int,第二个是字符串 #include <string> #include <list> #include <boost/tuple/tuple.hpp> .... list< tuple<int,string> > time; #包括 #包括 #包括 .... 列表时间;

如何创建元组列表C++; 我对C++很陌生,我在这方面没有任何背景。我想创建一个元组列表,第一个是int,第二个是字符串 #include <string> #include <list> #include <boost/tuple/tuple.hpp> .... list< tuple<int,string> > time; #包括 #包括 #包括 .... 列表时间;,c++,list,boost,tuples,C++,List,Boost,Tuples,并且得到了一个错误。我希望能够创建一个列表,添加可以按int排序的条目,并拥有描述int是什么的字符串 如何创建此列表?作为旁注:

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

如何创建此列表?

作为旁注:

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

唯一的问题是,一些编译器在std::tr1命名空间中仍然有元组,而其他编译器在std命名空间中已经有元组。有时候你需要包括,有时候。但是,您可以将构建系统配置为定义一些宏,以帮助您支持多种方案。例如,如果您只需要支持Visual Studio 10和/或全新的gcc版本,则可以执行以下操作:

#include <list>
#include <string>
#include <tuple>

std::list<std::tuple<int, string> > time;
#包括
#包括
#包括
std::列表时间;
例如,使用cmake,您可以生成一个头文件,它为所有支持元组的编译器提供支持(稍微多做一点工作,甚至可以使用boost作为后备)

为此,您需要创建类似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
#如果定义(uu GNUC_uuu)和&(u GNUC_uuu*100+u GNUC_uuu小调)<430)
#定义GCC_早于_430 1
#恩迪夫
#如果定义(_MSC_VER)和(_MSC_VER<1600/*2010*/)
#定义MSC_早于_2010 1
#恩迪夫
#如果已定义(GCC_早于_430)
#在\u TR1\u子目录1中定义TR1\u
#恩迪夫
#如果定义(ZORBA_GCC_OLDER_Thor_430)|定义(ZORBA_MSC_OLDER_Thor_2010)
#定义TR1\u NS\u为标准\u TR1
#恩迪夫
#如果定义TR1是标准TR1
#定义TR1\u NS标准::TR1
#否则
#定义TR1\u NS标准
#恩迪夫
#ifdef TR1_在_TR1_子目录中
#包括
#否则
#包括
#恩迪夫
然后,上面的示例将如下所示:

#include <string>
#include <list>
#include "tuple.h"

std::list<TR1_NS::tuple<int, std::string> > time;
#包括
#包括
#包括“tuple.h”
std::列表时间;

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

对于简单的列表,请使用
std::vector
而不是
std::list

您可能只是想要一些简单的东西,例如:

#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;
    }
}
#包括
#包括
#包括
#包括“boost/tuple/tuple.hpp”
使用名称空间std;
使用boost::tuple;
typedef向量tuple\u列表;
int main(int arg,char*argv[]{
元组列表tl;
tl.push_back(元组(21,“Jim”);
对于(元组列表::常量迭代器i=tl.begin();i!=tl.end();++i){

cout可能与此无关,但如果“创建部分”包含用元素填充列表,则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");
}
#包括
#包括
int main()
{
typedef boost::tuple tuple;
std::vector v=boost::assign::元组列表(1,“foo”)(2,“bar”);
}

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

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

由于您可以使用标准,因此可以使用
向量
,例如:

#include <tuple>
#include <vector>
// ...
vector<tuple<int, string>> data;
要排序,使用标准函数就足够了,因为在我们的例子中,
int
是元组的第一个元素,默认排序顺序将首先按
int
s,然后按
string
s对元素进行排序:

#include <algorithm>
// ...
sort(data.begin(), data.end());
#按索引包括:

get<0>(data[i])
get(数据[i])
或按类型:

get<int>(data[i])
get(数据[i])

我提出了一个.

愚蠢的问题:您是否记得添加using namespace std/using namespace boost?您收到的具体错误消息是什么?您的代码中是否有“using namespace boost;”字样?我想您需要一个“using namespace std;”还有。我想,你可以使用std::map来实现这个目的。它将容纳成对的
int字符串
,自动按
int
值排序。描述正确。你说得对,我没有使用名称空间boost。哈哈,我已经习惯了,谢谢!在这种情况下,有没有一种简单的方法可以按年龄对其排序?我必须自己写吗rt函数?
get<int>(data[i])