#include <iostream>
#include <string>
#include <map>
int main()
{
std::map<int, std::string> id2str{ {1, "hello"},
{3, "Structured"}, {5, "bindings"} };
for (const auto& elem : id2str) {
std::cout << "id=" << elem.first
<< ", str=" << elem.second << std::endl;
}
std::cout << "结构化绑定" << std::endl;
//结构化绑定,需要支持c++17以上的编译器
for (const auto& [id, str] : id2str) {
std::cout << "id=" << id << ",str = " << str << std::endl;
}
system("pause");
}
当操作map,多元素类时,使用结构化绑定比较方便,记录一下。