c++ primer 第十章习题

练习10.1

int main() {
    vector v = {1,1,2,3,3,4,4,4,4,5,5,5};
    cout << count(v.begin(),v.end(),6)<

练习10.2

int main() {
    list v = {"hello", "hello", "how", "are", "are", "you"};
    cout << count(v.begin(),v.end(),"hello")<

练习10.3

int main() {
    vector v = {1,2,3,4,5,6};
    int sum = 0;
    sum = accumulate(v.begin(), v.end(), sum);
	cout << sum <

练习10.4  使用的加法是整数类型的,返回整数。

练习10.5 正确执行。

练习10.6

int main() {
    vector a = {1,2,3,4,5};
    fill_n(a.begin(), a.size(), 0);
    for(auto x : a)
    	cout << x<

练习10.6 fill_n(v.begin, v.size(),0)

练习10.7 (a) 使用back_inserter(vec)做copy的参数   (b)同a

练习10.8 算法不会直接调用容器操作,因此不会直接增删。而调用迭代器进行增删是迭代器的操作而不是算法的。

练习10.9

int main() {
    vector a = {1,2,3,4,6,2,2,2,3,4,5,6,7};
    sort(a.begin(), a.end());
    auto end_unique = unique(a.begin(), a.end());
    for(auto x : a)
    	cout << x << ' ';
    cout << endl;
    a.erase(end_unique, a.end());
    for(auto x : a)
    	cout << x << ' ';
    cout<

练习10.9

void elimDups(vector& vs) {
	sort(vs.begin(), vs.end());
	auto end_unique = unique(vs.begin(), vs.end());
	for(auto x : vs)
		cout << x << ' ';
	cout << endl;
	vs.erase(end_unique, vs.end());
	for(auto x : vs)
		cout << x << ' ';
	cout<

练习10.10 不会调用容器的增删操作。

练习10.11

void elimDups(vector& vs) {
	sort(vs.begin(), vs.end());
	auto end_unique = unique(vs.begin(), vs.end());
	for(auto x : vs)
		cout << x << ' ';
	cout << endl;
	vs.erase(end_unique, vs.end());
	for(auto x : vs)
		cout << x << ' ';
	cout<

练习10.12

bool compareIsbn(const Sales_data&a, const Sales_data&b) {
	return a.isbn() < b.isbn();
}

练习10.13

bool isShorter(const string& a, const string& b) {
	return a.size() < b.size();
}

void elimDups(vector& vs) {
	sort(vs.begin(), vs.end());
	auto end_unique = unique(vs.begin(), vs.end());
	for(auto x : vs)
		cout << x << ' ';
	cout << endl;
	vs.erase(end_unique, vs.end());
	for(auto x : vs)
		cout << x << ' ';
	cout<= 5;
}

int main() {
	vector vs = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
	elimDups(vs);
	stable_sort(vs.begin(), vs.end(), isShorter);
	for(auto x : vs)
		cout << x << ' ';
	cout << endl;
	auto it = partition(vs.begin(), vs.end(),longer);
	for(auto begin = vs.begin(); begin != it; begin++)
		cout << *begin << ' ';
	cout << endl;
	return 0;
}

练习10.14 10.15

int main() {
	int i1 = 2, i2 = 3;
	auto f = [](int i1, int i2){return i1+i2;};
	cout << f(i1, i2) <

练习10.16

void elimDups(vector& vs) {
	sort(vs.begin(), vs.end());
	auto end_unique = unique(vs.begin(), vs.end());
	for(auto x : vs)
		cout << x << ' ';
	cout << endl;
	vs.erase(end_unique, vs.end());
	for(auto x : vs)
		cout << x << ' ';
	cout< & words, vector::size_type sz) {
	elimDups(words);
	stable_sort(words.begin(), words.end(), [](const string&a, const string&b){return a.size()=sz;});
    for_each(wc, words.end(), [](string& s) {cout << s << ' ';});
    cout << endl;
}

练习10.17

sort(v.begin(),v.end(), [](Sales_data&a, Sales_data&b) {return a.isbn()

练习10.18

void bigges(vector & words, vector::size_type sz) {
	elimDups(words);
	stable_sort(words.begin(), words.end(), [](const string&a, const string&b){return a.size()=sz;});
    for_each(words.begin(), wc, [](string& s) {cout << s << ' ';});
    cout << endl;
}

练习10.19 略

练习10.20

int main() {
	vector vs = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
	int c = count_if(vs.begin(), vs.end(), [](string& s){return s.size()>=6;});
	cout << c <

练习10.21

int main() {
	int i1 = 3;
	auto f = [i1]()mutable->bool{if (i1 > 0) return (i1--) == 0;
	                             else return true;};
	cout << f()<

练习10.22

#include 
#include 
#include 
#include 
#include  
#include 

using namespace std::placeholders;
using namespace std;


bool check_size(const string& s, string::size_type sz) {
    return s.size()>=sz;
}

int main() {
	auto f = bind(check_size,_1, 6);
	vector vs = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
	int c = count_if(vs.begin(), vs.end(), f);
	cout << c <

练习10.23 参数是第一个参数本身和第一个参数的形参列表。

练习10.24

int main() {

	string s = "nihaoma";
	vector v = {1,2,3,4,5,6,9,11,2,113,443};
	auto f = bind(check_size,s,_1);
	auto it = find_if_not(v.begin(), v.end(), f);
	cout << *it <

练习10.25

bool check_size(const string&, string::size_type);
void bigges(vector & words, vector::size_type sz) {
	elimDups(words);
	stable_sort(words.begin(), words.end(), [](const string&a, const string&b){return a.size()

练习10.27

void printStrings(vector& vs) {
	for_each(vs.begin(), vs.end(), [](string& s) {cout << s <<' ';});
	cout << endl;
}
int main() {
	vector vs = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
	vector em;
	sort(vs.begin(), vs.end());
	unique_copy(vs.begin(),vs.end(),back_inserter(em));
	printStrings(vs);
	printStrings(em);
	return 0;
}

练习10.28

void printInts(deque& v) {
	for_each(v.begin(), v.end(), [](int& x){cout << x << ' ';});
	cout << endl;
}
int main() {
	// auto f = bind(check_size,_1, 6);
	vector v = {1,2,3,4,5,6,7,8,9};
	deque v1,v2,v3;
	copy(v.begin(), v.end(), inserter(v1,v1.begin()));
	copy(v.begin(), v.end(), back_inserter(v2));
	copy(v.begin(), v.end(), front_inserter(v3));
    printInts(v1);
    printInts(v2);
    printInts(v3);
	return 0;
}

练习10.29

int main() {
	ifstream in("e_9_20.cpp");
	if(in) {
	    istream_iterator in_iter(in), eof;
	    vector vs(in_iter,eof);
	    cout << vs.back()<

练习10.30

int main() {
	istream_iterator in_iter(cin), eof;
    ostream_iterator out_iter(cout, " ");

    vector v(in_iter,eof);
    sort(v.begin(),v.end());
    copy(v.begin(),v.end(),out_iter);
	return 0;
}

练习10.31  sort和copy中间加个unique。

练习10.32

bool compareIsb(Sales_item& a, Sales_item& b) {
	return a.isbn() < b.isbn();
}
int main() 
{
	ifstream in_f("../1/data/book_sales");
	istream_iterator in_iter(in_f), eof;
	ostream_iterator out_iter(cout, "\n");
    if(in_f) {
    	vector v(in_iter, eof);
    	sort(v.begin(), v.end(),compareIsb);
    	copy(v.begin(), v.end(),out_iter);
    	cout << endl;
    	for(auto begin = v.begin(); begin != v.end();) {
    		auto end = find_if(begin, v.end(), [begin](Sales_item& s) {return s.isbn() != begin->isbn();});
    		Sales_item total(begin->isbn());
    		out_iter = accumulate(begin, end, total);
    		begin = end;
    	}
    }else
        cout << "could not open data."<

练习10.33

int main() 
{
	ifstream in_f("data/10_33");
	ofstream out_f1("data/10_33_1"), out_f2("data/10_33_2");
	istream_iterator in_iter(in_f), eof;
	ostream_iterator out_iter1(out_f1, " "), out_iter2(out_f2, "\n");
	int tmp;
    while(in_iter != eof) {
    	tmp = *in_iter++;
    	if (tmp%2)
    		out_iter1 = tmp;
    	else
    		out_iter2 = tmp;
    }
    return 0;
}

练习10.34

int main() 
{
	vector v = {1,2,4,5};
	vector::reverse_iterator iter = v.rbegin(), rend = v.rend();
	while(iter != rend)
		cout << *iter++ << ' ';
    return 0;
}

练习10.35

int main() 
{
	vector v = {1,2,4,5};
	vector::iterator iter = v.end(), end = v.begin();
	while(iter != end)
		cout << *--iter << ' ';
    return 0;
}

练习10.36

int main() 
{
	list v = {1,2,4,5,0,1,2,0,3};
    auto iter = find(v.rbegin(), v.rend(), 0);
    cout << *++iter <

练习10.37

int main() 
{
	vector v = {1,2,4,5,0,1,2,0,3,10};
    list l(v.rbegin()+3,v.rbegin()+7);
    ostream_iterator out_iter(cout, " ");
    copy(l.begin(), l.end(), out_iter);
    return 0;
}

练习10.39 双向迭代器  随机迭代器

练习10.40 copy要求输入和输出两种对两个容器。 reverse和unique要求双向读写。

练习10.41 略

练习10.42

void elimDups(list& ls) {
	ls.sort();
	for_each(ls.begin(), ls.end(),[](string&s){cout << s<<' ';});
	cout << endl;
	ls.unique();
	for_each(ls.begin(), ls.end(),[](string&s){cout << s<<' ';});
	cout<

 

你可能感兴趣的:(C++)