矢量问题 [英] VECTOR problem

查看:55
本文介绍了矢量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在完成我的任务,但在最后一步遇到了问题!!!!!!


为了方便阅读,我还有很多其他的东西


// =====================代码开始=================== =============

class Buyer {

void start(void);

friend void Buyer_run(买方*买方);

};

void买方::开始(无效){

Buyer_run(this);

};


std :: vector< Buyer> buyer_queue;


void买家:: Buyer_run(买家*买家){

/ * =============== =========================

*我的问题在这里。我可以找到存储在矢量中的对象

并指向*买家?

*

*一开始,我这样做,我为买家类写了一个函数。

*以下:

*

*买方*买方:: return_address(无效){

*返回此;

*}

*

*然后使用迭代器循环向量,之后

* I这样做 - >

* if((* iterator).return_address()==(*买家).return_ address())

*如果是是的,我会使用buyer_queue.erase();来删除元素。

*

*但if()语句永远不会返回true,为什么会这样? ????

*

*总结一下,如何删除指针所指向的元素(

只知道/拥有该元素指针)?

* ===================================== =======

* /

};


void main {


买方b =买方();

b.start();


}

I was doing my assignment,but encountered a problem at last step!!!!!!

for easy reading, i ommited lots of other things

//=====================code begin================================
class Buyer{
void start(void);
friend void Buyer_run(Buyer *buyer);
};
void Buyer::start(void){
Buyer_run(this);
};

std::vector<Buyer> buyer_queue;

void Buyer::Buyer_run(Buyer* buyer){
/*========================================
*my problem is here.how can I find out the object which stored in vector
and pointed by *buyer ?
*
*at beginning ,I did it like this,I wrote a function for Buyer class.like
*below:
*
*Buyer* Buyer::return_address(void) {
*return this;
*}
*
*and then use iterator to loop the vector,after that
*I do it like this-->
*if((*iterator).return_address()==(*buyer).return_ address())
*if it is true ,i will use buyer_queue.erase();to delete the element.
*
*but the if()statement never return true,why is that?????
*
*to sum up,how can I delete the element which that pointer pointed(by
only knowing/haveing that pointer)?
*============================================
*/
};

void main{

Buyer b=Buyer();
b.start();

}

推荐答案



" Joseph" <吴******* @ hotmail.com> schrieb im Newsbeitrag

新闻:Xn ********************** @ 203.96.216.21 ...

"Joseph" <wu*******@hotmail.com> schrieb im Newsbeitrag
news:Xn**********************@203.96.216.21...
我正在完成我的任务,但最后遇到问题
步!!!!!!

方便阅读,我还有很多其他的东西

// =====================代码开始========================= =======
类买家{
void start(void);
friend void Buyer_run(买方*买家);
};
void买家: :start(void){
Buyer_run(this);
};

std :: vector< Buyer> buyer_queue;

void买家:: Buyer_run(买家*买家){
/ * ======================= =================
*我的问题在这里。我可以找到存储在
向量中并由*买家指向的对象吗? * * *
*一开始,我这样做了,我为买家写了一个函数
class.like *如下:
*
*买家*买家:: return_address(无效){
*返回此;
*}
*
*然后使用迭代器循环向量,之后
*我这样做 - >
* if((* iterator).return_address()==(*买方).return_ address())
*如果是,我将使用buyer_queue.erase();删除
元素。 *
*但是if()语句永远不会返回true,为什么要总结一下,我怎样才能删除那个指针$ b $的元素b指出(只知道/有指针)?
* ================================= ===========
* /
};

无效主要{

买方b =买方();
b.start();

}
I was doing my assignment,but encountered a problem at last step!!!!!!
for easy reading, i ommited lots of other things

//=====================code begin================================
class Buyer{
void start(void);
friend void Buyer_run(Buyer *buyer);
};
void Buyer::start(void){
Buyer_run(this);
};

std::vector<Buyer> buyer_queue;

void Buyer::Buyer_run(Buyer* buyer){
/*========================================
*my problem is here.how can I find out the object which stored in vector and pointed by *buyer ?
*
*at beginning ,I did it like this,I wrote a function for Buyer class.like *below:
*
*Buyer* Buyer::return_address(void) {
*return this;
*}
*
*and then use iterator to loop the vector,after that
*I do it like this-->
*if((*iterator).return_address()==(*buyer).return_ address())
*if it is true ,i will use buyer_queue.erase();to delete the element. *
*but the if()statement never return true,why is that?????
*
*to sum up,how can I delete the element which that pointer pointed(by only knowing/haveing that pointer)?
*============================================
*/
};

