how could I access "it" outside the function?
I will also ask - how could I access "nbr" outside the function?
(still consider myself a noob and appreciate people being gentle, lol )
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;
}
Last edited: