Home > Blockchain >  How do I add a map with vector inside a map? C
How do I add a map with vector inside a map? C

Time:11-02

#include <iostream>
#include <map>
#include <vector>
using namespace std;

class Calificaciones{
    private:
        map<string, map<string, vector<float>>>notas;
        map<string, vector<float>>Map2;
        vector<float>V;
    public:
        void AgregarNota(string seccion, string matricula, float nota);
        void Cantidad_De_Notas();
        void alumnos_Mas_de_una_seccion();
};

void Calificaciones::Agregar_Nota(string seccion, string matricula, float nota){
    V.push_back(nota);
    Map2.insert(matricula, V);
    Notas.insert(seccion, Map2);
}

int main()
{
    cout<<"Hello World";

    return 0;
}

Agregar_nota should add all 3 attributes, first the note to a vector, then the vector to a map, and that map to another map.

I tried to use this inside Agregar_nota() but it does not work:

notas.insert(seccion,map<string, vector<float>>);
notas[seccion].insert(matricula,vector<float>);
notas[seccion][matricula].push_back(nota);

What should I do?

CodePudding user response:

Your code doesn't work because you are not calling map::insert() correctly. You need to wrap the input key/value inside of a std::pair.

Also, in your second code, you are not instantiating the vectors and maps you are trying to push. You are pushing types instead of objects.

Try this instead:

V.push_back(nota);
Map2.insert(make_pair(matricula, V));
Notas.insert(make_pair(seccion, Map2));

Or:

notas.insert(make_pair(seccion, map<string, vector<float>>{}));
notas[seccion].insert(make_pair(matricula, vector<float>{}));
notas[seccion][matricula].push_back(nota);

If you are using C 11 or later, you should use map::emplace() instead:

V.push_back(nota);
Map2.emplace(matricula, V);
Notas.emplace(seccion, Map2);

Or:

notas.emplace(seccion,map<string, vector<float>>{});
notas[seccion].emplace(matricula, vector<float>{});
notas[seccion][matricula].push_back(nota);

CodePudding user response:

I'm not sure you need the 3 data structures: notas, Map2, and V, since you seem to update V, then update Map2 with V, then update notas with Map2. It would only make sense (to me) if you were after some kind of optimization (e.g. let's keep all the notes handy in a vector V, instead of going and retrieving them from notas when we need them). If that's what you are pretending just let me know and I'll delete this answer as not useful. Otherwise, let's think you only want to keep the outer map, secciones, which contains a map of matriculas, which contains a vector of notes:

[Demo]

#include <fmt/ranges.h>
#include <map>
#include <vector>

struct Calificaciones {
    using nota_t = float;
    using matricula_name_t = std::string;
    using seccion_name_t = std::string;
    using notas_t = std::vector<nota_t>;
    using matriculas_t = std::map<matricula_name_t, notas_t>;
    using secciones_t = std::map<seccion_name_t, matriculas_t>;

    secciones_t secciones;

    void AgregarNota(const seccion_name_t& seccion,
        const matricula_name_t& matricula, float nota) {

        if (auto matriculas_it{ secciones.find(seccion) };
            matriculas_it != secciones.end()) {

            auto& matriculas{ matriculas_it->second };
            if (auto notas_it{ matriculas.find(matricula) };
                notas_it != matriculas.end()) {

                auto& notas{ notas_it->second };
                notas.emplace_back(nota);
            } else {
                matriculas.emplace(matricula, notas_t{ nota });
            }
        } else {
            auto matriculas = matriculas_t{};
            matriculas.emplace(matricula, notas_t{ nota });
            secciones.emplace(seccion, std::move(matriculas));
        }
    }
};

int main() {
    Calificaciones c{};
    c.AgregarNota("Programming I", "First grade", 1.0f);
    c.AgregarNota("Programming I", "First grade", 2.0f);
    c.AgregarNota("Programming II", "Second grade", 3.0f);
    c.AgregarNota("Logic", "First grade", 4.0f);
    fmt::print("{}", c.secciones);
}

// Outputs:
//
//   {
//       "Logic": {"First grade": [4]},
//       "Programming I": {"First grade": [1, 2]},
//       "Programming II": {"Second grade": [3]}
//   }
  • Related