-1

Consider the following code snippet, taken for a program that works fine.

As you can see, It uses 2 nested for loops to iterate through a vector of vectors:

#define VERTEXES 10

vector<int> adjacency_list[VERTEXES];

for ( int index = 0; index < VERTEXES; index++ ){
    cout << "List[" << index << "]: ";
    for (auto element : adjacency_list[index])
        cout << element << ", ";
    cout << ".\n";
}

Could the first for be replaced by a ranged for too, in order to make the code simpler? If so, how?

If it makes the code more complex I'm not interested, thank you. But, in this case, an explanation of why it cannot make the code simpler is welcome!

  • 1
    Well, may I ask why you're using a raw c-style array of vectors here? That's not very idiomatic c++ code. And yes, for that specific case you can use a range based for loop, since there's a definition for `std::begin(adjacency_list)` and `std::end(adjacency_list)`. – πάντα ῥεῖ Oct 24 '22 at 21:31
  • Making it "simpler" is entirely a matter of opinion. The direct answer to your question, however is yes, it can, ideally by const-reference. Frankly, I'm surprised you thought it easier to ask here rather than just *trying* it instead. – WhozCraig Oct 24 '22 at 21:32
  • @πάνταῥεῖ No specific reason. I took the program as a whole and was improving and adapting it to better adhere to C++, but got stuck on the nested ranged `for` before reaching the point of questioning myself about how the `vector`s were originally declared. – BsAxUbx5KoQDEpCAqSffwGy554PSah Oct 24 '22 at 21:35
  • 1
    Did you actually mean to write `vector adjacency_list(VERTEXES);` instead of creating an array of `vector`s?? – πάντα ῥεῖ Oct 24 '22 at 21:37
  • @WhozCraig I would try it if I knew what it is and how to try it. – BsAxUbx5KoQDEpCAqSffwGy554PSah Oct 24 '22 at 21:41
  • @πάνταῥεῖ The person who wrote that line wasn't me. I just didn't touch it yet. – BsAxUbx5KoQDEpCAqSffwGy554PSah Oct 24 '22 at 21:42
  • 1
    @BsAxUbx5KoQDEpCAqSffwGy554PSah well, then the information is finally not sufficient here. The code as is doesn't compile, and it's left completely unclear, what it is supposed to do. Can you [edit], and fix that in your question please. – πάντα ῥεῖ Oct 24 '22 at 21:49
  • 1
    If you need to output the index, then a range-based loop (which hides that information away by default) is not the tool for the job. – Peter Oct 25 '22 at 00:07

1 Answers1

2

Could the first for be replaced by a ranged for too, in order to make the code simpler? If so, how?

Yes

for (auto& edge : adjacency_list){
    // cout << "List[" << index << "]: ";
    for (auto element : edge)
        cout << element << ", ";
    cout << ".\n";
}

Does it make the code simpler? That's up to you to decide, seeing as you are now missing the index of each edge. IMO, it is simpler/less code, therefore less thinking to comprehend.

smac89
  • 39,374
  • 15
  • 132
  • 179
  • ERROR on the first line when compiling: "error: no matching function for call to 'begin(std::vector*&)'". The first line is "for (auto& edge : adjacency_list)". – BsAxUbx5KoQDEpCAqSffwGy554PSah Oct 24 '22 at 21:39
  • @BsAxUbx5KoQDEpCAqSffwGy554PSah Sounds like your code doesn't look like what you've shown us. Are you e.g. passing `adjacency_list` as argument to function? – Yksisarvinen Oct 24 '22 at 21:44
  • 1
    @BsAxUbx5KoQDEpCAqSffwGy554PSah Well, then `adjacency_list` is not an array anymore. It [decayed to pointer](https://stackoverflow.com/q/1461432/7976805) and lost all info about size. That means you can't use range-based loop on it. Index-based or iterator (pointer in this case) based loop is your only option. – Yksisarvinen Oct 24 '22 at 21:56
  • @Yksisarvinen Upgrading the array into a global variable would solve the problem without pointers. Wouldn't this be an option too? – BsAxUbx5KoQDEpCAqSffwGy554PSah Oct 24 '22 at 22:08
  • 1
    @BsAxUbx5KoQDEpCAqSffwGy554PSah Most people consider making a global variable a downgrade rather than upgrade. It is an option, but global variables have quite a lot of problems in long term. – Yksisarvinen Oct 24 '22 at 22:12