【发布时间】:2020-02-06 20:11:07
【问题描述】:
这是Cout from a map with std::tuple的后续问题
我制作了一张小地图,我称之为BMW。它包含键Usage和Diesel,如下所示。
#include <iostream>
#include <bits/stdc++.h>
#include <map>
#include <vector>
using namespace std;
int main()
{
// initialize container
map<string, tuple<string, string>> BMW;
// insert elements
BMW.insert({"Usage", {"1", "2"}});
BMW.insert({"Disel", {"2", "3"}});
string sFirst_value;
string sSecond_value;
//prints out the map
for (const auto& x : BMW) {
sFirst_value.assign(get<0>(BMW.find(x.first)->second));
sSecond_value.assign(get<1>(BMW.find(x.first)->second));
cout << x.first << "\n" << "Min: " << sFirst_value << "\n" << "Max: " << sSecond_value << "\n" << "\n";
}
return 0;
}
有没有我可以从字符串中调用地图名称BMW 而不是写BMW.insert({"Usage", {"1", "2"}});?像这样:
stirng Mycar;
Mycar.insert({"Usage", {"1", "2"}});
【问题讨论】:
-
否,但您可以将其全部存储在
map<string, map<string, tuple<string, string>>>中。 -
如何搜索该地图?你有一个小例子(我是 C++ 的新手)吗?
-
无关,但如果你是 C++ 新手,你应该看看 Why should I not include bits/stdc++.h? 和 Why is “using namespace std;” considered bad practice?。只是为您指明未来项目的正确方向;)
标签: c++ dictionary