我的程序不会编译,因为它找不到操作数的匹配项。 它访问struct Student中的地图,我不确定这是否是访问地图的确切方法。
我的程序不会编译,因为它找不到操作数的匹配项。 它访问struct Student中的地图,我不确定这是否是访问地图的确切方法。
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <string>
#include <map>
#include <list>
using namespace std;
struct Student {
string id;
map<string, int> scores;
};
istream& operator >>(istream &is, Sudent& g) {
auto it = g.scores.begin();
is >> g.id >> it->first >> it.second;
return is;
}
在>> it->first
我收到此错误:
Error: no operator ">>" matches these operands
operand types are: std::basic_istream<char, std::char_traits<char>> >> const std::string
答案 0 :(得分:6)
您可以使用临时变量
std::string tempStr;
int tempInt;
is >> g.id >> tempStr >> tempInt;
scores.insert( std::pair<std::string,int>(tempStr , tempInt));
答案 1 :(得分:4)
错误是由于it->first
类型为const string
,而非string
。
除此之外,您还需要通过读取(未知)数量的字符串和相应的int来找到读取该映射的方法。如何做到这一点取决于它们在文件中的存储方式。