Array "visited" method?

Uhm, can someone show me an example of how an array is made for visited locations so we don't have to visit them again? like for example if i have 1 2 3 4 5 and i visited 1 and 2 and i wanna start at 3 , but doing that using an array that has the visited locations etc..
1
2
3
4
5
bool visited_location[5]; // create an array for keeping track of visiting some other array
for (int i = 0; i < 5; ++i)
{
  visited_location[i] = false; // set all to false to begin
}


Each time you visit some element in whatever array you care about, set the corresponding element in visited_location to true.

When you want to know if you've visited already, look at the corresponding element in visited_location

Also, don't use arrays. Use std::vector or std::array.

Last edited on
@Moschops:

When you want to know if you've visited already, look at the corresponding element in visited_location


can you demonstrate this one? i didn't quite get that part
using std::pair<int, bool>:
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
#include <iostream>
#include <vector>
#include <tuple>
#include <iterator>
#include <utility>

constexpr auto SIZE = 5;

int main()
{
  std::vector<std::pair<int, bool>> myVec{};

  for (size_t i = 0; i < SIZE; ++i)
  {
      std::cout << "Enter number " << i + 1 << " of " << SIZE << "\n";
      int temp{};
      std::cin >> temp;
      myVec.push_back(std::move(std::make_pair(temp, false)));
  }
  for (auto itr = myVec.begin(); itr != myVec.end(); ++itr)
  {
      if(std::distance(myVec.begin(), itr)%2)
      {
          ++(*itr).first;//increment all odd numbered cells
          (*itr).second = true;//has been visited
      }
  }
  for (auto& elem : myVec)std::cout << elem.first << " " << elem.second << "\n";
}
Last edited on
Topic archived. No new replies allowed.