-1

I want to be able to create N skyscrapers. Using an inputdata string, I would like to give them coordinate values of their X and Y positions. My main function I used "i" to demonstrate that I am trying to create as many skyscrapers as I can using the input data. Essentially, I would like to create N/3 skyscrapers and assign the input to coordinates for each.

#include <iostream>
#include <vector>
#include <string>
#include <math.h>

using namespace std;  
vector<int> inputData = {1, 4, 10, 3, 5, 7, 9, 10, 4, 11, 3, 2, 14, 5, 5}; 
int N = inputData.size();

class Buildings{
    public: 
        int yCoordinateLow; 
        int yCoordinateHigh; 
        int xCoordinateLeft; 
        int xCoordinateRight;

}; 

int main(){ 
    for(int i=0; i<N; i=i+3){
        Buildings skyscraper; 
        skyscraper.xCoordianteLeft = inputData.at(i); 
        skyscraper.yCoordianteLow = 0;
        skyscraper.yCoordinateHigh = inputData.at(i+1); 
        skyscraper.xCoordinateRight = inputData.at(i+2); 
    }
    return 0;
}
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222

1 Answers1

3

Jeff Atwood once said: use the best tools money can buy. And those aren't even expensive: Visual Studio community edition is free. Such a proper IDE will tell you that the skyscraper is unused except for the assignments.

Visual Studio tooltip

Since you probably want to do something with those skyscrapers later, you should store them somewhere, e.g. in another vector.

int main() {
    vector<Buildings> skyscrapers;
    for (int i = 0; i < N; i = i + 3) {
        Buildings skyscraper{};
        skyscraper.xCoordinateLeft = inputData.at(i);
        skyscraper.yCoordinateLow = 0;
        skyscraper.yCoordinateHigh = inputData.at(i + 1);
        skyscraper.xCoordinateRight = inputData.at(i + 2);
        skyscrapers.push_back(skyscraper);
    }
    return 0;
}

Other than that, I'd say the loop works fine as long as there are N*3 coordinates in the original vector.

If you e.g. implement a game, you would probably not hard code the skyscraper coordinates in a vector but rather read that data from a file, potentially per level.

Instead of doing all the error-prone coding, maybe you want to initialize the skyscrapers immediately

vector<Buildings> skyscrapers = {{1,0,4,10}, {3,0,5,7}, {9,0,10,4}, {11,0,3,4}, {14,0,5,5}};
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • 'Not sure why you're pushing back onto the vector. The compiler *may* optimise this, but otherwise you're unnecessarily copying each element (plus, causing more copies in the resizing). Why not do it like [this](https://godbolt.org/z/WYYPhjqjq)? – Elliott Mar 18 '22 at 07:34
  • @Elliott: I did that because I wanted that the OP still recognizes his parts of the code. I also provide an alternative solution which initializes the vector on its construction. – Thomas Weller Mar 18 '22 at 07:42
  • @Elliott: Your proposal does not keep the original class. It looks to me a bit like "why don't you rewrite everything from scratch"? The copying does not hurt for 5 objects. It's likely not a performance issue. I tend to not optimize unless I have a specific performance goal which is measurable. – Thomas Weller Mar 18 '22 at 07:47
  • I meant "*like* this." Not "*exactly* this." ie. I meant the way I initialise the vector size and iterate over its elements instead. – Elliott Mar 18 '22 at 07:53