我如何在mapc++中添加超过2个模板

How do i add more than 2 Template in a MAP c++?

本文关键字:2个 添加 mapc++      更新时间:2023-10-16

目前我正试图实现一个地图数据结构来解决我的问题,但我只能添加两个模板到一个地图。例如

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;