How to manage <map> iterators from an external iterator

Hi everyone,

I have <map> container where I associate a string (a word) with an object of the class NodoIndice (NodoIndice.h).
I'm able to insert all data with no problem (with the function insertar(string palabra, int unaLinea) in ListaIndice.h).

But now I have to define the following functions (of ListaIndice.h):

1)string leerPalabra(); that return the word that is pointed by the iterator iterator_lista
2)void iniciar(); that restart the iterator iter_lista
3)void avanzar(); that make the advance the iterator of 1 unit
4)bool estaDentro(); that verifies if the iterator iter_lista is still with the limit of <map>

Basically, as I understand it, I have to build some sort of external iterator that goes through the <map> managing the <map> proper iterator iter_linea.

I tried anything but since <map> iterators don't even let the use of the "+" operator I remained with no idea to how to proceed.

Below is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//NodoIndice
#include <set>
#include <iostream>
#include <string>
using namespace std;
#ifndef NODOINDICE_H
#define NODOINDICE_H

typedef set<int, less < int>> TListaLineas;

class NodoIndice {

private:
    TListaLineas listaLineas;

public:
    NodoIndice(int unaLinea);
    ~NodoIndice();
};
#endif


//NodoIndice.cpp
#include <iostream>
#include <sstream>
#include"NodoIndice.h"
using namespace std;

//Implementación de métodos de la clase NodoIndice
NodoIndice::NodoIndice(int unaLinea){
    listaLineas.insert(unaLinea);
    }

NodoIndice::~NodoIndice(){

    cout << "Se ejecuta el DESTRUCTOR" << endl;
};

void NodoIndice::insertaLinea(int unaLinea){
    listaLineas.insert(unaLinea);
    }



//ListaIndice.h
#include<iostream>
using namespace std;
#include <map>
#include "NodoIndice.h"
#ifndef LISTAINDICE_H
#define LISTAINDICE_H

typedef map <string, NodoIndice*, less<string> > TListaOrd;


class ListaIndice{
private:
  TListaOrd laLista;
  TListaOrd::iterator iter_lista;


public:

    void insertar (string palabra, int unaLinea);
    string leerPalabra();
    void iniciar();
    void avanzar();
    bool estaDentro();


};

#endif



//ListaIndice.cpp
#include <map>
#include <utility>
#include "ListaIndice.h"
#include "NodoIndice.h"


void ListaIndice::insertar(string palabra, int unaLinea){
    NodoIndice *nodo= new NodoIndice(unaLinea);
    laLista.insert(TListaOrd::value_type(palabra,nodo));
    /*for (iter_lista = laLista.begin(); iter_lista!= laLista.end(); iter_lista++){
        cout << iter_lista->first << endl;
        cout << iter_lista->second->recuperaLineas() << endl;

    }
    cout << endl;*/
}

string ListaIndice::leerPalabra(){
return iter_lista->first;
}


void ListaIndice::iniciar(){
    iter_lista=laLista.begin();
    }

void ListaIndice::avanzar(){
    iter_lista++;
    }

bool ListaIndice::estaDentro(){
   // TListaOrd::iterator iter_lista_test;
    if (iter_lista != laLista.end()){
        return true;
        }
    return false;
}



//main
#include <iostream>
#include "indice.h"

#include <map>
using namespace std;



int main()
{
    PresentarMenu();
    cout << endl;

    ListaIndice miIndice;


    miIndice.insertar("rem", 3);
    miIndice.insertar("gioven", 4);
    miIndice.insertar("ger", 4);




   cout << miIndice.leerPalabra() << endl;

return 0;
}


Thank you in advance for your support.


mlm
the string leerPalabra(); method is ill-defined. it doesn't state on what basis the map kep (std::string) is to be viewed. if you just want to view the strings in the map you don't need a separate method for it, you can just use iterator or range-loop through the map and view the first element. so a separate method needs to add something extra to the program

since <map> iterators don't even let the use of the "+" operator

look up std::advance(), std::next()
Topic archived. No new replies allowed.