声明一个由 struct C 的元素组成的向量,元素数量为 i(int 类型的输入)

发布于 2024-07-14 04:16:47 字数 1191 浏览 5 评论 0原文

请查看这段代码(并原谅缺乏知识)。 它输出我无法解决的错误。 我需要声明 struct C 的元素向量,但我需要元素的数量为 i (int 类型的输入)。

我还尝试了其他方法,但在所有方法中我都收到错误(无法将 C 转换为 int 等)。 我怎样才能做到这一点?

# include < iostream >
using std::cout;
using std::cin;
using std::endl;

# include < vector >
using std::vector;

struct C{
    int cor;
    vector<int>cores;

    };

    void LerVector( vector< C> &array ) ;

int main ()
{
     int n;
    bool done=false;
        bool don=false;
    vector<C>cidade;
    int i;


    while(!done){
    cout<<"Entre o número de cidades "<<endl;
    cin>>n;
    if(n>500)
    {
        cout<<endl;
        cout<<"O número máximo é 500"<<endl;
}
else
done=true;
}
n--;
while(!don){
cout<<"Entre o número de confederações"<<endl;
cin>>i;
if(i>100){
cout<<endl;
cout<<"Número máximo de 100 cidades"<<endl;

}
else {

 LerVector(  cidade) ;

don=true;
}
}


    cin.get();
    return 0;
}
//resolve...
 void LerVector( vector< C> &array ) 
  { 
    for ( size_t i = 0; i < array.size(); i++ ) 
      cin>>array[i];

  } // end function inputVector 

Please look at this code (and forgive the lack of knowledge). It outputs errors that I couldn't solve. I need to declare a vector of elements of struct C,but I need the number of elements be i (a input of type int).

I also tried others approaches but in all of them I received an error (cannot convert C to int,etc). How can I do this?

# include < iostream >
using std::cout;
using std::cin;
using std::endl;

# include < vector >
using std::vector;

struct C{
    int cor;
    vector<int>cores;

    };

    void LerVector( vector< C> &array ) ;

int main ()
{
     int n;
    bool done=false;
        bool don=false;
    vector<C>cidade;
    int i;


    while(!done){
    cout<<"Entre o número de cidades "<<endl;
    cin>>n;
    if(n>500)
    {
        cout<<endl;
        cout<<"O número máximo é 500"<<endl;
}
else
done=true;
}
n--;
while(!don){
cout<<"Entre o número de confederações"<<endl;
cin>>i;
if(i>100){
cout<<endl;
cout<<"Número máximo de 100 cidades"<<endl;

}
else {

 LerVector(  cidade) ;

don=true;
}
}


    cin.get();
    return 0;
}
//resolve...
 void LerVector( vector< C> &array ) 
  { 
    for ( size_t i = 0; i < array.size(); i++ ) 
      cin>>array[i];

  } // end function inputVector 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故事灯 2024-07-21 04:16:47

让我们尝试解释一下:)

cin >> array[i];

它尝试从 cin 提取到 struct C 的对象中。好吧,所以它需要一个运算符>>> 这实际上做到了这一点:

istream & operator>>(istream &is, C &c) {
    is >> c.cor; // or into whatever member 
    return is;
}

此外,正如另一位提到的,您必须首先将元素添加到向量中:

while(!don){
    cout<<"Entre o número de confederações"<<endl;
    ....
} else {
    cidade.resize(i); // resize to i elements
    LerVector(cidade);
    don = true;
}

下次,请格式化文本(正确缩进它)。 我很难迈出这一步:)

Let's try with an explanation :)

cin >> array[i];

That tries to extract from cin into an object of struct C. Well, so it needs an operator>> that actually does that work:

istream & operator>>(istream &is, C &c) {
    is >> c.cor; // or into whatever member 
    return is;
}

In addition, as another one mentioned, you have to actually add the elements to the vector first:

while(!don){
    cout<<"Entre o número de confederações"<<endl;
    ....
} else {
    cidade.resize(i); // resize to i elements
    LerVector(cidade);
    don = true;
}

For the next time, please format the text (correct indent it). It was hard for me to step through it :)

夏日浅笑〃 2024-07-21 04:16:47

您的代码生成了哪些错误?

我也不确定你的代码应该做什么。
在 main() 中,您创建了一个 C 向量。但 C 还包含一个 int 向量。 这是故意的吗?

Which errors did your code generate?

I'm also not sure what your code is supposed to do.
In main(), you create a vector of C. But C also contains a vector of int's. Is that intended?

行至春深 2024-07-21 04:16:47

我不太清楚你想做什么。

然而,我已经可以在我们的代码中看到一个潜在的错误:

在 LerVector 中,您传入一个对当前没有任何项目的向量的引用,因此大小为 0。

您想要做什么只要 i 小于大小,就可以更新数组中的该项。 然而,当你开始时 size 是 0 所以我认为你甚至不会进入输入循环。

现在,即使您这样做了,由于向量未初始化为任何大小,您可能会收到一个错误,表明您超出了范围。 您必须调整阵列的大小。

I'm not really clear what you're trying to do.

However, I can already see one potential error in our code:

In LerVector, you come in with a reference to a vector that does not currently have any items in it, and therefore has a size of 0.

What you're trying to do is that as long as i is smaller than the size, you update that item in the array. However, when you start out size is 0 so I don't think you'll even go into the input loop.

Now, even if you did, since the vector is not initialized with any size, you may get an error that you're going out of bounds. You have to resize the rray.

春夜浅 2024-07-21 04:16:47

如果我猜你想做什么,应该是这样的:

// First create an empty vector of C's
vector<C> cidade;

// cidade has zero elements now
// Read i from user
cin >> i;

// Resize vector to contain i elements
cidade.resize(i);

// Then go on and fill them.
int n;
for (n = 0; n < i; i++) {
  cin >> cores;
  cidade[n].cores.resize(cores);
  // now cidade[n].cores has 'cores' elements, but they are uninitialized
}

if I guess what you want to do, it should be like this:

// First create an empty vector of C's
vector<C> cidade;

// cidade has zero elements now
// Read i from user
cin >> i;

// Resize vector to contain i elements
cidade.resize(i);

// Then go on and fill them.
int n;
for (n = 0; n < i; i++) {
  cin >> cores;
  cidade[n].cores.resize(cores);
  // now cidade[n].cores has 'cores' elements, but they are uninitialized
}
呆头 2024-07-21 04:16:47

其中一个 std::vector构造函数将采用初始大小,如果在知道该数字之后声明,则可以将其传递给构造函数。

cin >> n;
std::vector<C> cidade(n);

或者您可以使用调整大小方法来更改矢量的大小。

或者您可以使用 add 方法来扩展向量(无需明确给出大小)。

但总的来说,提供完整版本的代码以及代码试图执行的更多详细信息可能更容易。

One of the std::vector<T> constructors will take an initial size, and if declared after that number is know you can pass it to the constructor.

cin >> n;
std::vector<C> cidade(n);

Or you can use the resize method to change a vector's size.

Or you can use the add method to extend the vector (without explicitly giving a size).

But overall, it might be easier to give help with a full version of the code and more details of what the code is trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文