简体   繁体   English

如何管理结构类型为XPoint的向量?

[英]How to manage a vector of struct type XPoint?

good evening. 晚上好。 Yeah I'm a newbie in this world of C and C++. 是的,我是C和C ++领域的新手。 What should I do to solve this: 我应该怎么做才能解决这个问题:

int i;
vector <XPoint> originales;
originales.reserve(7);
XPoint asteroid[5];
for(k = 0; k < 7; k++){
    for(i = 0; i < 5; i++){
        asteroid[i].x = rand() % 20 - 100;
        asteroid[i].y = rand() % 20 - 100;
    }
    originales.push_back(*asteroid);
}//end of first for

/*
    The XPoint structure contains:
    typedef struct {
        short x, y; 
    } XPoint; 
*/

When I print the coordinates are incomplete. 当我打印时,坐标不完整。 Is this means that You can't save XPoint[] arrays inside a vector of XPoint?. 这是否意味着您无法将XPoint []数组保存在XPoint的向量内? In that case, how can I fix it? 在这种情况下,我该如何解决? Please help! 请帮忙!

Arrays and vectors are not compatible. 数组和向量不兼容。 The only thing you can push_back onto originales is an XPoint . 您可以将push_back originales上的唯一东西是XPoint But vector s already handle this sort of thing very well - they were made for it! 但是vector已经很好地处理了这种事情-他们是为此而生的! - so you don't need the extra array at all: -因此您根本不需要额外的数组:

for(k = 0; k < 7; k++){
    for(i = 0; i < 5; i++){
        XPoint asteroid;
        asteroid.x = rand() % 20 - 100;
        asteroid.y = rand() % 20 - 100;
        originales.push_back(asteroid);
    }
}

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

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