删除列表中的条目 [英] deleting entries in a list

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

问题描述



假设我有一个字符串列表;

我必须迭代字符串并删除某些条目。

I并且不确定如何做到这一点?


例如:

#include< list>

#include< string>

#include< iostream>

int main(){

list< string> l;

l.push_back(" first");

l.push_back(" second");

l.push_back( " deleteme");

l.push_back(" 4th");

l.push_back(" deleteme");

l.push_back(" six");

//我想删除说deleteme的条目


我可以得到和迭代器,但是搞砸事情

list< string> :: iterator it = l.begin();

for(; it!= l.end(); it ++){

if((* it).compare(" deleteme")== 0){

l.erase(it);

}

}

}


这给我分段错误


我想当我删除第一个deleteme时

它现在指向第四;


基本上,这就是我想要的。

我将有一个一些对象的巨大列表。

我想一直遍历并删除符合特定条件的
对象。

任何帮助都会非常感谢。

感谢

Hi,
suppose I have a list of strings;
I have to iterate of the strings and delete certain entries.
I and not sure how to do this?

for example:
#include<list>
#include<string>
#include<iostream>
int main(){
list<string> l;
l.push_back("first");
l.push_back("second");
l.push_back("deleteme");
l.push_back("fourth");
l.push_back("deleteme");
l.push_back("sixth");
// I want to erase entries that say "deleteme"

I can get and iterator, but that screws up things
list<string>::iterator it=l.begin();
for( ; it!=l.end(); it++){
if( (*it).compare("deleteme")==0){
l.erase(it);
}
}
}

This gives me segmentation fault

I think that when I delete the first "deleteme" the
it now points to "fourth";

Basically, this is what I want.
I will have a huge list of some object.
I want to traverse it all the way and delete
objects that meet a certain criteria.
any help will be much appreciated.
thanks

推荐答案

* Christoff Pale:
* Christoff Pale:

假设我有一个字符串列表;
我必须迭代字符串并删除某些条目。
我不知道如何做到这一点?

