how do I access "it" outside the function?

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
how could I access "it" outside the function?

Code:
void iD()
        {
        vector<string> airports = {"ATL", "BOS", "DEN", "DFW", "EWR", "IAH", "JFK", "LAS", "LAX", "LGA", "MCO", "ORD"};
        unsigned long n = airports.size();
   
        for (i = 0; i < n; i++)
        {
            map <string, int> airportKeys;
            airportKeys [airports[i]] = i;
            map<string, int>::iterator it = airportKeys.begin();
            if (it != airportKeys.end())
            {
                cout << it-> first << " " << it-> second;         
            }
        }
    }

I will also ask - how could I access "nbr" outside the function?

Code:
#include <iostream>
#include <map>
#include <vector>
#include <list>

using namespace std;


class Graph
{
    int V;
    list<int> *l;             
 
public:
    Graph (int V)              
    {
        this->V = V;          
                              
        l = new list<int>[V]; 
    }
    void addEdge(int x, int y) 
    {
        l[x].push_back(y);    
        l[y].push_back(x);    
    }
 
    void printList()
    {
        for (int i = 0; i < V; i++)         
        {
            cout << "Vertex " << i << "->";  
         
            for (int nbr:l [i])              
            {                             
                cout << nbr << ",";          
            }                               
            cout << endl;
        }
    }
};

int main()
{
    Graph g(13);                                
    g.addEdge(1, 2);
    g.addEdge(2, 4);
    g.addEdge(2, 8);
    g.addEdge(3, 1);
    g.addEdge(4, 3);
    g.addEdge(4, 6);
    g.addEdge(5, 3);
    g.addEdge(6, 7);
    g.addEdge(7, 6);
    g.addEdge(8, 9);
    g.addEdge(9, 10);
    g.addEdge(10, 8);
    g.addEdge(10, 11);
 
    g.printList ();             
                                
    return 0;
}
(still consider myself a noob and appreciate people being gentle, lol )
 
Last edited:

BobTPH

Joined Jun 5, 2013
11,516
but "it" is both a string and an int, how do I declare that?

or should I say it first is a string and it second is an int
It is neither. It is an iterator for a map object that maps strings to integers.

Read about maps and iterators for maps in the C++ library documentation.
maps
 
Last edited:

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
It is neither. It is an iterator for a map object that maps strings to integers.
yes that's right, I got confused for a second... so how do I get around the error message "use of undeclared identifier" if I try to print the map values and the adjacency table from the other block of code together like here in line 5 -

Code:
void printList()
    {
        for (int i = 0; i < V; i++)       
        {
            cout <<  it-> first << " " << it-> second << i << "->";        //HERE
          
            for (int nbr:l [i])     
            {                   
                cout << nbr << ",";          
            }                
            cout << endl;
        }
    }
};
all I want is for the each airport name to be on the same line as the corresponding data from the adjacency table

do I need to create a variable and store those map values in it or something?
 
Top