声明一个由 struct C 的元素组成的向量,元素数量为 i(int 类型的输入)
请查看这段代码(并原谅缺乏知识)。 它输出我无法解决的错误。 我需要声明 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
让我们尝试解释一下:)
它尝试从 cin 提取到 struct C 的对象中。好吧,所以它需要一个运算符>>> 这实际上做到了这一点:
此外,正如另一位提到的,您必须首先将元素添加到向量中:
下次,请格式化文本(正确缩进它)。 我很难迈出这一步:)
Let's try with an explanation :)
That tries to extract from
cin
into an object of struct C. Well, so it needs an operator>> that actually does that work:In addition, as another one mentioned, you have to actually add the elements to the vector first:
For the next time, please format the text (correct indent it). It was hard for me to step through it :)
您的代码生成了哪些错误?
我也不确定你的代码应该做什么。
在 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?
我不太清楚你想做什么。
然而,我已经可以在我们的代码中看到一个潜在的错误:
在 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.
如果我猜你想做什么,应该是这样的:
if I guess what you want to do, it should be like this:
其中一个 std::vector构造函数将采用初始大小,如果在知道该数字之后声明,则可以将其传递给构造函数。
或者您可以使用调整大小方法来更改矢量的大小。
或者您可以使用 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.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.