STL删除算法问题

编程入门 行业动态 更新时间:2024-10-25 17:15:16
本文介绍了STL删除算法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好 我的项目代码与此类似: 注意:BSTR是一个令人憎恶的人在 COM世界中召唤出一些不安的人。必须使用 SysAllocString / SysFreeString Windows API分配/解除分配BSTR字符串。 typedef struct tagMyStruct { BSTR somestring; BSTR someotherstring; } MyStruct; vector< MyStruct> my_struct; 在我的应用程序中,我在MyStruct中分配了BSTR,并且 将它们填充到向量中。 当需要摆脱它们的时候,我想知道是否有一种方法可以释放每个MyStruct中BSTR所指向的内存 $ b使用_single_ STL算法调用向量内的$ b实例? 目前我使用for_each的组合(用谓词删除 BSTR)然后调用my_struct.erase(mystruct.begin(), mystruct.end())。 必须有更好的方法来做到这一点,对吧? br /> 我抬头删除并删除_if,但它们似乎不适合 我的情况......

Howdy I have code similar to this in my project: Note: BSTR is an abomination conjured up some disturbed person in the COM world. BSTR strings must be allocated/deallocated using the SysAllocString/SysFreeString Windows APIs. typedef struct tagMyStruct { BSTR somestring; BSTR someotherstring; } MyStruct; vector<MyStruct> my_struct; over the course of my app, I allocate the BSTRs inside MyStruct and stuff them into the vector. When the time comes to get rid of them I was wondering if there is a way to free the memory pointed to by the BSTR''s in every MyStruct instance inside the vector using a _single_ STL algorithm call? Currently I use a combination of for_each (with a predicate to delete the BSTRs) followed by a call to my_struct.erase(mystruct.begin(), mystruct.end()). There must be a better way to do this, right? I looked up remove and remove_if, but they don''t seem to be right for my situation...

推荐答案

Dilip写道: 我的项目代码与此类似: 注意:BSTR是一个令人憎恶的人在COM世界中召唤出一些不安的人。必须使用 SysAllocString / SysFreeString Windows API分配/取消分配BSTR字符串。 注意。 typedef struct tagMyStruct {BSTR somestring; BSTR someotherstring; } MyStruct ; 请使用C ++定义类型的方式,它更容易: struct MyStruct { BSTR somestring; BSTR someotherstring; }; vector< MyStruct> my_struct; 在我的应用程序中,我在MyStruct中分配BSTR并将它们填充到向量中。 你自己分配那些BSTR吗?为什么不把它交给MyStruct来分配?b $ b分配?你知道,例如,在一个构造函数中,例如... 当时间到了摆脱它们时,我想知道是否有一种方法来释放指向的内存使用_single_ STL算法调用向量内的每个MyStruct 实例中的BSTR? 在MyStruct中定义析构函数。让它解除分配那些东西。 当然,要遵循三法则,你需要定义 复制c-tor和赋值操作。 之后,对向量的简单破坏将释放所有 的东西。 目前我使用for_each的组合(使用谓词删除BSTR)然后调用my_struct.erase(mystruct.begin(), mystruct.end())。 OK 必须有更好的方法来做到这一点,对吧? 必须有。动态内存管理需要负责该内存所有者的。所以,让MyStruct处理它自己的内存为 它应该。 我查找删除和remove_if,但它们似乎不适合我的情况...... I have code similar to this in my project: Note: BSTR is an abomination conjured up some disturbed person in the COM world. BSTR strings must be allocated/deallocated using the SysAllocString/SysFreeString Windows APIs. Noted. typedef struct tagMyStruct { BSTR somestring; BSTR someotherstring; } MyStruct; Please use C++ way of defining types, it''s so much easier: struct MyStruct { BSTR somestring; BSTR someotherstring; }; vector<MyStruct> my_struct; over the course of my app, I allocate the BSTRs inside MyStruct and stuff them into the vector. Do you allocate those BSTR yourself? Why not give it to MyStruct to allocate? You know, like, in a constructor, for example... When the time comes to get rid of them I was wondering if there is a way to free the memory pointed to by the BSTR''s in every MyStruct instance inside the vector using a _single_ STL algorithm call? Define the destructor in MyStruct. Make it deallocate those things. Of course, to follow the Rule of Three, you will need to define the copy c-tor and the assignment op as well. After that, a simple destruction of the vector will free up all the things. Currently I use a combination of for_each (with a predicate to delete the BSTRs) followed by a call to my_struct.erase(mystruct.begin(), mystruct.end()). OK There must be a better way to do this, right? There must be. Dynamic memory management needs to be the responsibility of the owner of that memory. So, let MyStruct handle its own memory as it should. I looked up remove and remove_if, but they don''t seem to be right for my situation...

他们不是。 V - 请在通过电子邮件回复时删除资金''A' 我没有回复最热门的回复,请不要问

They are not. V -- Please remove capital ''A''s when replying by e-mail I do not respond to top-posted replies, please don''t ask

