概述
申明,本文非笔者原创,原文转载自:http://www.cppblog.com/bujiwu/archive/2014/04/24/90087.html
正确版本:
1
#include
<
iostream
>
2 #include < list >
3 #include < algorithm >
4 using namespace std;
5
6 int main( int argc, char * argv[])
7 {
8 list < int > MyList;
9
10 for ( int i = 0 ; i < 10 ; i ++ )
11 {
12 MyList.push_back(i);
13 }
14
15 list < int > ::iterator Itor;
16
17 for ( Itor = MyList.begin(); Itor != MyList.end(); )
18 {
19 if ( * Itor == 4 )
20 {
21 Itor = MyList.erase(Itor);
22 }
23 else
24 {
25 Itor ++ ;
26 }
27 }
28
29 copy(MyList.begin(), MyList.end(), ostream_iterator < int > (cout, " " ) );
30 cout << endl;
31
32 return 0 ;
33 }
2 #include < list >
3 #include < algorithm >
4 using namespace std;
5
6 int main( int argc, char * argv[])
7 {
8 list < int > MyList;
9
10 for ( int i = 0 ; i < 10 ; i ++ )
11 {
12 MyList.push_back(i);
13 }
14
15 list < int > ::iterator Itor;
16
17 for ( Itor = MyList.begin(); Itor != MyList.end(); )
18 {
19 if ( * Itor == 4 )
20 {
21 Itor = MyList.erase(Itor);
22 }
23 else
24 {
25 Itor ++ ;
26 }
27 }
28
29 copy(MyList.begin(), MyList.end(), ostream_iterator < int > (cout, " " ) );
30 cout << endl;
31
32 return 0 ;
33 }
错误版本:
1
#include
<
iostream
>
2 #include < list >
3 #include < algorithm >
4 using namespace std;
5
6 int main( int argc, char * argv[])
7 {
8 list < int > MyList;
9
10 for ( int i = 0 ; i < 10 ; i ++ )
11 {
12 MyList.push_back(i);
13 }
14
15 list < int > ::iterator Itor;
16
17 for ( Itor = MyList.begin(); Itor != MyList.end(); Itor ++ )
18 {
19 if ( * Itor == 4 )
20 {
21 MyList.erase(Itor); //断链,出错地方
22 }
23 }
24
25 copy(MyList.begin(), MyList.end(), ostream_iterator < int > (cout, " " ) );
26 cout << endl;
27
28 return 0 ;
29 }
2 #include < list >
3 #include < algorithm >
4 using namespace std;
5
6 int main( int argc, char * argv[])
7 {
8 list < int > MyList;
9
10 for ( int i = 0 ; i < 10 ; i ++ )
11 {
12 MyList.push_back(i);
13 }
14
15 list < int > ::iterator Itor;
16
17 for ( Itor = MyList.begin(); Itor != MyList.end(); Itor ++ )
18 {
19 if ( * Itor == 4 )
20 {
21 MyList.erase(Itor); //断链,出错地方
22 }
23 }
24
25 copy(MyList.begin(), MyList.end(), ostream_iterator < int > (cout, " " ) );
26 cout << endl;
27
28 return 0 ;
29 }
最后
以上就是苹果星星为你收集整理的从STL中的list删除元素的全部内容,希望文章能够帮你解决从STL中的list删除元素所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复