18.7.4 list大小操作
函数原型:
size();
//判断容器是否为空empty();
//返回容器元素个数resize(num);
//重新指定容器长度为num。
//若容器变长,则以默认值填充新位置;
//若容器变短,则删除超出容器长度的元素。resize(num, ele);
//重新指定容器长度为num。
//若容器变长,则以默认值填充新位置;
//若容器变短,则删除超出容器长度的元素。
示例
#include <iostream>
#include <list>
using namespace std;
template<class T>
void printList(const list<T>& L)
{
for (list<T>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << '\t';
}
cout << endl;
}
void test1()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
if (L1.empty())
{
cout << "L1为空" << endl;
}
else
{
cout << "L1不为空" << endl;
cout << "L1的元素个数为:" << L1.size() << endl;
}
//重新指定大小
L1.resize(10, 10000);
printList(L1);
L1.resize(2);
printList(L1);
}
int main()
{
test1();
}