" Dilip" < RD ***** @ lycos>在消息中写道 news:11 ********************** @ t31g2000cwb.googlegr oups ... "Dilip" <rd*****@lycos> wrote in message news:11**********************@t31g2000cwb.googlegr oups... 我好 在我的项目中我有类似的代码: 注意:BSTR是一个令人憎恶的人,让人想起一些不安的人。 /> COM世界。必须使用 SysAllocString / SysFreeString Windows API分配/解除分配BSTR字符串。 typedef struct tagMyStruct {BSTR somestring; BSTR someotherstring; } MyStruct; vector< MyStruct> my_struct; 在我的应用程序的过程中,我在MyStruct中分配BSTR并将它们填充到向量中。 什么时候摆脱它他们想知道是否有一种方法可以使用_single_ STL算法调用释放BSTR在向量内的每个MyStruct 实例中指向的内存?目前我使用for_each(使用谓词删除BSTR)然后调用my_struct.erase(mystruct.begin(), mystruct.end())的组合。 必须有一个更好的方法来做到这一点,对吧? 我抬头删除并删除_if,但它们似乎不适合我的情况... Howdy I have code similar to this in my project: Note: BSTR is an abomination conjured up some disturbed person in the COM world. BSTR strings must be allocated/deallocated using the SysAllocString/SysFreeString Windows APIs. typedef struct tagMyStruct { BSTR somestring; BSTR someotherstring; } MyStruct; vector<MyStruct> my_struct; over the course of my app, I allocate the BSTRs inside MyStruct and stuff them into the vector. When the time comes to get rid of them I was wondering if there is a way to free the memory pointed to by the BSTR''s in every MyStruct instance inside the vector using a _single_ STL algorithm call? Currently I use a combination of for_each (with a predicate to delete the BSTRs) followed by a call to my_struct.erase(mystruct.begin(), mystruct.end()). There must be a better way to do this, right? I looked up remove and remove_if, but they don''t seem to be right for my situation...

struct MyStruct { BSTR somestring; BSTR someotherstring; MyStruct(/ * params * /) { //分配 } ~MyStruct() { //取消分配 } }; -Mike

struct MyStruct { BSTR somestring; BSTR someotherstring; MyStruct(/* params */) { // allocate } ~MyStruct() { // de-allocate } }; -Mike

Victor Bazarov写道: Victor Bazarov wrote: Dilip写道:请使用C ++定义类型的方式,它更容易: struct MyStruct {BSTR somestring; BSTR someotherstring; }; Dilip wrote: Please use C++ way of defining types, it''s so much easier: struct MyStruct { BSTR somestring; BSTR someotherstring; };

除了那个名为公司政策的达摩克利剑之外,我会这样做。

I would except for that damocle''s sword called "company policy"

矢量<&MYSTRUCT GT; my_struct; 在我的应用程序中,我在MyStruct中分配BSTR并将它们填充到向量中。 vector<MyStruct> my_struct; over the course of my app, I allocate the BSTRs inside MyStruct and stuff them into the vector.

你分配那些吗? BSTR自己?为什么不把它交给MyStruct进行分配呢?你知道,比如,在一个构造函数中,例如......

Do you allocate those BSTR yourself? Why not give it to MyStruct to allocate? You know, like, in a constructor, for example...

看,问题是我简化了这个结构有点太多了。这个 结构用于跨越COM边界,所以我必须把它放在IDL 文件中,一旦我这样做,我不认为我可以开始添加ctors / dtors 和其他东西。

See, the problem is I simplified this struct a little too much. This struct gets used across COM boundaries so I have to put it in a IDL file and once I do that I don''t think I can start adding ctors/dtors and other stuff.

什么时候摆脱它们我想知道是否有<使用_single_STL算法调用,在向量内的每个MyStruct 实例中释放BSTR指向的内存的方法? When the time comes to get rid of them I was wondering if there is a way to free the memory pointed to by the BSTR''s in every MyStruct instance inside the vector using a _single_ STL algorithm call?

定义析构函数MYSTRUCT。让它解除分配那些东西。当然,要遵循三法则,你需要定义复制c-tor和赋值操作。

Define the destructor in MyStruct. Make it deallocate those things. Of course, to follow the Rule of Three, you will need to define the copy c-tor and the assignment op as well.

对这个愚蠢的问题抱歉,但我认为vector.erase会调用所包含对象的 dtor?如果没有,我可能无法使用这个 ,因为矢量在我的应用程序的生命周期内被擦除/插入,并且在这样的擦除期间我希望能够免费在MyStruct的每个实例中分配的 BSTR内存。 IOW我有 设计了一种情况,即矢量的容量保持不变 - 它只是它的大小()收缩&扩展。

Apologies for the silly question but I presume vector.erase will call dtor of the contained objects? If not I may not be able to use this because the vector undergoes erasure/insertion during the lifetime of my application and during such erasure I want to be able to free up the BSTRs memory allocated inside every instance of MyStruct. IOW I have engineered a situation where the capacity of the vector remains intact -- its just its size() that contracts & expands.

我查找了删除和remove_if,但它们似乎不适合我的情况... I looked up remove and remove_if, but they don''t seem to be right for my situation...

他们不是。

They are not.

感谢您的确认。我阅读了Scott Meyers ESTL的33/34项目现在只需 - 我甚至不会靠近它!

Thanks for confirming. I read Items 33/34 of Scott Meyers ESTL just now -- I won''t even go near it!

更多推荐

STL删除算法问题

本文发布于:2023-11-30 11:06:48,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   STL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!