1、
#include<iostream>
#include<string>
bool huiwen(const std::string &s);
int main()
{
using namespace std;
cout << "please enter a string: ";
string s;
getline(cin, s);
while (cin&&s.size() > 0)
{
if (huiwen(s))
cout << "it is\n";
else cout << "it is not\n";
cout << "please enter a string(empty string to quit): ";
getline(cin, s);
}
system("pause");
return 0;
}
bool huiwen(const std::string &s)
{
std::string s1(s.rbegin(),s.rend());
return (s1==s);
}
2、
#include<iostream>
#include<string>
#include<cctype>
bool huiwen(const std::string &s);
int main()
{
using namespace std;
cout << "please enter a string: ";
string s;
getline(cin, s);
while (cin&&s.size() > 0)
{
if (huiwen(s))
cout << "it is\n";
else cout << "it is not\n";
cout << "please enter a string(empty string to quit): ";
getline(cin, s);
}
system("pause");
return 0;
}
bool huiwen(const std::string &s)
{
std::string s1;
int j = 0;
for (int i = 0; i < s.size(); i++)
{ if (!(ispunct(s[i]) &&isgraph(s[i]) &&isspace(s[i])&&iscntrl(s[i])))
s1[j++] = islower(s[i]);
}
std::string s2(s1.rbegin(),s1.rend());
return (s1==s2);
}
7、#include<iostream>
#include<algorithm>
#include<vector>
#include<ctime>
using namespace std;
vector<int> lotto(int n1, int n2);
int main()
{
using namespace std;
vector<int> s;
void show(int n);
s = lotto(51, 6);
for_each(s.begin(), s.end(), show);
system("pause");
return 0;
}
vector<int> lotto(int n1,int n2)
{
vector<int> nums;
vector<int>nums1;
for (int i = 0; i < n1; i++)
nums.push_back(i);
random_shuffle(nums.begin(), nums.end());
for (int i = 0; i < n2; i++)
nums1.push_back(nums[rand()%n1+1]);
return nums1;
}
void show(int n)
{
cout << "the winner is: " << n << endl;
}
8、#include<iostream>
#include<algorithm>
#include<set>
#include<iterator>
#include<string>
int main()
{
using namespace std;
ostream_iterator<string, char>out(cout, " ");
string name1;
set<string>Mat;
set<string>Pat;
cout << "please enter the name(empty is quit): \n";
getline(cin, name1);
while(name1!="quit")
{
Mat.insert(name1);
getline(cin, name1);
}
cout << "Mat's friends: \n";
copy(Mat.begin(), Mat.end(), out);
cout << endl;
getline(cin, name1);
while (name1 != "quit")
{
Pat.insert(name1);
getline(cin, name1);
}
cout << "Pat's friends: \n";
copy(Pat.begin(), Pat.end(), out);
cout << endl;
cout << "their friends is: ";
set_union(Mat.begin(), Mat.end(), Pat.begin(), Pat.end(), out);
system("pause");
return 0;
}
9、#include<iostream>
#include<algorithm>
#include<ctime>
#include<iterator>
#include<vector>
#include<list>
const int Size = 10000;
int main()
{
using namespace std;
vector<int> vio(Size);
list<int>li(Size);
srand(time(0));
for (int i = 0; i < Size; i++)
{
vio[i] = rand() % 10000 + 1;
}
vector<int>vi(vio);
copy(vio.begin(), vio.end(), back_inserter(li));
clock_t start = clock();
sort(vi.begin(), vi.end());
clock_t end = clock();
cout << "the vi spend: " << (double)(end - start) / CLOCKS_PER_SEC<<endl;
start = clock();
li.sort();
end = clock();
cout << "the li spend: " << (double)(end - start) / CLOCKS_PER_SEC << endl;
copy(vio.begin(), vio.end(), back_inserter(li));
start = clock();
copy(li.begin(), li.end(), back_inserter(vi));
sort(vi.begin(), vi.end());
copy(vi.begin(), vi.end(), back_inserter(li));
end = clock();
cout << "Time of Sort List2:" << (double)(end - start) / CLOCKS_PER_SEC << endl;
system("pause");
return 0;
}
10、#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <memory>
#include <cstdlib>
using namespace std;
struct Review
{
string title;
int rating;
double price;
};
bool operator<(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2);
bool worseThan(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2);
bool betterThan(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2);
bool worseThanP(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2);
bool betterThanP(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2);
bool FillReview(shared_ptr<Review> &rr);
shared_ptr<Review> make_Review();
void ShowReview(shared_ptr<Review> &rr);
int main()
{
vector<shared_ptr<Review>> books;
shared_ptr<Review> temp(new Review);
while (FillReview(temp))
{
books.push_back(temp);
temp = make_Review();//
}
if (books.size() > 0)
{
vector<shared_ptr<Review>> sbook(books);///??????????
cout << "Thank you. You entered the following:\n"
<< books.size() << " ratings:\n" << "Rating\tBook\tPrice\n";
for_each(books.begin(), books.end(), ShowReview);
char ch;
cout << "Enter measures of sort:\no to old";
cout << "t to title, r to down rating,\nR to up rating"
"p to down price,P to up price, f(F) to shuffle,\nq to quit:";
cin >> ch;
while (tolower(ch) != 'q')
{
switch (ch)
{
case'o':
cout << "Not Sort:\nRating\tBook\tPrice\n";
for_each(books.begin(), books.end(), ShowReview);
break;//如果不重建矢量sbook,则此项会受到其他情况的干扰。
case't':
sort(sbook.begin(), sbook.end());//sort默认使用运算符<
cout << "Sorted by title:\nRating\tBook\tPrice\n";
for_each(sbook.begin(), sbook.end(), ShowReview);
break;
case'r':
sort(sbook.begin(), sbook.end(), worseThan);
cout << "Sorted by down rating:\nRating\tBook\tPrice\n";
for_each(sbook.begin(), sbook.end(), ShowReview);
break;
case'R':
sort(sbook.begin(), sbook.end(), betterThan);
cout << "Sorted by up rating:\nRating\tBook\tPrice\n";
for_each(sbook.begin(), sbook.end(), ShowReview);
break;
case'p':
sort(sbook.begin(), sbook.end(), worseThanP);
cout << "Sorted by down price:\nRating\tBook\tPrice\n";
for_each(sbook.begin(), sbook.end(), ShowReview);
break;
case'P':
sort(sbook.begin(), sbook.end(), betterThanP);
cout << "Sorted by up price:\nRating\tBook\tPrice\n";
for_each(sbook.begin(), sbook.end(), ShowReview);
break;
case'F':
case'f':
random_shuffle(sbook.begin(), sbook.end());
cout << "After shuffling:\nRating\tBook\n";
for_each(sbook.begin(), sbook.end(), ShowReview);
break;
default:
cout << "Error input!Input again!";
break;
}
cout << "Enter the next measures:\no to old";
cout << "t to title, r to down rating,\nR to up rating"
"p to down price,P to up price, q to quit:";
cin >> ch;
}
}
else
cout << "No entries. ";
cout << "Bye.\n";
system("pause");
return 0;
}
bool operator<(const shared_ptr<Review> &r1, const shared_ptr<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 shared_ptr<Review> &r1, const shared_ptr<Review> &r2)
{
if (r1->rating < r2->rating)
return true;
else
return false;
}
bool betterThan(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2)
{
if (r1->rating > r2->rating)
return true;
else
return false;
}
bool worseThanP(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2)
{
if (r1->price < r2->price)
return true;
else
return false;
}
bool betterThanP(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2)
{
if (r1->price > r2->price)
return true;
else
return false;
}
shared_ptr<Review> make_Review()
{
return shared_ptr<Review>(new Review);
}
bool FillReview(shared_ptr<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;
cout << "Enter book price: ";
cin >> rr->price;
if (!cin)
return false;
while (cin.get() != '\n')
continue;
return true;
}
void ShowReview(shared_ptr<Review> &rr)
{
cout << rr->rating << "\t" << rr->title << "\t" << rr->price << endl;
}