我试图在C ++ 11中定义一个元组向量,如下所示:
#include <iostream>
#include <string>
#include <vector>
#include <tuple>
typedef unsigned char uchar;
typedef std::tuple<uchar, std::string, uchar, float> fruitInfoTuple;
const std::vector<fruitInfoTuple> jointsInfo{
{ 0, "mango", 100, -6.01},
{10, "apple", 144, 6.25},
{12, "orange", 159, 2.59},
{33, "banana", 144, -28.96},
{ 4, "grapes", 128, 3.79},
};
我在启用C ++ 11标志的情况下编译程序。但是,它显示出并发症错误,如下所示:
ravi@lab:~/Desktop/a$ g++ -std=c++11 learn.cpp
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[6], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
};
^
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[6], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[7], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[7], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[7], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
我猜GCC 4.8不支持元组功能。请问有什么解决方法吗?请注意,如有需要,我可以使用boost
。我只想要一种干净的方法来定义元组,就像上面那样。
答案 0 :(得分:4)
您可以尝试:
const std::vector<fruitInfoTuple> jointsInfo{
fruitInfoTuple{ 0, "mango", 100, -6.01},
fruitInfoTuple{10, "apple", 144, 6.25},
fruitInfoTuple{12, "orange", 159, 2.59},
fruitInfoTuple{33, "banana", 144, -28.96},
fruitInfoTuple{ 4, "grapes", 128, 3.79},
};
答案 1 :(得分:3)
在C ++ 11中,您应该使用std::make_tuple
来构造元组:
#include <iostream>
#include <string>
#include <vector>
#include <tuple>
typedef unsigned char uchar;
typedef std::tuple<uchar, std::string, uchar, float> fruitInfoTuple;
const std::vector<fruitInfoTuple> jointsInfo{
std::make_tuple( 0, "mango", 100, -6.01),
std::make_tuple(10, "apple", 144, 6.25),
std::make_tuple(12, "orange", 159, 2.59),
std::make_tuple(33, "banana", 144, -28.96),
std::make_tuple( 4, "grapes", 128, 3.79),
};
int main()
{
for(int i = 0 ; i < jointsInfo.size(); ++i)
{
std::cout << std::get<1>(jointsInfo[i]) << std::endl;
}
}
结果:
mango
apple
orange
banana
grapes