/*#include<bits/stdc++.h>*/
#include<iostream>
#include<cstdlib>
#include<vector>
#include<string>
#include"windows.h"
#include<algorithm>
using namespace std;
struct book
{
string title;
int date;
};
void Entry(book &b);
void Display(book &b);
bool operator<(const book &b1, const book &b2);
bool downsort(const book &b1, const book &b2);
int main()
{
int n;
cout << "please input the number of book: ";
(cin >> n).get();
vector<book>::iterator pr;
vector<book>read(n);
for (auto &x : read)///*要想改变x的值要用引用
Entry(x);
for_each(read.begin(), read.end(), Display);
cout << "The size of reading book is: " << read.size()<<endl;
book temp = { "苏格拉底没有底", 2012 };
read.push_back(temp);
cout << "**********puch_back()*************" << endl;
for (pr = read.begin(); pr != read.end(); pr++)
Display(*pr);
cout << "The size of reading book is: " << read.size()<<endl;
cout << "**********erase()*****************" << endl;
read.erase(read.begin(), read.begin() + 1);
for_each(read.begin(), read.end(), Display);
cout << "The size of reading book is: " << read.size() << endl;
cout << "**********insert()****************\n";
vector<book>add(2);
for (auto &x : add)
Entry(x);
read.insert(read.end(), add.begin(), add.begin() + 1);
for_each(read.begin(), read.end(), Display);
cout << "The size of reading book is: " << read.size() << endl;
cout << "**********sort()******************\n";
sort(read.begin(), read.end());
cout << "The result of ascending order is: ";
for (auto &x : read)
Display(x);
sort(read.begin(), read.end(),downsort);
cout << "\nThe result of decending order is: ";
for (auto &x : read)
Display(x);
cout << "\n**********random_shuffle()********\n";
random_shuffle(read.begin(), read.end());
cout << "The result of random order is: ";
for (auto &x : read)
Display(x);
system("pause");
return 0;
}
void Entry(book &b)
{
cout << "please input the title of book: ";
getline(cin, b.title);
cout << "please input the date of book: ";
while (!(cin >> b.date)||b.date <0)
{
cin.clear();
cout << "Sorry, the date of book is wrong.\n";
while (cin.get() != '\n')
continue;
cout << "please input the new date of book: ";
}
while (cin.get() != '\n')
continue;
cout << "OK! Entry Done.\n";
}
void Display(book &b)
{
cout << "Title: " << b.title
<< "\nDate: " << b.date << endl;
}
bool operator<(const book &b1, const book &b2)//升序排列
{
if (b1.date < b2.date)
return true;
else if (b1.date == b2.date&&b1.title < b2.title)
return true;
else
return false;
}
bool downsort(const book &b1, const book &b2)
{
if (b1.date > b2.date)
return true;
else if (b1.date == b2.date&&b1.title > b2.title)
return true;
else
return false;
}
运行结果如下图
please input the number of book: 3
please input the title of book: 大世界
please input the date of book: 平凡的世界
Sorry, the date of book is wrong.
please input the new date of book: 1992
OK! Entry Done.
please input the title of book: 平凡的世界
please input the date of book: 1982
OK! Entry Done.
please input the title of book: 致青春
please input the date of book: 2012
OK! Entry Done.
Title: 大世界
Date: 1992
Title: 平凡的世界
Date: 1982
Title: 致青春
Date: 2012
The size of reading book is: 3
**********puch_back()*************
Title: 大世界
Date: 1992
Title: 平凡的世界
Date: 1982
Title: 致青春
Date: 2012
Title: 苏格拉底没有底
Date: 2012
The size of reading book is: 4
**********erase()*****************
Title: 平凡的世界
Date: 1982
Title: 致青春
Date: 2012
Title: 苏格拉底没有底
Date: 2012
The size of reading book is: 3
**********insert()****************
please input the title of book: 落日黄昏
please input the date of book: 2010
OK! Entry Done.
please input the title of book: 青春
please input the date of book: 1980
OK! Entry Done.
Title: 平凡的世界
Date: 1982
Title: 致青春
Date: 2012
Title: 苏格拉底没有底
Date: 2012
Title: 落日黄昏
Date: 2010
The size of reading book is: 4
**********sort()******************
The result of ascending order is: Title: 平凡的世界
Date: 1982
Title: 落日黄昏
Date: 2010
Title: 苏格拉底没有底
Date: 2012
Title: 致青春
Date: 2012
The result of decending order is: Title: 致青春
Date: 2012
Title: 苏格拉底没有底
Date: 2012
Title: 落日黄昏
Date: 2010
Title: 平凡的世界
Date: 1982
**********random_shuffle()********
The result of random order is: Title: 致青春
Date: 2012
Title: 苏格拉底没有底
Date: 2012
Title: 平凡的世界
Date: 1982
Title: 落日黄昏
Date: 2010
请按任意键继续. . .