auto keyword type is not recognized in my pc

Aug 18, 2020 at 12:14pm
with the given code, my compiler does give an error saying that x has no type defined.

1
2
3
4
5
6
7
8
9
10
11
 void printGraph(vector<int> adj[], int V)
{
    for (int v = 0; v < V; ++v)
    {
        cout << "\n Adjacency list of vertex "
             << v << "\n head ";
        for (auto x : adj[v])
           cout << "-> " << x;
        printf("\n");
    }
}
Aug 18, 2020 at 12:16pm
auto is a keyword that requires C++11 standard.
What compiler / IDE are you using?
Aug 18, 2020 at 12:53pm
What exactly are you trying to do with the range-based for loop at line 7? Range-based for loops work with entire containers, not one specific element. An element has no container begin() statement associated with it.

MAJOR brain-fart going on*. I didn't notice the OP passing a 2D container into the function. (see my follow-up reply)

*at least I caught my screw-up, eventually. *ouch*
Last edited on Aug 21, 2020 at 11:15pm
Aug 18, 2020 at 3:04pm
Change auto to int and you'll be fine.

I'm more intrigued by the fact that you are mixing cout and printf.
Aug 21, 2020 at 11:15pm
An array of vectors. Creating and passing a 2D vector might have been a better way to go, but what-the-hey. YMMV.

void printGraph(std::vector<std::vector<int>> adj)

One advantage to a 2D vector is it can be "ragged." Each row doesn't have to contain the same number of columnar elements:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>

void print(std::vector<std::vector<int>>);

int main()
{
   std::vector<std::vector<int>> vec { { 1, 2, 3 }, { 2, 4, 6, 8 }, { 3, 6 } };

   print(vec);
}

void print(std::vector<std::vector<int>> v)
{
   for (int r { }; r < v.size(); r++)
   {
      for (const auto& c : v[r])
      {
         std::cout << c << ' ';
      }
      std::cout << '\n';
   }
}
1 2 3
2 4 6 8
3 6
Topic archived. No new replies allowed.