list类的使用 |
您所在的位置:网站首页 › 怎么定义一个list › list类的使用 |
的成员函数 empty() 判断 list 是否为空
if (cList.empty()) {
printf(“this list is empty”);
} 4) 、用 list::iterator 得到指向 list 的指针
list::iterator charIterator; for(cIterator = cList.Begin();cIterator != cList.end();cIterator++) {
printf(“%c”, *cIterator);
} // 输出 list 中的所有对象
说明: cList.Begin() 和 cList.end() 函数返回指向 list::iterator 的指针, 由于 list 采用链表结构,因此它不支持随机存取,因此不能用 cList.begin()+3 来指向 list 中 的第四个对象, vector 和 deque 支持随机存取。
5 )、用 STL 的通用算法 count() 来统计 list 中的元素个数
int cNum; char ch = ’b’;
cNum = count(cList.Begin(), cList.end(), ch); // 统计 list 中的字符 b 的个数
说明:在使用 count() 函数之前必须加入 #include
6 )、用 STL 的通用算法 count_if () 来统计 list 中的元素个数
const char c(‘c’);
class IsC { public: bool operator() ( char& ch ) { return ch== c; |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |