习题10.1:
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string;
using std::count;
int main()
{
vector ivec;
int i, val;
cout << "input vector series:" << endl;
while (cin >> i)
ivec.push_back(i);
cin.clear();
cout << "input the value u want to count:" << endl;
cin >> val;
auto times = count(ivec.cbegin(), ivec.cend(), val);
cout << val << " occurs " << times << " times." << endl;
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
using std::count;
int main()
{
list slst;
string s, val;
cout << "input list of string:" << endl;
while (cin >> s)
slst.push_back(s);
cin.clear();
cout << "input the value u want to count:" << endl;
cin >> val;
auto times = count(slst.cbegin(), slst.cend(), val);
cout << val << " occurs " << times << " times." << endl;
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
//#include
#include
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
int main()
{
vector ivec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int sum = std::accumulate(ivec.cbegin(), ivec.cend(), 0);
cout << sum << endl;
getchar();
getchar();
return 0;
}
习题10.6:
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
int main()
{
vector ivec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::fill_n(ivec.begin(), ivec.size(), 0);
for (auto i : ivec)
cout << i << " ";
cout << endl;
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
void print(const vector &svec)
{
for (const auto &s : svec)
cout << s << " ";
cout << endl;
}
void elimDups(vector &svec)
{
print(svec);
std::sort(svec.begin(), svec.end());
print(svec);
auto end_unique = std::unique(svec.begin(), svec.end());
print(svec);
svec.erase(end_unique, svec.end());
print(svec);
}
int main()
{
vector svec;
string s;
while (cin >> s)
svec.push_back(s);
cin.clear();
elimDups(svec);
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
void print(const vector &svec)
{
for (const auto &s : svec)
cout << s << " ";
cout << endl;
}
void elimDups(vector &svec)
{
print(svec);
std::sort(svec.begin(), svec.end());
print(svec);
auto end_unique = std::unique(svec.begin(), svec.end());
print(svec);
svec.erase(end_unique, svec.end());
print(svec);
}
bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
int main()
{
vector svec;
string s;
while (cin >> s)
svec.push_back(s);
cin.clear();
elimDups(svec);
std::stable_sort(svec.begin(), svec.end(), isShorter);
print(svec);
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
bool compareIsbn(const Sales_data &sd1, const Sales_data &sd2)
{
int flag = sd1.isbn().compare(sd2.isbn());
if (flag == -1)
return true;
else
return false;
}
int main()
{
vector sdvec{ Sales_data("0-123-45678-0"),
Sales_data("0-123-45678-9"), Sales_data("0-123-45378-9"),
Sales_data("0-123-45678-5"), Sales_data("0-126-45678-5") };
std::stable_sort(sdvec.begin(), sdvec.end(), compareIsbn);
for (auto &s : sdvec)
cout << s.isbn() << endl;
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
bool five(const string &s)
{
return s.size() >= 5;
}
void print(vector &words)
{
auto true_end = std::partition(words.begin(), words.end(), five);
for (auto beg = words.begin(); beg != true_end;++beg)
cout << *beg << " ";
cout << endl;
}
int main()
{
vector svec;
string s;
while (cin >> s)
svec.push_back(s);
cin.clear();
print(svec);
getchar();
getchar();
return 0;
}
auto add = [](int a, int b){return a + b; };
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
void elimDups(vector &words)
{
std::sort(words.begin(), words.end());
auto end_unique = std::unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
string make_plural(unsigned count, const string &word, const string &post)
{
if (count > 1)
return word + post;
else
return word;
}
void biggies(vector &words, vector::size_type sz)
{
elimDups(words);
std::stable_sort(words.begin(), words.end(),
[](const string &s1, const string &s2){return s1.size() < s2.size(); });
auto iter = find_if(words.begin(), words.end(),
[sz](const string &s){return s.size() >= sz; });
auto count = words.end() - iter;
cout << count << " " << make_plural(count, "word", "s") <<
" of length " << sz << " or longer." << endl;
std::for_each(iter, words.end(), [](const string &s){cout << s << " "; });
cout << endl;
}
int main()
{
vector svec{ "a", "red", "kitty", "bites", "a", "red", "turtle" };
biggies(svec, 3);
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
int main()
{
vector sdvec{ Sales_data("0-123-45678-0"),
Sales_data("0-123-45678-9"), Sales_data("0-123-45378-9"),
Sales_data("0-123-45678-5"), Sales_data("0-126-45678-5") };
std::stable_sort(sdvec.begin(), sdvec.end(),
[](const Sales_data &sd1, const Sales_data &sd2){
int flag = sd1.isbn().compare(sd2.isbn());
if (flag == -1)
return true;
else
return false;
});
for (auto &s : sdvec)
cout << s.isbn() << endl;
getchar();
getchar();
return 0;
}
习题10.18:
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
void elimDups(vector &words)
{
std::sort(words.begin(), words.end());
auto end_unique = std::unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
string make_plural(unsigned count, const string &word, const string &post)
{
if (count > 1)
return word + post;
else
return word;
}
void biggies(vector &words, vector::size_type sz)
{
elimDups(words);
std::stable_sort(words.begin(), words.end(),
[](const string &s1, const string &s2){return s1.size() < s2.size(); });
auto iter = std::partition(words.begin(), words.end(),
[sz](const string &s){return s.size() < sz; });
auto count = words.end() - iter;
cout << count << " " << make_plural(count, "word", "s") <<
" of length " << sz << " or longer." << endl;
std::for_each(iter, words.end(), [](const string &s){cout << s << " "; });
cout << endl;
}
int main()
{
vector svec{ "a", "red", "kitty", "bites", "a", "red", "turtle" };
biggies(svec, 3);
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
vector::size_type biggies(vector &words, vector::size_type sz)
{
return std::count_if(words.cbegin(), words.cend(),
[sz](const string &s){return s.size() > sz; });
}
int main()
{
vector svec{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
cout << biggies(svec, 6) << endl;
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
int main()
{
vector svec{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
int i = 8;
auto f = [&i]()->bool{return --i ? false : true; };
while (!f())
cout << i << endl;
cout << i << endl;
getchar();
getchar();
return 0;
}
习题10.22:
#include
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
using std::placeholders::_1;
bool check_size(const string &s, vector::size_type sz)
{
return s.size() <= sz;
}
int main()
{
vector svec{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
auto wc = count_if(svec.cbegin(), svec.cend(), bind(check_size, _1, 6));
cout << wc << endl;
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
using std::placeholders::_1;
bool check_size(const string &s, const string &s_given)
{
return s.size() > s_given.size();
}
int main()
{
vector svec{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
auto first = std::find_if(svec.cbegin(), svec.cend(), bind(check_size, _1, "hah"));
cout << *first << endl;
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
using std::placeholders::_1;
void elimDups(vector &words)
{
std::sort(words.begin(), words.end());
auto end_unique = std::unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
string make_plural(unsigned count, const string &word, const string &post)
{
if (count > 1)
return word + post;
else
return word;
}
inline auto check_size(const string &s, vector::size_type sz)->bool
{
return s.size() < sz;
}
void biggies(vector &words, vector::size_type sz)
{
elimDups(words);
std::stable_sort(words.begin(), words.end(),
[](const string &s1, const string &s2){return s1.size() < s2.size(); });
auto iter = std::partition(words.begin(), words.end(),
bind(check_size, _1, sz));
auto count = words.end() - iter;
cout << count << " " << make_plural(count, "word", "s") <<
" of length " << sz << " or longer." << endl;
std::for_each(iter, words.end(), [](const string &s){cout << s << " "; });
cout << endl;
}
int main()
{
vector svec{ "a", "red", "kitty", "bites", "a", "red", "turtle" };
biggies(svec, 3);
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
using std::placeholders::_1;
int main()
{
vector svec{ "a", "red", "kitty", "bites", "a", "a", "red", "turtle." };
list slst;
std::unique_copy(svec.cbegin(), svec.cend(), std::inserter(slst, slst.begin()));
for (auto s : slst)
cout << s << " ";
cout << endl;
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
using std::placeholders::_1;
int main()
{
std::ifstream in("火焰杯.txt");
std::istream_iterator string_iter(in);
std::istream_iterator string_eof;
vector svec(string_iter, string_eof);
for (auto &s : svec)
cout << s << " ";
cout << endl;
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "Sales_data.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
using std::placeholders::_1;
int main()
{
std::istream_iterator in_iter(cin), eof;
std::ostream_iterator out_iter(cout," ");//加空格
vector ivec(in_iter, eof);//while(in_iter!=eof)ivec.push_back(*in_iter++);
std::sort(ivec.begin(), ivec.end(),
[](const int &a, const int &b)->bool{return a < b; });//lambda
std::copy(ivec.cbegin(), ivec.cend(), out_iter);
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "Sales_item.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
using std::placeholders::_1;
void read(std::istream &is, vector &item_vec)
{
std::istream_iterator item_iter(is), eof;
std::copy(item_iter, eof, std::inserter(item_vec, item_vec.begin()));
}
bool compare_Isbn(const Sales_item &item1, const Sales_item &item2)//头文件里有重名,加了个下划线区别
{
return (-1 == item1.isbn().compare(item2.isbn())) ? true : false;
}
int main(){
std::cout << "input several sale records & print:" << std::endl;
vector item_vec, item_solved;
read(cin, item_vec);
std::sort(item_vec.begin(), item_vec.end(), compare_Isbn);
for (auto beg = item_vec.begin(), end = beg; beg != item_vec.end(); beg = end)
{
end = std::find_if(beg, item_vec.end(),
[beg](const Sales_item &item){return item.isbn() != beg->isbn(); });
*beg = std::accumulate(beg, end, Sales_item(beg->isbn()));
item_solved.push_back(*beg);
}
for (auto s : item_solved)
cout << s << endl;
getchar();
getchar();
return 0;
}
习题10.34-35:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "Sales_item.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
using std::placeholders::_1;
int main(){
vector ivec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (auto iter = ivec.crbegin(); iter != ivec.crend(); ++iter)
cout << *iter << " ";
cout << endl;
for (auto iter = ivec.back(); iter != ivec.front()-1; --iter)
cout << iter << " ";
cout << endl;
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "Sales_item.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
using std::placeholders::_1;
int main(){
list ilst{ 1, 2, 0, 3, 4, 5, 6, 7, 8, 9 };
auto iter_zero = find(ilst.crbegin(), ilst.crend(), 0);
//cout << *iter_zero << endl;
cout << std::distance(iter_zero, ilst.crend());
cout << endl;
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "Sales_item.h"
using std::cin; using std::cout; using std::endl; using std::flush;
using std::vector; using std::string; using std::list;
using std::placeholders::_1;
int main(){
vector ivec{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
list ilst(std::find(ivec.crbegin(), ivec.crend(), 7),
++std::find(ivec.crbegin(), ivec.crend(), 3));
for (auto i : ilst)
cout << i << " ";
cout << endl;
getchar();
getchar();
return 0;
}