STL顺序容器操作6

删除元素

    c.erase(p)    c.erase(b,e)   //包括前面,不包括后面     c.clear()   c.pop_back()      c.pop_front()

注意:c.pop_front()  只适用于 list和deque容器


#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <string>
#include <stdexcept>

using namespace std;

int main( int argc, char ** argv )
{
	list<string> lst;

	lst.push_back("apple");
	lst.push_back("bill");
	lst.push_back("cat");
	lst.push_back("dog");
	lst.push_back("egg");
	lst.push_back("fish");
	lst.push_back("girl");

	
	lst.pop_front();
	lst.pop_back();


	string s("dog");
	string s2("fish");

	list<string>::iterator itr = find(lst.begin(), lst.end(), s );
	list<string>::iterator itr2 = find(lst.begin(), lst.end(), s2 );
	if( itr != lst.end() and itr2 != lst.end() )
	{
		//lst.erase(itr);
		lst.erase( itr, itr2 );
	}
	else
	{
		cout<<"not find"<<endl;
	}

	lst.clear();
	//slt.erase(slt.begin(), slt.end());
	if( !lst.empty() )
	{
		for( list<string>::iterator itr = lst.begin(); itr!=lst.end(); ++itr )
		{
			cout<<*itr<<endl;
		}
	}
	else
	{
		cout<<"lst is empty"<<endl;
	}
	return 0;
}


posted @ 2015-02-28 17:44  SandKing  阅读(4)  评论(0)    收藏  举报  来源