例如:
#include< list>
#include< string>
#include< iostream>
int main(){
list< string> l; p /> l.push_back(" first");
l.push_back(" second");
l.push_back(" deleteme");
l。 push_back(" 4th");
l.push_back(" deleteme");
l.push_back(" six");
//我要删除说的条目; deleteme"

我可以得到和迭代器,但这搞砸了事情
list< string> :: iterator it = l.begin();
for(;它! = l.end(); it ++){
if((* it).compare(" deleteme")== 0){
l.erase(it);
}
}

这给了我分段错误

suppose I have a list of strings;
I have to iterate of the strings and delete certain entries.
I and not sure how to do this?

for example:
#include<list>
#include<string>
#include<iostream>
int main(){
list<string> l;
l.push_back("first");
l.push_back("second");
l.push_back("deleteme");
l.push_back("fourth");
l.push_back("deleteme");
l.push_back("sixth");
// I want to erase entries that say "deleteme"

I can get and iterator, but that screws up things
list<string>::iterator it=l.begin();
for( ; it!=l.end(); it++){
if( (*it).compare("deleteme")==0){
l.erase(it);
}
}
}

This gives me segmentation fault



#include< algorithm>

#include < iostream>

#include< list>

#include< string>


int main()

{

typedef std :: list< std :: string> StringList;

typedef StringList :: iterator Iterator;


StringList l;

l.push_back(" first");

l.push_back(" second");

l.push_back(" deleteme");

l.push_back("第四个);

l.push_back(" deleteme");

l.push_back(" six");


for(Iterator it = l.begin(); it!= l.end();)

{

it-> compare(" deleteme" )== 0?它= l.erase(it):++ it;

}


std :: copy(

l.begin (),l.end(),

std :: ostream_iterator< std :: string>(std :: cout," \ n")

);

}


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和e-最令人烦恼的是什么?邮件?


#include <algorithm>
#include <iostream>
#include <list>
#include <string>

int main()
{
typedef std::list<std::string> StringList;
typedef StringList::iterator Iterator;

StringList l;
l.push_back( "first" );
l.push_back( "second" );
l.push_back( "deleteme" );
l.push_back( "fourth" );
l.push_back( "deleteme" );
l.push_back( "sixth" );

for( Iterator it = l.begin(); it != l.end(); )
{
it->compare( "deleteme" ) == 0? it = l.erase( it ) : ++it;
}

std::copy(
l.begin(), l.end(),
std::ostream_iterator<std::string>( std::cout, "\n" )
);
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?




" Christoff Pale" < CH ************ @ yahoo.com>在消息中写道

news:73 ************************** @ posting.google.c om ...

"Christoff Pale" <ch************@yahoo.com> wrote in message
news:73**************************@posting.google.c om...

假设我有一个字符串列表;
我必须迭代字符串并删除某些条目。
我不知道如何做到这一点例如:
#include< list>
#include< string>
#include< iostream>
int main(){
list< string>升;


std :: list< std :: string>升; / *我*讨厌*那个标识符! :-) * /

l.push_back(" first");
l.push_back(" second");
l.push_back(" deleteme") ;
l.push_back(" 4th");
l.push_back(" deleteme");
l.push_back(" six");
//我想要删除说deleteme的条目

我可以得到和迭代器,但这搞砸了事情
list< string> :: iterator it = l.begin();


std :: list< std :: string> :: iterator it = l.begin();

for(; it!= l.end ();它++){
if((* it).compare(" deleteme")== 0){
l.erase(it);
}
}
}

这给了我分段错误


当你删除迭代器所指向的元素时,

那个迭代器变得无效。然后你尝试增加

无效迭代器。未定义的行为。

我认为当我删除第一个deleteme时它现在指向第四;


没有它指向第六个行星盘旋的外月

Alpha Seti。但那只是今天。明天它会在其他地方点上



std :: list :: erase()返回一个指定

的迭代器第一个元素超出任何元素被移除,

或end()如果不存在这样的元素。所以当你擦除时,

不要递增''它'',给它指定返回值

'' erase()''。

基本上,这就是我想要的。
我会有一个巨大的对象列表。
我想一直遍历它并删除符合某个标准的对象。
任何帮助将不胜感激。
Hi,
suppose I have a list of strings;
I have to iterate of the strings and delete certain entries.
I and not sure how to do this?

for example:
#include<list>
#include<string>
#include<iostream>
int main(){
list<string> l;
std::list<std::string> l; /* I *hate* that identifier! :-) */
l.push_back("first");
l.push_back("second");
l.push_back("deleteme");
l.push_back("fourth");
l.push_back("deleteme");
l.push_back("sixth");
// I want to erase entries that say "deleteme"

I can get and iterator, but that screws up things
list<string>::iterator it=l.begin();
std::list<std::string>::iterator it = l.begin();
for( ; it!=l.end(); it++){
if( (*it).compare("deleteme")==0){
l.erase(it);
}
}
}

This gives me segmentation fault
When you delete an element to which the iterator points,
that iterator becomes invalidated. Then you try to increment
that invalid iterator. Undefined behavior.

I think that when I delete the first "deleteme" the
it now points to "fourth";
No it points to the outer moon of sixth planet circling
Alpha Seti. But that''s just today. Tomorrow it would
point somewhere else.

std::list::erase() returns an iterator that designates
the first element remaining beyond any elements removed,
or end() if no such element exists. So when you erase,
don''t increment ''it'', assign it the return value from
''erase()''.

Basically, this is what I want.
I will have a huge list of some object.
I want to traverse it all the way and delete
objects that meet a certain criteria.
any help will be much appreciated.




#include< iostream>

#include< list>

#include< string>


int main()

{

std :: list< std ::串GT; l;

l.push_back(" first");

l.push_back(" second");

l.push_back( " deleteme");

l.push_back(" 4th");

l.push_back(" deleteme");

l.push_back(" six");


std :: cout<< l.size()<< \\\
; //打印6


for(std :: list< std :: string> :: iterator it = l.begin(); it!= l.end(); it ++)

if((* it).compare(" deleteme")== 0)

it = l.erase(it);


std :: cout<< l.size()<< \\\
; //打印4

返回0;

}


也查询''std :: list :: remove_if() ''


-Mike



#include<iostream>
#include<list>
#include<string>

int main()
{
std::list<std::string> l;
l.push_back("first");
l.push_back("second");
l.push_back("deleteme");
l.push_back("fourth");
l.push_back("deleteme");
l.push_back("sixth");

std::cout << l.size() << ''\n''; // prints 6

for(std::list<std::string>::iterator it = l.begin(); it!=l.end(); it++)
if((*it).compare("deleteme") == 0)
it = l.erase(it);

std::cout << l.size() << ''\n''; // prints 4
return 0;
}

Also look up ''std::list::remove_if()''

-Mike


" Mike Wahler" < MK ****** @ mkwahler.net>在消息中写道

新闻:TR ***************** @ newsread1.news.pas.earthl ink.net ...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:TR*****************@newsread1.news.pas.earthl ink.net...

for(std :: list< std :: string> :: iterator it = l.begin(); it!= l.end();
it ++)if((* it ).compare(" deleteme")== 0)
它= l.erase(it);

for(std::list<std::string>::iterator it = l.begin(); it!=l.end(); it++) if((*it).compare("deleteme") == 0)
it = l.erase(it);




这不太对。它产生一个有效的迭代器,

但是''for''循环会增加它,在擦除后的项目上将跳过

。请参阅Alf'的帖子

以获得正确的方法。


抱歉错误。


-Mike



This isn''t quite right. It produces a valid iterator,
but the ''for'' loop increments it, which will skip
over the item after the erased one. See Alf''s post
for a proper way to do this.

Sorry for the mistake.

-Mike


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

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