新的,删除,STL [英] new, delete, STL

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

问题描述

我在分配矢量的某些元素时遇到问题,然后删除

它们。


Basicaly我有这样的事情:

班级基础

{

私人:

std :: vector< object> V;

// std :: vector< * object> V;


public:


void addObject()

{

object * temp = new object;

V.push_back(* temp);


//V.push_back(new object);

}

void removeObject(index)

{

delete& V [index];

V .erase(V.begin()+ index);


//删除V [index];

//V.erase(V.begin( )+ index);

}


};


注释行是我使用指针版本的地方。在第一个

的情况下,对象的析构函数被调用两次(我想因为当我调用
addObject时,局部变量超出范围(任何方法来防止这种情况) )并且

在第二种情况下它根本没有被调用; /


我宁愿不使用指针向量。 ..我想也许我可以

将对象* temp声明为静态,但这似乎有点像黑客......我只需要一种方式来说b $ b

它超出范围或者其他东西时,不要为这个本地对象调用析构函数。


任何想法?


Jon

I''m having a problem allocating some elements of a vector then deleting
them.

Basicaly I have something like this:
class base
{
private:
std::vector<object> V;
//std::vector<*object> V;

public:

void addObject()
{
object *temp = new object;
V.push_back(*temp);

//V.push_back(new object);
}
void removeObject(index)
{
delete &V[index];
V.erase(V.begin() + index);

//delete V[index];
//V.erase(V.begin() + index);
}

};

the commented line is where I used the pointerized version. In the first
case the object''s destructor gets called twice(I suppose because when I call
addObject the local variable goes out of scope(any way to prevent this) and
in the second case it doesn''t get called at all ;/

I''d rather not use a vector of pointers though... I thought maybe I could
declare the object *temp as static but this seems to be kinda a hack... I
just need a way to say "don''t call the destructor for this local object when
it goes out of scope" or something.

Any ideas?

Jon

推荐答案

" Jon Slaughter"< Jo *********** @ Hotmail。 com>在消息中写道

news:11 ************* @ corp.supernews.com ...
"Jon Slaughter" <Jo***********@Hotmail.com> wrote in message
news:11*************@corp.supernews.com...
我是在分配向量的某些元素时遇到问题然后删除它们。


那是因为你是我们操作员新手太多了。

Basicaly我有这样的事情:

班级基础
{
私人:
std :: vector< ;对象> V;
// std :: vector< * object> V;

公开:

void addObject()
{
对象* temp =新对象;
V.push_back(* temp );

//V.push_back(new object);
}

void removeObject(index)
{
删除& V [index];
V.erase(V.begin()+ index);

//删除V [index];
//V.erase(V。 begin()+ index);
}

};

[snip]
Jon
I''m having a problem allocating some elements of a vector then deleting
them.
That''s because you are using operator new too much.

Basicaly I have something like this:
class base
{
private:
std::vector<object> V;
//std::vector<*object> V;

public:

void addObject()
{
object *temp = new object;
V.push_back(*temp);

//V.push_back(new object);
}
void removeObject(index)
{
delete &V[index];
V.erase(V.begin() + index);

//delete V[index];
//V.erase(V.begin() + index);
}

};
[snip]
Jon




班级基础

{

私人:

std :: vector< object> V;


public:

void addObject()

{

V.push_back(object ());

}


void removeObject(index)

{

V.erase (V.begin()+ index);

}

};


-

Cy
http://home.rochester.rr.com/cyhome /




" Jon Slaughter" <乔*********** @ Hotmail.com>在消息中写道

新闻:11 ************* @ corp.supernews.com ...

"Jon Slaughter" <Jo***********@Hotmail.com> wrote in message
news:11*************@corp.supernews.com...
我有一个分配向量的一些元素然后删除它们的问题。


真的,我发现这很令人惊讶,因为std :: vector

容器会自动为你做所有的分配。

Basicaly我有这样的事情:

班级基地
{
私人:
std :: vector< object> V;
// std :: vector< * object> V;

公开:

void addObject()
{
对象* temp =新对象;
V.push_back(* temp );


将前两行替换为:


V.push_back(object());


正如我最近在另一条消息中写道的那样,我不明白为什么这么多人觉得他们需要使用''new''才真正

。 :-)

//V.push_back(new object);
}

void removeObject(index)


这不会编译。你没有指定

参数''索引''的类型。在这种情况下它应该是:


void removeObject(std :: vector< object> :: size_type)

{
delete& V [index];
V.erase(V.begin()+ index);

//删除V [index];
//V.erase(V.begin ()+ index);


将所有内容替换为:


if(!V.empty())//以防止可能的UB

V.erase(V.begin()+ index); }

};

注释行是我使用指针版本的地方。在第一个
情况下,对象的析构函数被调用两次(我想是因为当我调用addObject时,局部变量超出范围(任何方式来阻止这个)并且在第二种情况它根本没有被调用; /

我宁愿不使用指针向量...我想也许我可以
声明对象* temp作为静态,但这似乎有点像黑客...我只需要一种方式来说当它超出范围时,不要为这个本地对象调用析构函数或者某种东西。
I''m having a problem allocating some elements of a vector then deleting
them.
Really, I find that surprising, since the std::vector
container does all allocations for you automatically.

Basicaly I have something like this:
class base
{
private:
std::vector<object> V;
//std::vector<*object> V;

public:

void addObject()
{
object *temp = new object;
V.push_back(*temp);
Replace previous two lines with:

V.push_back(object());

As I recently wrote in another message, I don''t understand
why so many folks feel they need to use ''new'' when they really
dont. :-)

//V.push_back(new object);
}
void removeObject(index)
This won''t compile. You didn''t specify the type of the
parameter ''index''. In this case it should be:

void removeObject(std::vector<object>::size_type)
{
delete &V[index];
V.erase(V.begin() + index);

//delete V[index];
//V.erase(V.begin() + index);
Replace all that with:

if(!V.empty()) // to prevent possible UB
V.erase(V.begin() + index); }

};

the commented line is where I used the pointerized version. In the first
case the object''s destructor gets called twice(I suppose because when I
call addObject the local variable goes out of scope(any way to prevent
this) and in the second case it doesn''t get called at all ;/

I''d rather not use a vector of pointers though... I thought maybe I could
declare the object *temp as static but this seems to be kinda a hack... I
just need a way to say "don''t call the destructor for this local object
when it goes out of scope" or something.




不,不要试图规避语言的运作方式,更好的是

来理解它并在预期中使用它方式。


-Mike



No, don''t try to circumvent the way the language works, better
to understand it and use it in the intended ways.

-Mike




" Cy Edmunds" ce *** ***@spamless.rochester.rr.com>在留言中写道

新闻:Ce ******************* @ twister.nyroc。 rr.com ...

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:Ce*******************@twister.nyroc.rr.com...
" Jon Slaughter"&Jo; Jo *** ********@Hotmail.com>在消息中写道
新闻:11 ************* @ corp.supernews.com ...
"Jon Slaughter" <Jo***********@Hotmail.com> wrote in message
news:11*************@corp.supernews.com...
我在分配一些元素时遇到问题然后删除
它们。
那是因为你使用的操作符太多了。
I''m having a problem allocating some elements of a vector then deleting
them.
That''s because you are using operator new too much.




同意。似乎有太多人这样做了。 :-)

void removeObject(index)
{


if(!V.empty())

V.erase(V.begin()+ index);
}
};



Agreed. And too many people seem to be doing so. :-)

void removeObject(index)
{
if(!V.empty())
V.erase(V.begin() + index);
}
};




-Mike



-Mike


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

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