简体   繁体   English

我怎样才能使这个 function 适用于 100 个对象(c++)?

[英]How can I make this function work for 100 objects(c++)?

So I have a program that shows the numbers in a text file of each object, the problem is that the original program is with only 6 objects, which I found preety easy, but now I have to change it to work with 100 objects.所以我有一个程序可以显示每个 object 的文本文件中的数字,问题是原始程序只有 6 个对象,我发现这很容易,但现在我必须更改它以使用 100 个对象。 I tried using a for loop but it only showed me the first object.我尝试使用 for 循环,但它只显示了第一个 object。 I don't have much experience programming and have tried other ways but none of them work.我没有太多编程经验,也尝试过其他方法,但都没有奏效。 Anyways, here's the function:无论如何,这是 function:

void Circle::writeAll(Circle circle[6], int &count) {
if (count == 0) {
    cout << "No circles in file. " << endl << endl;
};
if (count == 1 || count == 2 || count == 3 || count == 4 || count == 5 || count == 6) {
    cout << " Radius: " << circle[0].getRadius() << "; Center(x): " << circle[0].getCenterx() << "; Center(y): " << circle[0].getCentery() << endl;
};
if (count == 2 || count == 3 || count == 4 || count == 5 || count == 6) {
    cout << " Radius: " << circle[1].getRadius() << "; Center(x): " << circle[1].getCenterx() << "; Center(y): " << circle[1].getCentery() << endl;
};
if (count == 3 || count == 4 || count == 5 || count == 6) {
    cout << " Radius: " << circle[2].getRadius() << "; Center(x): "  << circle[2].getCenterx() << "; Centery(y): " << circle[2].getCentery() << endl;
};
if (count == 4 || count == 5 || count == 6){
  cout << " Radio: " << circle[3].getRadius() << "; Center(x): " << circle[3].getCenterx() << "; Center(y): " << circle[3].getCentery() << endl;  
};
if (count == 5 || count == 6){
  cout << " Radius: " << circle[4].getRadius() << "; Center(x): " << circle[4].getCenterx() << "; Center(y): " << circle[4].getCentery() << endl;  
};
if (count == 6){
  cout << " Radio: " << circle[5].getRadius() << "; Center(x): " << circle[5].getCenterx() << "; Center(y): " << circle[5].getCentery() << endl;  
};
cout << endl;

}; };

Instead of an array I would use a built in container type like std::vector .我会使用像std::vector这样的内置容器类型,而不是数组。 In this case it would look like this:在这种情况下,它看起来像这样:

void Circle::writeAll(const std::vector<Circle>& circles)
{
    size_t size = circles.size();

    if (size == 0)
    {
        cout << "no circles in file" << endl;
        return;
    }

    // print all the circles
    for (auto& circle : circles)
    {
        cout << " Radio: " << circle.getRadius() << "; Center(x): " << circle.getCenterx() << "; Center(y): " << circle.getCentery() << endl;
    }
}



It seemed to me you are trying to print every object until the number count is reached.在我看来,您正在尝试打印每个 object 直到达到count
Well this will do the same but with a vector of Circle objects.好吧,这将做同样的事情,但使用Circle对象的vector

And as @Josh said if I misunderstood something, please feel free to correct me.正如@Josh 所说,如果我误解了什么,请随时纠正我。

It looks like this is what you're trying to do: (unless I'm not getting the problem right)看起来这就是你想要做的:(除非我没有解决问题)

void Circle::writeAll(Circle circle[], int &count,int size)
{
    for(int i = 0; i <= size; i++)
    {
        if(count == 0)
            cout << "No circles in file. " << endl << endl;
        else if(count == i)
            cout << " Radius: " << circle[i-1].getRadius() << "; Center(x):"
            << circle[i].getCenterx() << "; Center(y): " << circle[i-1].getCentery() << endl;
    }
}

however you will need to send in a size parameter if the solution is not clear feel free to comment但是,如果解决方案不清楚,您将需要发送size参数,请随时发表评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM