原创 使用序列式容器并分析字符串.cpp

2009-3-16 13:45 2484 5 5 分类: 软件与OS

#include "Student_info.h"
#include "grade.h"
#include <vector>
#include <list>


using std::string;
using std::vector;


// predicate to determine whether a student failed
bool fgrade(const Student_info& s)
{
 return grade(s) < 60;
}


//separate passing and failing student records: first try
vector<Student_info> extract_fails(vector<Student_info>& students)
{
 vector<Student_info> pass, fail;
 for(vector<Student_info>::size_type i = 0;
  i != students.size(); ++i) {
   if (fgrade(students))
    fail.push_back(students);
   else
    pass.push_back(students);
 }
 students = pass;
 return fail;
}
///////////////////////////////////////////////////////////////////////////


#include "Student_info.h"
#include "grade.h"
#include <vector>
#include <list>


using std::string;
using std::vector;


// predicate to determine whether a student failed
bool fgrade(const Student_info& s)
{
 return grade(s) < 60;
}



// second try:correct but potentially slow
vector<Student_info> extract_fails(vector<Student_info>& students)
{
 vector<Student_info> fail;
 for(vector<Student_info>::size_type i = 0;
  i != students.size(); ) {
   if (fgrade(students)) {
    fail.push_back(students);
    students.erase(students.begin() + i);
   }
   else
    ++ i;
 }
 return fail;
}
/////////////////////////////////////////////////////////////////


#include "Student_info.h"
#include "grade.h"
#include <vector>
#include <list>


using std::string;
using std::vector;


// predicate to determine whether a student failed
bool fgrade(const Student_info& s)
{
 return grade(s) < 60;
}



// version 3:iterators but no indexing: still potentially slow
vector<Student_info> extract_fails(vector<Student_info>& students)
{
 vector<Student_info> fail;
 vector<Student_info>::iterator iter = students.begin();
 while (iter != students.end()) {
   if (fgrade(*iter)) {
    fail.push_back(*iter);
    iter = students.erase(iter);
   }
   else
    ++ iter;
 }
 return fail;
}
////////////////////////////////////////////////////////////////


#include "Student_info.h"
#include "grade.h"
#include <vector>
#include <list>


using std::string;
using std::list;


// predicate to determine whether a student failed
bool fgrade(const Student_info& s)
{
 return grade(s) < 60;
}


// version 4: use list instead of vector
list<Student_info> extract_fails(list<Student_info>& students)
{
 list<Student_info> fail;
 list<Student_info>::iterator iter = students.begin();
 while (iter != students.end()) {
   if (fgrade(*iter)) {
    fail.push_back(*iter);
    iter = students.erase(iter);
   }
   else
    ++ iter;
 }
 return fail;
}


//////////////////////////////////////////////////////

文章评论0条评论)

登录后参与讨论
我要评论
0
5
关闭 站长推荐上一条 /2 下一条