1

I have a triangle class consisting of a triangle's sides perimeter and many helper functions.

I have created a function that will populate an array of instances of triangle. Let us say I will create 3 objects so the triangles parameter will be set to 3.

void populate_triangle(triangle in_array[], int triangles) {
    for (int i = 0; i < triangles; i++) {
        in_array[i].input_random_sides();
        in_array[i].get_perimeter();
    }
}

I have also created a print function that will output all the information about said 3 objects, i.e. the three sides and perimeter.

void print_triangle(trianglein_array[], int triangles) {
    for (int i = 0; i < triangles; i++) {
        cout << "Triangle N: " << i << endl;
        in_array[i].print_information();
    }
}

I later call them in my main using

populate_triangle(in_array, 3);
print_triangle(in_array, 3);

I feel like this way is very inefficient and ugly.

Is there a better way to go through the filled-in array, other than the for (int i = 0; i < triangles; i++) loop? I know I can fuse this into one function, but I want to know how to do it with two.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
DarknessPlusPlus
  • 543
  • 1
  • 5
  • 18
  • 1
    If you used `std::array` or `std::vector` you could have used a range based for loop. The choice between the two here are if the number of items is fixed or not. std::vector if the number can change. – drescherjm Dec 04 '20 at 19:07
  • Why aren't you using `std::array`? Why are you using `int` instead of `size_t`? And why are you writing C++ without using the STL and its Iterators functionality? – Dai Dec 04 '20 at 19:07
  • 3
    If you use arrays and pointers (as you are currently) then there's no better. I'm not sure why you think it is very inefficient and ugly. What were you hoping for instead? Can you explain what your concern is? Maybe someone could set your mind at rest. – john Dec 04 '20 at 19:14
  • Because I had this pythonic thought that I could go “for each in array”. – DarknessPlusPlus Dec 04 '20 at 19:16
  • It's C++ though, not Python. It's usually not going to be pythonic.. – harold Dec 04 '20 at 19:19
  • The range based for would be similar. There are some good examples here: [https://stackoverflow.com/a/15927037/487892](https://stackoverflow.com/a/15927037/487892) – drescherjm Dec 04 '20 at 19:19
  • You can write code that is quite pythonic actually. But the first step would be to stop using arrays, and use `std::vector` instead. – cigien Dec 04 '20 at 19:31
  • If you're looking for speed and know roughly how many loop iterations you will get you could manually unroll the loop, or use other techniques such as threading and `std::async`. If you're looking for something that is simply cleaner code-wise then there really isn't much you can do. – user2205930 Dec 04 '20 at 19:34
  • the C++thonic way is to use a `std::vector` for dynamic arrays instead of pointer + size – 463035818_is_not_an_ai Dec 04 '20 at 19:44

1 Answers1

2

As mentioned in the comments, you can use a std::vector.

std::vector keeps track of its own length so when you write your methods, there's no need to include the length as an extra argument, it's automatically deduced in the range for loop.

With a std::vector your method might look something like this:

void populate_triangle(std::vector<Triangle> triangles) {
    for (auto triangle : triangles) {
        triangle.input_random_sides();
        triangle.get_perimeter();
    }
}

std::vector's data array is guaranteed to be stored in contiguous memory.

Josh Hardman
  • 721
  • 6
  • 17