设为首页 加入收藏

TOP

标准模板库(STL)List介绍(三)
2011-06-07 12:31:23 】 浏览:13594
Tags:标准 模板 STL List 介绍
 
  
The list member function empty()list的成员函数empty() 
     知道一个list是否为空很重要。如果list为空,empty()这个成员函数返回真。 我通常会这样使用它。通篇程序我都用push_back()来把错误消息放到list中去。然后,通过调用empty() 我就可以说出这个程序是否报告了错误。如果我定义了一个list来放信息,一个放警告,一个放严重错误, 我就可以通过使用empty()轻易的说出到底有那种类型的错误发生了。  
  
     我可以整理这些list,然后在打印它们之前,用标题来整理它们,或者把它们排序成类。  
  
     这是我的意思:  
  
   
/* 
|| Using a list to track and report program messages and status  
*/ 
#include <iostream.h> 
#include <string> 
#include <list> 
int main (void)  
   #define OK 0  
   #define INFO 1 
   #define WARNING 2 
   int return_code; 
   list<string> InfoMessages; 
   list<string> WarningMessages; 
  
   // during a program these messages are loaded at various points 
   InfoMessages.push_back("Info: Program started"); 
   // do work... 
   WarningMessages.push_back("Warning: No Customer records have been found"); 
   // do work... 
   
   return_code = OK;  
   
   if  (!InfoMessages.empty()) {          // there were info messages 
      InfoMessages.push_front("Informational Messages:"); 
      // ... print the info messages list, we'll see how later 
      return_code = INFO; 
   } 
  
   if  (!WarningMessages.empty()) {       // there were warning messages 
      WarningMessages.push_front("Warning Messages:"); 
      // ... print the warning messages list, we'll see how later 
      return_code = WARNING;               
   } 
  
   // If there were no messages say so. 
   if (InfoMessages.empty() && WarningMessages.empty()) { 
      cout << "There were no messages " << endl; 
   } 
  
   return return_code; 
  
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 3/18/18
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇 C++语言的url encode 和decode 下一篇C++ map的基本操作和使用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目