【问题标题】:Declare a vector of elements of struct C,and have the number of elements be i (a input of type int)声明结构 C 的元素向量,元素个数为 i(int 类型的输入)
【发布时间】:2009-02-22 00:54:25
【问题描述】:

请查看此代码(并原谅缺乏知识)。它输出我无法解决的错误。我需要声明一个结构 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 

【问题讨论】:

  • 请做一个真实的标题

标签: c++ stl stdvector


【解决方案1】:

让我们试着解释一下:)

cin >> array[i];

它试图从cin 中提取到结构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;
}

下次请格式化文本(正确缩进)。我很难通过它:)

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      我不太清楚你想做什么。

      但是,我已经在我们的代码中看到了一个潜在的错误:

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

      您要做的是,只要 i 小于大小,您就会更新数组中的该项目。但是,当您开始时大小为 0,所以我认为您甚至不会进入输入循环。

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

      【讨论】:

        【解决方案4】:

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

        // 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
        }
        

        【讨论】:

          【解决方案5】:

          std::vector&lt;T&gt; 构造函数之一将采用初始大小,如果在知道该数字之后声明,您可以将其传递给构造函数。

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

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

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

          但总体而言,提供完整版代码的帮助可能更容易,并提供更多关于代码尝试执行的操作的详细信息。

          【讨论】:

            猜你喜欢
            • 2022-01-16
            • 2013-01-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-12-05
            • 1970-01-01
            • 2021-10-17
            相关资源
            最近更新 更多