repl.it
@anonymous/

SereneWearyApplet

C++

No description

fork
loading
Files
  • main.cpp
main.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <utility>

/**
 * Returning a pair
 */
template<typename T, size_t len_x, size_t len_y>
std::pair<size_t, size_t> search (T const (&m)[len_x][len_y], T const &x) {
    for(size_t pos_x = 0; pos_x < len_x; ++pos_x) {
        for(size_t pos_y = 0; pos_y < len_y; ++pos_y) {
            if(m[pos_x][pos_y] == x) {
                return std::pair<size_t, size_t>(pos_x, pos_y);
            }
        }
    }

    // return an invalid value if not found
    return std::pair<size_t, size_t>(len_x, len_y);
}

/**
 * Example: Encapsulate an "int" in a "Value" object
 */
class Value {
  private:
    int m_val;
    
  public:
    Value(int val) : m_val (val) {}
    int val() const { return m_val; }

    // comparison should just compare the internal values
    bool operator==(const Value& other) const {
      return val() == other.val();
    }
};

int main() {
  /**
   * Create a 4x4 matrix
   * 0 1 2 3
   * 4 5 6 7
   * 8 9...
   */
  Value matrix[4][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

  // Value to search for (you can change this!)
  Value needle (7);

  // actual search
  auto pos = search(matrix, needle);

  // evaluate return value
  if (pos.first >= 4 && pos.second >= 4) {
    std::cout << "needle was not found" << std::endl;
  } else {
    std::cout << "Found needle at matrix[" << pos.first << "][" << pos.second << "]" << std::endl;
  }

  return 0;
}