// TimerTest.cpp : Using STL functions
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Review
{
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(Review &rr);
int _tmain(int argc, _TCHAR* argv[])
{
vector<Review> books;
Review temp;
while(FillReview(temp))
books.push_back(temp);
cout<<"Thank you.You entered the following."<<books.size()<<" ratings:\n"<<"Rating\tBook\n";
for_each(books.begin(),books.end(),ShowReview);
sort(books.begin(),books.end());
cout<<"Sorted by title:\nRating \t Book\n";
for_each(books.begin(),books.end(),ShowReview);
sort(books.begin(),books.end(),worseThan);
cout<<"Sorted by rating\tBook\n";
for_each(books.begin(),books.end(),ShowReview);
random_shuffle(books.begin(),books.end());
cout<<"After shuffle rating\tBook\n";
for_each(books.begin(),books.end(),ShowReview);
cout<<"Bye.\n";
system("pause");
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 false;
else
return true;
}
bool FillReview(Review &rr)
{
cout<<"Enter book title(quit to quit):";
getline(cin,rr.title);
if(rr.title == "quit")
return false;
cout<<"Enter book rating:";
cin>>rr.rating;
if(!std::cin)
return false;
cin.get();
return true;
}
void ShowReview(Review &rr)
{
cout<<rr.rating<<"\t"<<rr.title<<endl;
}
// ostream_iterator for stream cout
// ostream_iterator<int> intOut ( cout , "\n" );
ostream_iterator<int> intOut( cout, "\n");
*intOut = 10;
intOut++;
*intOut = 20;
// intOut++;
int i;
vector<int> vec;
for ( i = 1 ; i < 7 ; ++i )
{
vec.push_back ( i );
}
// Write elements to standard output stream
cout << "Elements output without delimiter: ";
copy ( vec.begin ( ), vec.end ( ),
ostream_iterator<int> ( cout ) );
cout << endl;
// Write elements with delimiter " : " to output stream
cout << "Elements output with delimiter: ";
copy ( vec.begin ( ), vec.end ( ),
ostream_iterator<int> ( cout, " : " ) );
cout << endl;
system("pause");
//iterator 简单应用
string s1[4]={"fine","fish","fashion","fate"};
string s2[2]={"busy","bates"};
string s3[2]={"silly","singlers"};
vector<string> words(4);
copy(s1,s1+4,words.begin());
ostream_iterator<string,char> out(cout," ");
copy(words.begin(),words.end(),out);
cout<<endl;
//construct anonymous back_insert_iterator object
copy(s2,s2+2,back_insert_iterator<vector<string>> (words));
copy(words.begin(),words.end(),out);
cout<<endl;
//construct anonymous insert_iterator object
copy(s3,s3+2,insert_iterator<vector<string>> (words,words.begin()));
copy(words.begin(),words.end(),out);
// TimerTest.cpp : Using STL functions
// next_permutation算法:将区间内容转换为下一种排列方式
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
using namespace std;
/**
程序入口
*/
void Show(double);
const int LIM = 5;
int _tmain(int argc, _TCHAR* argv[])
{
string letters;
while(cin>>letters && letters != "quit")
{
cout<<"Permutations of "<<letters<<endl;
sort(letters.begin(),letters.end());
cout<<letters<<endl;
while(next_permutation(letters.begin(),letters.end()))
cout<<letters<<endl;
cout<<"Enter next sequence(quit to quit)";
}
system("pause");
}