C++ 在C+中定义元组常量向量的更简洁的方法+;11

C++ 在C+中定义元组常量向量的更简洁的方法+;11,c++,c++11,vector,tuples,stdtuple,C++,C++11,Vector,Tuples,Stdtuple,我有一个元组的常量向量,其中每个元组包含一个键、名称、数量和值。这就是我对它的定义- // tuple of key, name, quantity, value const std::vector<std::tuple<unsigned char, std::string, unsigned char, float> > myTable{ std::tuple<unsigned char, std::string, unsigned char, float

我有一个元组的常量向量,其中每个元组包含一个键、名称、数量和值。这就是我对它的定义-

// tuple of key, name, quantity, value
const std::vector<std::tuple<unsigned char, std::string, unsigned char, float> > myTable{
    std::tuple<unsigned char, std::string, unsigned char, float>(0, "mango", 12, 1.01f),
    std::tuple<unsigned char, std::string, unsigned char, float>(4, "apple", 101, 22.02f),
    std::tuple<unsigned char, std::string, unsigned char, float>(21, "orange", 179, 39.03f),
};
//键、名称、数量、值的元组
const std::vector myTable{
标准:元组(0,“芒果”,12,1.01f),
标准:元组(4,“苹果”,101,22.02f),
标准:元组(21,“橙色”,179,39.03f),
};
在main函数中,我需要每个元组的索引和要处理的所有值。为了简单起见,我使用以下方式打印它们-

for (int index = 0; index < myTable.size(); index++) {
    auto key = std::get<0>(myTable[index]);
    auto name = std::get<1>(myTable[index]);
    auto quantity = std::get<2>(myTable[index]);
    auto value = std::get<3>(myTable[index]);
    std::cout << " index: " << index
              << " key:" << (int)key
              << " name:" << name
              << " quantity:" << (int)quantity
              << " value:" << value
              << std::endl;
}
for(int index=0;indexstd::cout您可以使用
{}

const std::vector<std::tuple<unsigned char, std::string, unsigned char, float> > myTable{
    {0, "mango", 12, 1.01f},
    {4, "apple", 101, 22.02f},
    {21, "orange", 179, 39.03f},
};
const std::vector myTable{
{0,“芒果”,12,1.01f},
{4,“苹果”,101,22.02f},
{21,“橙色”,179,39.03f},
};

只需将上一个代码块中的
()
更改为
{}
。如果有一些推导或引用破坏了乐趣,也许添加
std::make_tuple
可以帮你摆脱困境。它就像一个符咒。请参见此处了解详细信息。我没有必要想太多!
const std::vector<std::tuple<unsigned char, std::string, unsigned char, float> > myTable{
    {0, "mango", 12, 1.01f},
    {4, "apple", 101, 22.02f},
    {21, "orange", 179, 39.03f},
};