【发布时间】:2020-05-08 18:35:09
【问题描述】:
对于我的班级,我需要找到以下结构的用法:vector<map<int, map<char, float>>>
我的想法是创建一种基本的加密方法,其输出取决于使用日期(全年的每一天都有自己的字符编码)。 vector 的外部 map 代表月份,而这些映射的 int 键代表日期。
该结构使用file,其中每一行都列出了ABC,并且每个字母都属于一个用分号分隔的数字。
我的代码有效,但仅在每个月的第一天提供加密,无论它收到什么日期。 << 运算符工作正常。
class CodeCollection{
vector<map<int, map<char, float>>> coding_of_the_day;
public:
CodeCollection(string path){
ifstream fin(path);
if(!fin.is_open()){
cerr << "Nope" << endl;
exit(432);
}
for(size_t j = 0; j < month_lenght.size(); ++j){
int day = 1;
map<int, map<char, float>> month;
for(int i = 0; i < month_lenght[j]; ++i){
map<char, float> char_value;
char c, tmp;
float f;
string abc_coding;
while(getline(fin, abc_coding)){
stringstream ss;
ss << abc_coding;
int counter = 1;
while(ss.good()){
ss >> c >> tmp >> f >> tmp;
char_value.emplace(c, f);
cout << c << f << " " << day << counter << endl;
counter++;
}
month.emplace(day, char_value);
day++;
}
}
coding_of_the_day.push_back(month);
}
fin.close();
}
friend ostream& operator<<(ostream& out, map<int, map<char, float>>& monthly){
for(auto const& first_key : monthly){
out << first_key.first;
for(auto const& second_key : first_key.second){
out << second_key.first << " = " << second_key.second << ", ";
}
out << endl;
}
return out;
}
ofstream codegenerator(string message, int month, int day){
ofstream fout("code.txt");
///I suppose my flaw is here:
map<int, map<char, float>>::iterator it = coding_of_the_day[month-1].find(day);
for(size_t i = 0; i < message.size(); ++i){
map<char, float>::iterator ite = it->second.find(message[i]);
fout << ite->second << ".";
}
return fout;
}
};
提前感谢您提供任何解决方案。
【问题讨论】:
-
你想在这个庞大的结构中找到什么?
-
如果您想快速查找这些内容,为什么不使用
map?作为说明,这种“加密”方法似乎非常脆弱,所以我希望这只是出于学术目的。 -
我建议在取消引用之前检查
find的返回值。 -
从 C++ 的角度来看,您可能希望将加密实现为发出可读输出流的可写流。
-
@JohnFilleau 我想找到定义需要使用哪个
map编码的给定日期
标签: c++ dictionary vector iterator find