Я пытаюсь перебрать хэш-карту, которую я создал, запрашивая вывод пользователя. Я не могу напечатать второе значение хэш-карты, которое представляет собой список чисел с плавающей запятой. Должен ли я делать вложенный цикл for? И если да, то каковы правила кода для этого?
/* 14. En una clase de 5 alumnos se han realizado tres exámenes
y se requiere determinar el número de:
a) Alumnos que aprobaron todos los exámenes.
b) Alumnos que aprobaron al menos un exámen.
c) Alumnos que aprobaron únicamente el último exámen.
Realice un programa que permita la lectura de datos y
el cálculo de estadísticas
*/
#include <iostream>
#include <string>
#include <unordered_map>
#include <list>
using namespace std;
int main(){
// Crear un mapa que guarde los valores
// de los estudiantes en la siguiente forma:
// "estudiante": [nota1, nota2, nota3]
unordered_map<string, list<float>> boletin;
string nombre;
float nota_1, nota_2, nota_3;
for (int i = 1; i < 3; i ++){
cout <<"Digite nombre del estudiante: " << i << endl;
cin >> nombre;
cout <<"Digite las tres calificaciones: " << endl;
cin >> nota_1 >> nota_2 >> nota_3;
boletin.insert({nombre, {nota_1, nota_2, nota_3}});
}
// iterando e imprimiendo los elementos del
// hashmap
for (auto i=boletin.begin(); i!= boletin.end(); i++){
cout << i -> first << endl;
cout << i -> second << endl;
}
}
return 0;
}





Да, вам нужно использовать вложенный цикл. Синтаксис кода должен быть таким:
for (auto i=boletin.begin(); i!= boletin.end(); i++) {
cout << i -> first << endl;
list<float> mylist = i->second;
for (auto it=mylist.begin(); it != mylist.end(); ++it) {
std::cout << ' ' << *it;
}
}
Спасибо, действительно полезно!
Как следует из сообщения об ошибке, для шаблона
<<нет перегруженного оператора выводаstd::list. Вы должны перебрать список самостоятельно. Здесь циклforна основе диапазона должен быть очень полезен (или для спискаstd::copyсstd::ostream_iterator).