Map within another Map

2

I'm trying to access a map that is contained in another map , but I get an error.

The code is as follows:

map<string, map<string, list<string>>>::iterator iter;
map<string, list<string>>::iterator iterdos;
map<string, list<string>> m_spm;
pair<string, map<string, list<string>> > p_aux;
pair<string, list<string>> p_auxdos,spm;
//cout<<"PALABRA CLAVE ENCONTRADA "<<palabra_clave_encontrada<<endl;
iter = palabra_paginas.find(palabra_clave_encontrada);

if(iter==palabra_paginas.end()){
  //Añadir un elemento nuevo
  //buscamos si esta el dominio
  p_aux.first=palabra_clave_encontrada;
  spm.first=dominio;
  spm.second.push_back(nodo);
  p_aux.second.insert(spm);
  palabra_paginas.insert(p_aux);
}
else{
  m_spm=palabra_paginas->second;
  iterdos=m_spm.find(dominio);
  if(iterdos==palabra_paginas->second.end()){
    p_auxdos.first=dominio;
    p_auxdos.second.push_back(nodo);
    palabra_paginas->second.insert(p_auxdos);
  }
  else{
    iter->second->second.push_back(nodo);
  }
}

When I try to access palabra_paginas->second I get an error, as in the following:

main.cpp: In function 'int main(int, char**)':
main.cpp:442:32 error: base operand of '->' has non-pointer type 'std:map<std::__cxx11::basic_string<char>, std::map<std::__cxx11::basic_string<char>, std::_cxx11::list<std::__cxx11::basic_string<char>

    m_spm=palabra_paginas->second;
                         ^
main.cpp:444:38: error: base operand of '->' has non-pointer type 'std:map<std::__cxx11:::basic_string<char>, std::map<std::__cxx11::basic_string<char>, std::_cxx11::list<std::__cxx11::basic_string<char>

    if(iterdos==palabra_paginas->second.end()){
                               ^
main.cpp:450:25: error: base operand of '->' has non-pointer type std::map<std::__cxx11::basic_string<char>, std::__cxx11__::list<std::basic_string<char> > >

    iter->second.push_back(nodo);
    
asked by Javi Ruiz 06.06.2017 в 17:30
source

2 answers

2
map<string, map<string, list<string>>>::iterator iter;

iter = palabra_paginas.find(palabra_clave_encontrada);

We can then assume that palabra_paginas is declared such that:

map<string, map<string, list<string>>> palabra_paginas;

Well, with this in mind, the following instruction is incorrect:

palabra_paginas->second.insert(p_auxdos);

Why? Various reasons:

  • palabra_paginas is not a pointer, then it is not possible to use the operator -> with that object.
  • palabra_paginas does not have a member named second .

Perhaps what you want at this point is to act on the iterator, not on the map itself:

iter->second.insert(p_auxdos);

But it is complicated to confirm because the example that you expose is not exactly clear.

    
answered by 06.06.2017 / 17:48
source
2

This is a perfect example of how using aliases of types improves the readability of the code and reduces errors, let's start by giving names to your types:

using lista_texto = list<string>;
using mapa_listas = map<string, lista_texto>;
using gran_mapa_listas = map<string, mapa_listas>;

So, your first lines would look like this:

gran_mapa_listas::iterator iter;
mapa_listas::iterator iterdos;
mapa_listas m_spm;

gran_mapa_listas::value_type p_aux;
mapa_listas::value_type p_auxdos,spm;

Which is slightly more readable. I do not know what the type of palabra_paginas is because you do not show your statement, but from what I gather from the code you have published is a map (you use std::map::find on it).

If it is a map, it will not have the arrow operator ( -> ) overloaded, so any line you write palabra_paginas->... will not make sense.

From what I see, you use it as if it were a map iterator, because you write palabra_paginas->second at several points but it is not a pair or an iterator so it will necessarily fail you.

Surely you must change palabra_paginas for one of the iterators that you declare at the beginning, review the code.

    
answered by 06.06.2017 в 17:52