删除地图的正确方法 [英] the correct way to delete a map
问题描述
我有一个包含指针的地图。当我破坏地图时我想要b $ b删除
所有指针。
typedef std :: map< std :: string,const T * Table;
void destroy_map()
{
for(Table :: iterator i = table_。 begin(); i!= table_.end(); ++ i)
{
delete(* i).second;
table_.erase(i);
}
}
这次崩溃。擦除()是否使迭代器失效?
这是我在网上找到的代码(latout固定了一下)
for( map :: iterator itr = myMap.begin(); itr!= myMap.end())
{
if(itr-> value == something)
myMap.erase(itr ++);
else
itr ++;
}
据称来自Josuttis。我想这个事实是它没有编译,这让我怀疑这个...
-
Nick Keighley
11月8日,10:55,Nick Keighley< nick_keighley_nos ... @ hotmail.com>
写道:
我有一个包含指针的地图。当我破坏地图时我想要b $ b删除
所有指针。
typedef std :: map< std :: string,const T * Table;
void destroy_map()
{
for(Table :: iterator i = table_。 begin(); i!= table_.end(); ++ i)
{
delete(* i).second;
table_.erase(i);
}
}
这次崩溃。擦除()是否使迭代器失效?
这是我在网上找到的代码(latout固定了一下)
for( map :: iterator itr = myMap.begin(); itr!= myMap.end())
{
if(itr-> value == something)
myMap.erase(itr ++);
else
itr ++;
}
据称来自Josuttis。我想这个事实是它没有编译,这让我怀疑这个...
啊!更多地在网上闲逛。 erase()确实使
迭代器无效。这就是为什么网络代码在擦除
电话中的后期增量。
所以我想要: -
void destroy_map()
{
for(Table :: iterator i = table_.begin(); i!= table_.end();)
{
删除(* i)。秒;
table_.erase(i ++);
}
}
因为这段代码实际上是一个DTOR而table_是成员变量
我会更好地将地图的破坏留给DTOR ?
class Symbol_table
{
public:
Table table_;
~Nature_table();
};
Symbol_table :: ~Vrace_table()
{
for(Table :: iterator i = table_.begin(); i!= table_.end(); ++ i)
delete(* i).second;
}
应该将for循环更改为算法吗? for_each()?
-
Nick Keighley
Nick Keighley写道:
table_.erase(i ++);
不,标准方式是:i = table_.erase(i);
此外,还有无需单独擦除地图元素。只需
释放元素指向的内存然后只清除()地图,
,因为你完全清理它。
Nick Keighley< ni ****************** @ hotmail.comwrote:
我有一个包含指针的地图。当我破坏地图时我想要删除所有指针。
typedef std :: map< std :: string,const T * Table;
void destroy_map()
{
for(Table :: iterator i = table_.begin(); i!= table_ .end(); ++ i)
{
delete(* i).second;
table_.erase(i);
}
}
这次崩溃。 erase()是否使迭代器失效?
是的。试试这个:
void destroy_map()
{
for(Table :: iterator i = table_.begin() ; i!= table_.end(); ++ i)
{
删除i->秒;
i->秒= 0; //我不认为这是绝对必要的......
}
table_.clear();
}
或者如果你想获得算法的乐趣:
typedef map< int,int * Table;
模板< typename T>
void delete_second(T& t){
delete t.second;
t.second = 0;
}
void destroy_map()
{
for_each(table_.begin(),table_.end (),
& delete_second< Table :: value_type);
table_.clear();
}
Hi,
I have a map containing pointers. When I destroy the map I want to
delete
all the pointers.
typedef std::map<std::string, const T*Table;
void destroy_map ()
{
for (Table::iterator i = table_.begin(); i != table_.end(); ++i)
{
delete (*i).second;
table_.erase (i);
}
}
this crashes. Does the erase() invalidate the iterator?
This was the code I found on the net (latout fixed a bit)
for (map::iterator itr = myMap.begin(); itr != myMap.end())
{
if(itr->value == something)
myMap.erase(itr++);
else
itr++;
}
which it is claimed is from Josuttis. I suppose the fact that it
didn''t compile should of made me doubt this...
--
Nick Keighley
On 8 Nov, 10:55, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:Hi,
I have a map containing pointers. When I destroy the map I want to
delete
all the pointers.
typedef std::map<std::string, const T*Table;
void destroy_map ()
{
for (Table::iterator i = table_.begin(); i != table_.end(); ++i)
{
delete (*i).second;
table_.erase (i);
}
}
this crashes. Does the erase() invalidate the iterator?
This was the code I found on the net (latout fixed a bit)
for (map::iterator itr = myMap.begin(); itr != myMap.end())
{
if(itr->value == something)
myMap.erase(itr++);
else
itr++;
}
which it is claimed is from Josuttis. I suppose the fact that it
didn''t compile should of made me doubt this...ah! more poking around on the net. erase() does invalidate the
iterator. That''s why the net code does a post increment in the erase
call.
So preumably I want:-
void destroy_map ()
{
for (Table::iterator i = table_.begin(); i != table_.end();)
{
delete (*i).second;
table_.erase (i++);
}
}
since this code is actually in a DTOR and table_ is member variable
would I be better leaving the destruction of the map to the DTOR?
class Symbol_table
{
public:
Table table_;
~Symbol_table();
};
Symbol_table::~Symbol_table()
{
for (Table::iterator i = table_.begin(); i != table_.end(); ++i)
delete (*i).second;
}
should the for-loop be changed into an algorithm? for_each()?
--
Nick Keighley
Nick Keighley wrote:table_.erase (i++);No, the standard way is: i = table_.erase(i);
Besides, there''s no need to individually erase the map elements. Just
free the memory pointed by the elements and then just clear() the map,
given that you are cleaning it completely.
Nick Keighley <ni******************@hotmail.comwrote:
I have a map containing pointers. When I destroy the map I want to
delete all the pointers.
typedef std::map<std::string, const T*Table;
void destroy_map ()
{
for (Table::iterator i = table_.begin(); i != table_.end(); ++i)
{
delete (*i).second;
table_.erase (i);
}
}
this crashes. Does the erase() invalidate the iterator?Yes. Try this instead:
void destroy_map()
{
for ( Table::iterator i = table_.begin(); i != table_.end(); ++i )
{
delete i->second;
i->second = 0; // I don''t think this is strictly necessary...
}
table_.clear();
}
or if you want to have fun with algorithms:
typedef map<int, int*Table;
template < typename T >
void delete_second( T& t ) {
delete t.second;
t.second = 0;
}
void destroy_map()
{
for_each( table_.begin(), table_.end(),
&delete_second<Table::value_type);
table_.clear();
}
这篇关于删除地图的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!