void main{

Buyer b=Buyer();
b.start();

}




慢慢地,牛仔。如果你使用vector类分配新对象,那么当你添加新元素时,

指向元素的指针可能会改变,或者

删除一些元素。使用列表<>如果你想保留指针地址。

接下来,你不需要一个return_address。功能,因为&

exaclty this:

if(& * iterator == pointer_to_buyer)...

如果你只有指向该元素的指针(为什么你没有

迭代器!?)你必须循环遍历列表(矢量,

仍然?)并检查它是否'的指针指向相同的。然后你有一个

迭代器并且可以删除它。同样,添加/ rmoving可能会更改向量中的_all_容器元素的地址

<>容器!使用列表<>,

保留地址直到销毁。

最佳解决方案:将迭代器传递给该函数。


HTH,


-

-Gernot

int main(int argc,char ** argv){printf

("%silto%c%cf%cgl%ssic%ccom%c"," ma",58,''g'',64," ba",46,10) ;}


________________________________________

寻找好游戏?亲自动手吧!

GLBasic - 你可以这样做
www.GLBasic .com






Slowly, cowboy. If you allocate new objects with vector class, the
pointers to the elements might change when you add new elements or
remove some. Use a list<> if you want the pointer addresses to stay.
Next, you don''t need a "return_address" function, since "&" does
exaclty this:
if (&*iterator == pointer_to_buyer) ...
If you only have the pointer to that element (why don''t you have an
iterator to it!?) you would have to loop through the list (vector,
still?) and check if it''s pointer addresses equal. Then you have an
iterator and can erase it. Again, adding/rmoving may change addresses
of _all_ container elements in a vector<> container! Use a list<>,
which keeps the addresses until destruction.
Best solution: Pass an iterator to that function.

HTH,

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, ''g'', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com






Joseph< wu ***** **@hotmail.com>写道:
Joseph <wu*******@hotmail.com> wrote:
我正在完成我的任务,但在最后一步遇到了问题!!!!!!

为了方便阅读,我省略了很多其他的事情

// =====================代码开始================ ================
类买家{
void start(无效);
朋友void Buyer_run(买家*买家);
};
void Buyer :: start(void){
Buyer_run(this);
};

std :: vector< Buyer> buyer_queue;

