我如何在mapc++中添加超过2个模板
How do i add more than 2 Template in a MAP c++?
目前我正试图实现一个地图数据结构来解决我的问题,但我只能添加两个模板到一个地图。例如
map<string, int> data;
如果我想添加两个以上,它将无法工作,我会得到各种内存和映射语义错误,如
Memory
1) type allocator_type aka int cannot be used prior to '::'
Map
1) rebind_alloc following 'template' keyword does not refer to a
template
2) Multiple overloads of 'map' instantiate to the same signature 'void(const)
下面是代码
void Teacher::modifyScore(string newName, int newEnglish, int newMath, int newBio) {
// Holds file data
map<string, int, int, int> data;
// Read file and fill data map
ifstream studentRec("StudentRecord.txt");
string line;
while (getline(studentRec, line))
{
string name;
int english;
int math;
int bio;
stringstream ss(line);
ss >> name >> english >> math >> bio;
data[name] = english;
data[name] = math;
data[name] = bio;
}
studentRec.close();
// Print data
for (auto& entry : data)
{
cout << entry.first << " " << entry.second << endl;
}
// Modify data
if(data[newEnglish] != 0) {
data[newName] = newEnglish;
}
if(data[newMath] != 0) {
data[newName] = newMath;
}
if(data[newBio] != 0) {
data[newName] = newBio;
}
// if(data[newChemical] != 0) {
// data[newName] = newChemical;
// }
// Open same file for output, overwrite existing data
ofstream ofs("StudentRecord.txt");
for (auto& entry : data)
{
ofs << entry.first << setw(10) << entry.second << setw(10) << entry.third << setw(10) << entry.fourth << endl;
}
ofs.close();
}
它只能在2个模板下工作。
编辑:如何流式传输每个数据?
for (const auto& entry : data)
{
tie(newEnglish, newBio, newMath) = entry.second;
of << entry.first << setw(10) << ?? << setw(10) << ?? << setw(10) << ?? << endl;
}
您需要使用的是一个元组http://de.cppreference.com/w/cpp/utility/tuple
std::map只能存储2个参数:keytype和datype,从概念上讲,你需要一个数据类型来存储多个数据类型。
你的地图会变成像
这样的东西std::map<std::string, std::tuple<int, int, int>> data;
关于元组的使用,请参考链接的文档
编辑:要添加新值,可以使用
int a, b, c;
...
data["Batman"] = std::make_tuple(a, b, c);
EDIT2:在while循环中,代码
data[name] = english;
data[name] = math;
data[name] = bio;
将成为
data[name] = std::make_tuple(english, math, bio);
EDIT3要访问元组数据,需要使用std::tie
int english, bio, math;
std::tie(english, bio, math) = data["batman"];
标引在map
上迭代的for循环中访问元组数据的最小示例#include <map>
#include <tuple>
int main()
{
std::map<int, std::tuple<int, int, int>> data;
for (const auto& entry : data)
{
int a, b, c;
std::tie(a, b, c) = entry.second;
}
return 0;
}
EDIT5std::tie函数将元组数据提取到提供的变量中。
代码tie(newEnglish, newBio, newMath) = entry.second;
of << entry.first << setw(10) << ?? << setw(10) << ?? << setw(10) << ?? << endl;
将成为
tie(newEnglish, newBio, newMath) = entry.second;
of << entry.first << setw(10) << newEnglish << setw(10) << newBio << setw(10) << newMath << endl;
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 在另一个类视图中添加最多2个图表的正确方法是什么
- 无法添加多个键以映射将结构作为键
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 如何在生成文件中添加多个标头 (HDR) 和对象?
- 将第二个 GATT 服务添加到 Movesense 容器
- oStream 不打印添加两个 valarray 的结果(使用运算符重载)
- 在 char* 数组中添加两个索引
- 静态堆栈函数不会 1) 输入第一个元素 2)添加新元素时识别旧元素
- 向指针地址添加 20 个字节偏移量
- 是否可以在 for 循环中添加两个浮点数?
- 野牛/flex:计算器将双精度值解释为整数,所以我添加了 YYSTYPE 双精度 #define 但我有多个编译错误
- OpenGL - 添加第二个着色器属性会导致空白屏幕
- 如何在Qt中合并/追加/添加两个用于线程的模型?
- 将两个数字添加为链表
- C++添加 2 个 DWORD 的算术溢出
- 链接列表C 类,这两个添加节点实现之间的差异是什么?
- 如何删除向量的多个添加指针