STL 实践(for_each() getline sort random_shuffle的使用)
2007-06-04 13:30
519 查看
#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;
相关文章推荐
- STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation
- STL_Algorithm5-math: random_shuffle, count, count_if, min_element, max_element, accumulate, for_each, transform
- 【C++ STL应用与实现】64: 如何使用shuffle和random_shuffle : 洗牌 (since C++11)
- 从零开始学C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- STL之sort和for_each的用法
- 从零开始学C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- 【C++ STL应用与实现】26: 如何使用std::for_each以及基于范围的for循环 (since C++11)
- STL中的random_shuffle()方法的使用
- (原創) 如何使用for_each() algorithm? (C/C++) (STL)
- STL中的random_shuffle()方法的使用
- (原創) 如何正確的使用迴圈(使用for_each)? (C/C++) (STL) (template)
- 如何正確的使用迴圈(使用for_each)? (C/C++) (STL) (template)
- STL for_each sort
- STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- STL的适配器、仿函数学习之一:accumulate和for_each的使用心得
- STL for_each 和函数对象(function object) 使用详解
- (原創) 如何使用for_each() algorithm? (C/C++) (STL)
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- STL之for_each, find_if,adjacent_find,count_if的使用
- stl for_each使用