stl操作4

容器大小的操作

 c.size()      c.max_size()     c.empty()     c.resize(n)     c.resize(n,t)

注意:resize操作可能会是迭代器失效


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

using namespace std;

int main( int argc, char ** argv )
{
	list<int> lst;
	lst.push_back(10);
	lst.push_back(20);
	lst.push_back(30);

	cout<<"size: "<<lst.size()<<endl;
	
	list<int>::size_type count = lst.size();
	cout<<"size_type size: "<<count<<endl;

	cout<<"max_size: "<<lst.max_size()<<endl;

	if( not lst.empty() )
	{
		cout<<"not empty"<<endl;
	}
	else
	{
		cout<<"empty"<<endl;
	}

	lst.resize(10);
	for(list<int>::iterator itr = lst.begin(); itr!=lst.end(); ++itr )
	{
		cout<<*itr<<endl;
	}

	lst.resize(20,-1);
	for(list<int>::iterator itr = lst.begin(); itr != lst.end(); ++itr )
	{
		cout<<*itr<<endl;
	}

	lst.resize(2);
	for(list<int>::iterator itr = lst.begin(); itr != lst.end(); ++itr )
	{
		cout<<*itr<<endl;
	}
	return 0;
}


posted @ 2015-02-28 14:39  SandKing  阅读(2)  评论(0)    收藏  举报  来源