gpt4 book ai didi

c++ - 结构化绑定(bind)在 C++17 中不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 02:05:33 24 4
gpt4 key购买 nike

我已经尝试了来自 Expert C++ Programming 的以下代码片段. g++ 给出编译错误。这仅仅是 g++ 没有 catch C++17 语法的情况吗?

lib_test.cpp:39:15: error: expected unqualified-id before ‘[’ token
auto [iter, success] = m.try_emplace(b.country, b, 1);
^

我使用 -std=c++17 标志。

g++ (Ubuntu 6.4.0-17ubuntu1) 6.4.0 20180424

#include <iostream>
#include <functional>
#include <list>
#include <map>

using namespace std;


struct billionaire {
string name;
double dollars;
string country;
};


void efficient_map_test()
{
list<billionaire> billionares {
{"Bill Gates", 86.0, "USA"},
{"Warren Buffet", 75.6, "USA"},
{"Jeff Bezos", 72.8, "USA"},
{"Amnancio Ortega", 71.3, "Spain"},
{"Mark Zuckerberg", 56.0, "USA"},
{"Carlos Slim", 54.5, "Mexico"},

{"Bernard Arnualt", 41.5, "France"},
{"Liliane Bettencourt", 39.5, "France"},
{"Wang Jianlin", 31.3, "China"},
{"Li Ka-shing", 31.2, "Hong Kong"}
};

map<string, pair<const billionaire, size_t>> m;

for (const auto &b: billionares) {
auto [iter, success] = m.try_emplace(b.country, b, 1);
if (!success) {
iter->second.second += 1;
}
}


for (const auto &[key, value]: m) {
const auto &[b, count ] = value;
cout << b.country << " : " << count
<< "billionaires. Richest is "
<< b.name << " with " << b.dollars
<< " B$n";
}
}

int main()
{
return 0;
}

编辑:1. -std=C++17 -> -std=c++17
2. 添加了空的 main 使其可以复制/粘贴

最佳答案

你只是把编译标志弄错了。这是一个小写字母 c:

g++ -std=c++17 -o main main.cpp

而不是像您在问题 (-std=C++17) 中所写的大写 C。哦,请添加一个空的 main 函数,以便您的代码可以复制粘贴。

关于c++ - 结构化绑定(bind)在 C++17 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51814688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com