//10.1
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
vector<int> vec = { 1,1,1,2,3,4,5,6,6,7,7,8 };
unsigned cont = 0;
cont = count(vec.begin(), vec.end(), 1);
cout << cont << endl;
return 0;
}
//10.2
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<list>
#include<fstream>
using namespace std;
int main(int argc,char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open cerr" << endl;
exit(1);
}
list<string> lst;
string word;
while (in >> word)
{
lst.push_back(word);
}
cin >> word;
cout << word <<"出现:" << count(lst.begin(), lst.end(), word) << " 次" << endl;
return 0;
}
//10.3
#include<iostream>
#include<numeric>
#include<vector>
using namespace std;
int main()
{
vector<int> vec = { 1,2,3,4,5,6,7,8,9,10 };
int sum = 0;
sum = accumulate(vec.begin(), vec.end(), 0);
cout << sum << endl;
return 0;
}
//10.6
#include<iostream>
#include<algorithm>
#include<vector>
#include<fstream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open cerr" << endl;
exit(1);
}
vector<int> vec;
int word;
while (in >> word)
{
vec.push_back(word);
}
fill_n(vec.begin(), vec.size(), 0);
for (auto tem : vec)
{
cout << tem << " ";
}
return 0;
}
//10.9
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
//删除重复元素
void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open cerr" << endl;
exit(1);
}
vector<string> str;
string word;
while (in >> word)
{
str.push_back(word);
}
elimDups(str);
for (auto tem : str)
{
cout << tem << " ";
}
return 0;
}
//10.12
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
//删除重复元素
void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open cerr" << endl;
exit(1);
}
vector<string> str;
string word;
while (in >> word)
{
str.push_back(word);
}
elimDups(str);
stable_sort(str.begin(), str.end(), isShorter);
for (auto tem : str)
{
cout << tem << " ";
}
return 0;
}
//10.12
#pragma once
#include<string>
#include<iostream>
using namespace std;
class Sales_data {
friend istream &read(istream &is, Sales_data &item);
friend ostream &print(ostream &os, Sales_data &item);
public:
//受委托构造函数,4个参数
Sales_data(const string &book, int num, double sell, double sale) :
bookNo(book), units_sold(num), sellprice(sell), saleprice(sale) {
if (sellprice)
discount = saleprice / sellprice;
cout << "该函数输入个参数" << endl;
}
Sales_data() :Sales_data("", 0, 0, 0)
{
cout << " 该函数不输入任何参数,执行默认实参" << endl;
}
Sales_data(const string &book) :Sales_data(book, 0, 0, 0)
{
cout << "该函数输入一个参数" << endl;
}
Sales_data(istream &is) :Sales_data()
{
read(is, *this);
cout << "该函数输入book对象" << endl;
}
private:
string bookNo;//书型号
int units_sold = 0;//销售数量
double sellprice = 0.0;//进价
double saleprice = 0.0;//售价
double discount = 0.0;//折扣
public:
Sales_data &combine(const Sales_data &rhs);
string isbn() const { return bookNo; }
};
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
if (units_sold)
{
saleprice = (rhs.sellprice*rhs.units_sold + units_sold*sellprice) / (units_sold + rhs.units_sold);
}
else
saleprice = 0;
if (sellprice)
discount = saleprice / sellprice;
return *this;
}
istream &read(istream &is, Sales_data &item)
{
is >> item.bookNo >> item.units_sold >> item.sellprice >> item.saleprice;
return is;
}
ostream &print(ostream &os, Sales_data &item)
{
os << item.bookNo << " " << item.units_sold << " " << item.sellprice << " " << item.saleprice << item.discount << endl;
return os;
}
Sales_data add(const Sales_data& lhs, const Sales_data& rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
#include<algorithm>
#include<vector>
#include<fstream>
#include"Sales_data.h"
using namespace std;
bool compareIsbn(const Sales_data &s1, const Sales_data &s2)
{
return s1.isbn() < s2.isbn();
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open cerr" << endl;
exit(1);
}
vector<Sales_data> str;
Sales_data word;
while (read(in,word))
{
str.push_back(word);
}
sort(str.begin(), str.end(), compareIsbn);
for (auto &tem : str)
{
print(cout,tem);
cout << endl;
}
return 0;
}
//9.13
#include<algorithm>
#include<vector>
#include<fstream>
#include"Sales_data.h"
using namespace std;
void out_str(vector<string>::iterator beg, vector<string>::iterator end)
{
for (beg;beg < end;beg++)
{
cout << *beg << " ";
}
}
bool More5(const string &s)
{
return s.size() >= 5;
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open cerr" << endl;
exit(1);
}
vector<string> str;
string word;
while (in>>word)
{
str.push_back(word);
}
cout << "Output all string:" << endl;
out_str(str.begin(), str.end());
auto it = partition(str.begin(), str.end(),More5);
cout << endl<<"Output more or equal 5 string:" << endl;
out_str(str.begin(), it);
return 0;
}
//10.14
//返回两个int的和,用lambda
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int val1, val2;
cout << "Please input 2 int numbers:" << endl;
cin >> val1>>val2;
auto sum = [](int a,int b){return a + b;};
cout << "The sum of 2 numbers is:" << sum(val1,val2) << endl;
return 0;
}
//10.15
//返回两个int的和,用lambda
#include<iostream>
#include<algorithm>
using namespace std;
void sum(int a)
{
int val;
cout << "Please input 1 int numbers:" << endl;
cin >> val;
auto sum = [a](int b) {return a + b;};
cout << "The sum of 2 numbers is:" << sum(val) << endl;
}
int main()
{
sum(2);
return 0;
}
//10.16
//按长度排序字符串
#include<iostream>
#include<algorithm>
#include<numeric>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
//删除重复元素,并从新排序
void elimDups(vector<string> &s)
{
sort(s.begin(), s.end());//按字典顺序排列
auto end_unique = unique(s.begin(), s.end());
s.erase(end_unique, s.end());
}
string make_plural(size_t ctr,const string &word,const string &ending)
{
return (ctr>1) ? word + ending:word;
}
//按长度排序,找出长度要求的字符串
void biggest(vector<string> &s, size_t sz)
{
elimDups(s); //删除重复元素,并从新排序
stable_sort(s.begin(), s.end(), [](const string &s1, const string &s2) {return s1.size() < s2.size();});//按照长度由短到长排序
auto wc = find_if(s.begin(), s.end(), [sz](const string &s) {return s.size() >= sz;});//找出第一个指向长度大于等于sz的元素指向的迭代器。
auto cont = s.end() - wc;//长度大于sz的字符串有几个
cout << "共有" << cont <<" "<< make_plural(cont, "word", "s") << "length more " << sz << endl;
for_each(wc, s.end(), [](const string &s) {cout << s << " ";});//谓词函数操作的是容器的元素
}
int main(int argc,char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open erro" << endl;
exit(1);
}
vector<string> vec;
string word;
while (in >> word)
{
vec.push_back(word);
}
biggest(vec, 5);
return 0;
}
//10.17
#include<algorithm>
#include<vector>
#include<fstream>
#include"Sales_data.h"
using namespace std;
bool compareIsbn(const Sales_data &s1, const Sales_data &s2)
{
return s1.isbn() < s2.isbn();
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open cerr" << endl;
exit(1);
}
vector<Sales_data> str;
Sales_data word;
while (read(in, word))
{
str.push_back(word);
}
sort(str.begin(), str.end(), [](const Sales_data &s1, const Sales_data &s2) {return s1.isbn() < s2.isbn();});
for (auto &tem : str)
{
print(cout, tem);
cout << endl;
}
return 0;
}
//10.18
//10.18
//按长度排序字符串
#include<iostream>
#include<algorithm>
#include<numeric>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
//删除重复元素,并从新排序
void elimDups(vector<string> &s)
{
sort(s.begin(), s.end());//按字典顺序排列
auto end_unique = unique(s.begin(), s.end());
s.erase(end_unique, s.end());
}
string make_plural(size_t ctr, const string &word, const string &ending)
{
return (ctr>1) ? word + ending : word;
}
//按长度排序,找出长度要求的字符串
void biggest(vector<string> &s, size_t sz)
{
elimDups(s); //删除重复元素,并从新排序
stable_sort(s.begin(), s.end(), [](const string &s1, const string &s2) {return s1.size() < s2.size();});//按照长度由短到长排序
auto wc = partition(s.begin(), s.end(), [sz](const string &s) {return s.size() >= sz;});
auto cont = wc - s.begin();//长度大于sz的字符串有几个
cout << "共有" << cont << " " << make_plural(cont, "word", "s") << "length more " << sz << endl;
for_each(s.begin(),wc, [](const string &s) {cout << s << " ";});//谓词函数操作的是容器的元素
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open erro" << endl;
exit(1);
}
vector<string> vec;
string word;
while (in >> word)
{
vec.push_back(word);
}
biggest(vec, 5);
return 0;
}
//10.19
//10.18
//按长度排序字符串
#include<iostream>
#include<algorithm>
#include<numeric>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
//删除重复元素,并从新排序
void elimDups(vector<string> &s)
{
sort(s.begin(), s.end());//按字典顺序排列
auto end_unique = unique(s.begin(), s.end());
s.erase(end_unique, s.end());
}
string make_plural(size_t ctr, const string &word, const string &ending)
{
return (ctr>1) ? word + ending : word;
}
//按长度排序,找出长度要求的字符串
void biggest(vector<string> &s, size_t sz)
{
elimDups(s); //删除重复元素,并从新排序
stable_sort(s.begin(), s.end(), [](const string &s1, const string &s2) {return s1.size() < s2.size();});//按照长度由短到长排序
auto wc = stable_partition(s.begin(), s.end(), [sz](const string &s) {return s.size() >= sz;});
auto cont = wc - s.begin();//长度大于sz的字符串有几个
cout << "共有" << cont << " " << make_plural(cont, "word", "s") << "length more " << sz << endl;
for_each(s.begin(),wc, [](const string &s) {cout << s << " ";});//谓词函数操作的是容器的元素
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open erro" << endl;
exit(1);
}
vector<string> vec;
string word;
while (in >> word)
{
vec.push_back(word);
}
biggest(vec, 5);
return 0;
}
//10.20
//按长度排序字符串
#include<iostream>
#include<algorithm>
#include<numeric>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
//删除重复元素,并从新排序
void elimDups(vector<string> &s)
{
sort(s.begin(), s.end());//按字典顺序排列
auto end_unique = unique(s.begin(), s.end());
s.erase(end_unique, s.end());
}
string make_plural(size_t ctr, const string &word, const string &ending)
{
return (ctr>1) ? word + ending : word;
}
//按长度排序,找出长度要求的字符串
void biggest(vector<string> &s, size_t sz)
{
elimDups(s); //删除重复元素,并从新排序
stable_sort(s.begin(), s.end(), [](const string &s1, const string &s2) {return s1.size() < s2.size();});//按照长度由短到长排序
auto wc = count_if(s.begin(), s.end(), [sz](const string &s) {return s.size() >= sz;});
//auto cont = wc - s.begin();//长度大于sz的字符串有几个
cout << "共有" << wc << " " << make_plural(wc, "word", "s") << "length more " << sz << endl;
//for_each(s.begin(),wc, [](const string &s) {cout << s << " ";});//谓词函数操作的是容器的元素
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open erro" << endl;
exit(1);
}
vector<string> vec;
string word;
while (in >> word)
{
vec.push_back(word);
}
biggest(vec, 6);
return 0;
}
//10.20
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int i = 5;
auto f = [i]()mutable->bool{if (i > 0) { i--;return false; }
else return true;};
for (int j = 0;j < 6;j++)
{
cout << f() << " ";
}
cout << endl;
return 0;
}
//10.22
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<string>
#include<functional>
using namespace std;
using namespace std::placeholders;
bool check_size(const string &s, size_t sz)
{
return s.size() <= sz;
}
int main(int argc,char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open erro" << endl;
exit(1);
}
vector<string> vec;
string str;
while (in >> str)
vec.push_back(str);
auto wc = count_if(vec.begin(), vec.end(), bind(check_size, _1, 6));
cout << wc << endl;
}
//10.24
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<string>
#include<functional>
using namespace std;
using namespace std::placeholders;
bool check_size(const string &s, size_t sz)
{
return s.size() <= sz;
}
void more_int(vector<int> vec,const string &s)
{
auto p = find_if(vec.begin(), vec.end(), bind(check_size, s, _1));
cout << "第" << p - vec.begin() + 1 << "个参数" << *p << "大于" << s << "d的长度" << endl;
}
int main(int argc,char *argv[])
{
vector<int> vec = { 1,2,3,4,5,6,7,8 };
more_int(vec, "ffa");
more_int(vec, "fasdj");
return 0;
}
//10.25
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<string>
#include<functional>
using namespace std;
using namespace std::placeholders;
//删除重复元素,并从新排序
void elimDups(vector<string> &s)
{
sort(s.begin(), s.end());//按字典顺序排列
auto end_unique = unique(s.begin(), s.end());
s.erase(end_unique, s.end());
}
string make_plural(size_t ctr, const string &word, const string &ending)
{
return (ctr>1) ? word + ending : word;
}
//按长度排序,找出长度要求的字符串
bool check_size(const string &s, size_t sz)
{
return s.size() >= sz;
}
void biggest(vector<string> &s, size_t sz)
{
elimDups(s); //删除重复元素,并从新排序
stable_sort(s.begin(), s.end(), [](const string &s1, const string &s2) {return s1.size() < s2.size();});//按照长度由短到长排序
auto wc = partition(s.begin(), s.end(), bind(check_size,_1,sz));
auto cont = wc - s.begin();//长度大于sz的字符串有几个
cout << "共有" << cont << " " << make_plural(cont, "word", "s") << "length more " << sz << endl;
for_each(s.begin(), wc, [](const string &s) {cout << s << " ";});//谓词函数操作的是容器的元素
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open erro" << endl;
exit(1);
}
vector<string> vec;
string word;
while (in >> word)
{
vec.push_back(word);
}
biggest(vec, 5);
return 0;
}
//10.27
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<string>
#include<list>
#include<iterator>
using namespace std;
int main()
{
vector<string> vec = { "hello","nihao","nihao","girl","boy","boy" };
list<string> lst;
unique_copy(vec.cbegin(), vec.cend(), back_inserter(lst));
for (auto tem : lst)
{
cout << tem << " ";
}
cout << endl;
return 0;
}
//10.28
#include<iostream>
#include<vector>
#include<list>
#include<algorithm>
#include<iterator>
using namespace std;
int main()
{
vector<int> vec = { 1,2,3,4,5,6,7,8,9 };
list<int> vec1, vec2, vec3;
copy(vec.cbegin(), vec.cend(), back_inserter(vec1));
copy(vec.cbegin(), vec.cend(), front_inserter(vec2));
copy(vec.cbegin(), vec.cend(), inserter(vec3,vec3.begin()));
cout << "vec1:" << endl;
for (auto tem : vec1)
{
cout << tem << " ";
}
cout <<endl<< "vec2:" << endl;
for (auto tem : vec2)
{
cout << tem << " ";
}
cout << endl<<"vec3:" << endl;
for (auto tem : vec3)
{
cout << tem << " ";
}
cout << endl;
return 0;
}
//10.29
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<vector>
#include<iterator>
using namespace std;
int main(int argc,char *argv[])
{
ifstream in(argv[1]);
istream_iterator<string> in_it(in),eof;
ostream_iterator<string> out_iter(cout, " ");
vector<string> vec(in_it,eof);
for (auto tem : vec)
out_iter = tem;
cout << endl;
return 0;
}
//10.30
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
istream_iterator<int> in_it(cin),eof;
ostream_iterator<int> out_iter(cout, " ");
vector<int> vec(in_it,eof);
while (in_it != eof)
vec.push_back(*in_it++);
sort(vec.begin(), vec.end());
copy(vec.begin(), vec.end(), out_iter);
cout << endl;
return 0;
}
//10.31
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
istream_iterator<int> in_it(cin),eof;
ostream_iterator<int> out_iter(cout, " ");
vector<int> vec(in_it,eof);
while (in_it != eof)
vec.push_back(*in_it++);
sort(vec.begin(), vec.end());
unique_copy(vec.begin(), vec.end(), out_iter);
cout << endl;
return 0;
}
//10.32
#include<iostream>
#include<algorithm>
#include<numeric>
#include<vector>
#include<iterator>
#include"Sales_data.h"
using namespace std;
int main()
{
istream_iterator<Sales_data> in_it(cin),eof;
vector<Sales_data> vec(in_it,eof);
while (in_it != eof)
vec.push_back(*in_it++);
if (!vec.empty())
{
cerr << "no data" << endl;
return -1;
}
sort(vec.begin(), vec.end(),compareIsbn);
auto it = vec.begin();
while (it != vec.end())
{
auto data = *it;
auto r = find_if(it + 1, vec.end(), [data](const Sales_data &data1) {return data.isbn() != data1.isbn();});
cout << accumulate(it + 1, r, data);
it = r;
}
return 0;
}
//10.34
#include<iostream>
#include<algorithm>
#include<fstream>
#include<vector>
#include<iterator>
using namespace std;
int main(int argc,char *argv[])
{
if (argc != 2)
{
cerr << "Input erro" << endl;
return -1;
}
ifstream in(argv[1]);
if (!in)
{
cerr << "Input file erro" << endl;
exit(1);
}
vector<int> vec;
int vi;
while (in >> vi)
vec.push_back(vi);
for (auto it = vec.rbegin();it != vec.rend();++it)
{
cout << *it << " ";
}
cout << endl;
return 0;
}
//10.35
#include<iostream>
#include<algorithm>
#include<fstream>
#include<vector>
#include<iterator>
using namespace std;
int main(int argc,char *argv[])
{
if (argc != 2)
{
cerr << "Input erro" << endl;
return -1;
}
ifstream in(argv[1]);
if (!in)
{
cerr << "Input file erro" << endl;
exit(1);
}
vector<int> vec;
int vi;
while (in >> vi)
vec.push_back(vi);
reverse(vec.begin(), vec.end());
for (auto tem:vec)
{
cout << tem << " ";
}
cout<<endl;
return 0;
}
//10.36
#include<iostream>
#include<algorithm>
#include<fstream>
#include<list>
#include<iterator>
using namespace std;
int main(int argc,char *argv[])
{
list<int> lst = {0,12,3,4,5,0,3,4,0,4,5};
auto it = find(lst.crbegin(), lst.crend(), 0);
it++;
unsigned count = 1;
for (auto iter = lst.begin();iter != it.base();iter++, count++);
if (count >= lst.size())
cerr << "没有找到0" << endl;
else
cout << "最后一个0的位置为:第" << count << "个元素" << endl;
return 0;
}
//10.42
//10.42
#include<iostream>
#include<algorithm>
#include<list>
#include<string>
#include<fstream>
using namespace std;
//删除重复元素
void elimDups(list<string> &words)
{
words.sort();
words.unique();
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cerr << "The file open cerr" << endl;
exit(1);
}
list<string> str;
string word;
while (in >> word)
{
str.push_back(word);
}
elimDups(str);
for (auto tem : str)
{
cout << tem << " ";
}
return 0;
}