STL 实践(for_each() getline sort random_shuffle的使用)

#include  < iostream >
#include 
< string >
#include 
< fstream >
#include 
< vector >
#include 
< algorithm >

using   namespace  std;

struct  Review  {
    std::
string title;
    
int rating;
}
;

bool   operator < ( const  Review  &  r1,  const  Review  &  r2);
bool  worseThan( const  Review  &  r1,  const  Review  &  r2);
// bool FillReview(Review & rr);
void  ShowReview( const  Review  &  rr);
bool  FillReview(Review  &  rr, ifstream  & fin);


int  main()
{


    ifstream fin;
    fin.open(
"test.txt");
    
if(!fin.is_open())
    
{
        cerr
<<"can't open the file!"<< endl;
        
return 0;
    }


    vector
<Review> books;
    Review temp;
    
while (FillReview(temp,fin))
        books.push_back(temp);

    fin.close();
    cout 
<< "Thank you. You entered the following "
         
<< books.size() << " ratings: "
          
<< "Rating Book ";
    for_each(books.begin(), books.end(), ShowReview);

    sort(books.begin(), books.end());
    cout 
<< "Sorted by title: Rating Book ";
    for_each(books.begin(), books.end(), ShowReview);

    sort(books.begin(), books.end(), worseThan);
    cout 
<< "Sorted by rating: Rating Book ";
    for_each(books.begin(), books.end(), ShowReview);

    random_shuffle(books.begin(), books.end());
    cout 
<< "After shuffling: Rating Book ";
    for_each(books.begin(), books.end(), ShowReview);
    cout 
<< "Bye. ";
    
return 0;
}


bool   operator < ( const  Review  &  r1,  const  Review  &  r2)
{
    
if (r1.title < r2.title)
        
return true;
    
else if (r1.title == r2.title && r1.rating < r2.rating)
        
return true;
    
else
        
return false;
}


bool  worseThan( const  Review  &  r1,  const  Review  &  r2)
{
    
if (r1.rating < r2.rating)
        
return true;
    
else
        
return false;
}


bool  FillReview(Review  &  rr)
{
    std::cout 
<< "Enter book title (quit to quit): ";
    std::getline(std::cin,rr.title);
    
if (rr.title == "quit")
        
return false;
    std::cout 
<< "Enter book rating: ";
    std::cin 
>> rr.rating;
    
if (!std::cin)
        
return false;
    std::cin.
get();
    
return true;
}


void  ShowReview( const  Review  &  rr)
{
    std::cout 
<< rr.rating << " " << rr.title << std::endl; 
}


bool  FillReview(Review  &  rr,ifstream  & fin)
{
    
if(!fin)
        
return false;
    getline(fin,rr.title,
';');
    
if(rr.title == "")
        
return false;
    
string rating;


    getline(fin,rating,
';');
    rr.rating 
= atoi(rating.c_str());

    
return true;
}

 

for_each()函数可用来替换for循环
vector<Review>::iterator pr;
for(pr = books.begin(); pr!=books.end(); pr++)
 showReview(*pr);

替换为:
for_each(book.begin(),book.end(),ShowReview);

ShowReview原型
void ShowReview(const Review& rr)
最后一个参数是函数对象,它不改变参数的值。

test.txt

abc;100;bdf;39;ffff;453;zzz;789;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值