void买家:: Buyer_run(买家*买家){
/ * ======================= =================
*我的问题在这里。我可以找到存储在矢量中的对象并由*买家指出?


vector< Buyer> :: iterator it = buyer_queue.begin();


while(it!= buyer_queue.end()| |& * it!=买家){

++ it;

}


if(it!= buyer_queue。 end()){

//它是向量中买方的迭代器

}

*但if()语句从不返回true,为什么是?????


因为你传递给函数的买家不在向量中。

我的猜测是,你遇到的问题不是在您发布的代码中

(虽然非常规,应该做你想要的。)你的问题是

认为买方传入矢量实际上是存在的。

注意你的主要功能:

void main {
应该是''int main(){''
买方b =买方() ;
''=买方()''部分是没有必要的。

b.start();

}
I was doing my assignment,but encountered a problem at last step!!!!!!

for easy reading, i ommited lots of other things

//=====================code begin================================
class Buyer{
void start(void);
friend void Buyer_run(Buyer *buyer);
};
void Buyer::start(void){
Buyer_run(this);
};

std::vector<Buyer> buyer_queue;

void Buyer::Buyer_run(Buyer* buyer){
/*========================================
*my problem is here.how can I find out the object which stored in vector
and pointed by *buyer ?
vector<Buyer>::iterator it = buyer_queue.begin();

while ( it != buyer_queue.end() || &*it != buyer ) {
++it;
}

if ( it != buyer_queue.end() ) {
// it is an iterator to the buyer in the vector
}
*but the if()statement never return true,why is that?????
Because the buyer you are passing into the function isn''t in the vector.
My guess is, the problem you are having isn''t in the code you posted
(which, while unconventional, should do what you want.) Your problem is
in thinking that the buyer passed into the vector is actually there.
Note your main function:
void main{ that should be ''int main() {''
Buyer b=Buyer(); the ''=Buyer()'' part isn''t necessary.
b.start();

}



您创建了一个Buyer对象,但从未将其添加到向量中。如果你确实这么做了,我希望你做的是这样的:


int main(){

买家b;

buyer_queue.push_back(b);

b.start();

}


以上在向量中找不到买方,因为''b''不是*

向量,它在主函数中。如果你这样做:


int main(){

买方b;

buyer_queue.push_back(b);

buyer_queue [0] .start();

}


然后你将在一个* in *的对象上调用start向量。



You created a Buyer object, but never added it to the vector. If you
did, I expect you did something like this:

int main() {
Buyer b;
buyer_queue.push_back(b);
b.start();
}

The above will not find the Buyer in the vector because ''b'' isn''t *in*
the vector, it''s in the main function. If you do this:

int main() {
Buyer b;
buyer_queue.push_back(b);
buyer_queue[0].start();
}

then you will be calling start on an object that is *in* the vector.




" Joseph" <吴******* @ hotmail.com>在消息中写道

news:Xn ********************** @ 203.96.216.21 ...

"Joseph" <wu*******@hotmail.com> wrote in message
news:Xn**********************@203.96.216.21...
我正在完成我的任务,但在最后一步遇到了问题!!!!!!

为了方便阅读,我省略了很多其他的事情

// == ===================代码开始============================= ===
类买家{
void start(void);
friend void Buyer_run(买方*买方);
};
void Buyer :: start(void ){
Buyer_run(this);
};


为什么要使用朋友功能,但通过它

std :: vector< Buyer> buyer_queue;

void买家:: Buyer_run(买家*买家){
/ * ======================= =================
*我的问题在这里。我可以找到存储在矢量中的对象
并由*买家指出?
*一开始,我这样做了,我为买家类写了一个函数。如下
*:
*
*买家*买家:: return_address(无效){
*返回此内容;
*}
*


无需此功能。指针本身保存

对象的地址。对于迭代器,你可以使用& *它。

*然后使用迭代器来循环向量,之后
*我这样做 - >
* if((* iterator).return_address()==(* buyer).return_ address())
*如果是,我将使用buyer_queue.erase();来删除元素。
*
*但是if()语句永远不会返回true,为什么要总结一下,如何删除该指针所指向的元素(通过
只知道/拥有该指针)?
* ================================ ============
* /
};

void main {

买方b =买方();


只是买方b;这就是你所需要的。或者您打算买方* b =新

买方();?

b.start();

}
I was doing my assignment,but encountered a problem at last step!!!!!!

for easy reading, i ommited lots of other things

//=====================code begin================================
class Buyer{
void start(void);
friend void Buyer_run(Buyer *buyer);
};
void Buyer::start(void){
Buyer_run(this);
};
Why use a friend function, but pass it

std::vector<Buyer> buyer_queue;

void Buyer::Buyer_run(Buyer* buyer){
/*========================================
*my problem is here.how can I find out the object which stored in vector
and pointed by *buyer ?
*
*at beginning ,I did it like this,I wrote a function for Buyer class.like
*below:
*
*Buyer* Buyer::return_address(void) {
*return this;
*}
*
No need for this function. A pointer itself holds the address of the
object. And for iterators you can just use &*it.
*and then use iterator to loop the vector,after that
*I do it like this-->
*if((*iterator).return_address()==(*buyer).return_ address())
*if it is true ,i will use buyer_queue.erase();to delete the element.
*
*but the if()statement never return true,why is that?????
*
*to sum up,how can I delete the element which that pointer pointed(by
only knowing/haveing that pointer)?
*============================================
*/
};

void main{

Buyer b=Buyer();
Just "Buyer b;" is all you need here. Or did you intend "Buyer* b = new
Buyer();"?
b.start();

}




我想你已经删除了*远*太多了。你不要把b推到矢量

的任何地方,所以它不在矢量中。即使你这样做了,为什么要立即

搜索并删除它?发布的简化很好,但不是代码含义的代价。


您确定要将对象本身存储在向量?你知道,当你调用push_back时,一个向量会生成对象的*副本*,因此向量中的对象的

地址将永远不会相同作为原始的

对象。如果你需要使用地址来比较对象,那么你需要推送指针,而不是对象。 (在这种情况下,只需将它与

指针进行比较,而不是& *它。)


-Howard




I think you''ve removed *far* too much. You don''t push b onto the vector
anywhere, so it''s not in the vector. And even if you did, why immediately
search for it and remove it? Simplification for posting is fine, but not at
the expense of the meaning of the code.

Are you sure you want to store the objects themselves in the vector? You
know, a vector makes a *copy* of the object when you call push_back, so the
address of the object in the vector wil *never* be the same as the original
object. If you need to use addresses to compare objects, then you need to
push pointers, not objects. (And in that case, just compare *it with the
pointer, instead of &*it.)

-Howard



这篇关于矢量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