C++ Primer 学习笔记 第十章 泛型算法

336 find函数

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;int main() {int val = 42;vector<int> vec = {30, 32, 22};auto result = find(vec.cbegin(), vec.cend(), val);cout << ((result == vec.cend()) ? "not found" : "found") << endl;// not foundint ia[] = {26, 22, 24, 53};int val1 = 24;int* result1 = find(begin(ia), end(ia),val1);cout << *result1 << endl;return 0;}

10.1 count函数

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;int main() {int t, n;vector<int> vec;cout << "enter numbers of val: " << endl;cin >> n;for (int i=0; i < n; i++){cin >> t;vec.push_back(t);}for (int tmp:vec){cout << tmp << " ";}cout << endl;cout << "enter a number to be counted: " << endl;int num;cin >> num;cout << count(vec.begin(), vec.end(), num) << endl;return 0;
}

338 accumulate

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <numeric>
#include <string>
using namespace std;int main() {vector<int> vec1 = {1, 2, 3, 4, 5};int sum = accumulate(vec1.cbegin(), vec1.cend(), 0);cout << sum << endl; // 15vector<string> vec2 = {"a", "b", "c"};cout << accumulate(vec2.cbegin(), vec2.cend(), string("")) << endl; //abcreturn 0;
}

339 equal

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <numeric>
#include <string>
using namespace std;int main() {vector<string> vec1 = {"1", "2"};
//    const char *p = "fsfsd";vector<const char*> vec2 = {"1", "2"};auto result = equal(vec1.cbegin(), vec1.cend(), vec2.cbegin());cout << result << endl; //1return 0;
}

340 fill fill_n

#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;int main() {vector<int> vec(10, 2);fill(vec.begin(), vec.end() - vec.size()/2, 0); // half of elements filled by 0for (int item : vec){cout << item << " ";}cout << endl;fill_n(vec.begin(), 2,5); // first two elements filled by 5for (int item : vec){cout << item << " ";}cout << endl;return 0;}

342 copy

#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;int main() {int a1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};int a2[sizeof(a1)/ sizeof(*a1)];auto ret = copy(begin(a1), end(a1), a2);for (auto item:a2){cout << item << " ";}cout << endl;cout << *(ret -1) << endl; //9return 0;}

342 replace replace_copy

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;int main() {vector<int> ivec = {0, 1, 2, 0};replace(ivec.begin(), ivec.end(), 0, 4);for (int item : ivec){cout << item << " ";}cout << endl;list<int> lst;replace_copy(ivec.begin(), ivec.end(), back_inserter(lst), 4, 5);for (int item : lst){cout << item << " ";}cout << endl;}

10.6

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;int main() {vector<int> ivec = {0, 1, 2, 0};fill_n(ivec.begin(), ivec.size(), 6);for (int item : ivec){cout << item << endl;}}

343 sort erase

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;// 343
void elimDups(vector<string> &words){sort(words.begin(), words.end());for (auto s:words){cout << s << " ";}cout << endl;auto end_unique = unique(words.begin(), words.end());for (auto s:words){cout << s << " ";}cout << endl;words.erase(end_unique, words.end());for (auto s:words){cout << s << " ";}cout << endl;
}int main() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};elimDups(vec);/*fox jumps over quick red red slow the the turtlefox jumps over quick red slow the turtle thefox jumps over quick red slow the turtle*/
}

10.9

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;// 10.9
int main(){vector<string> ivec;string s;for (int num=0; num < 5; ++num){cin >> s;ivec.push_back(s);}for (string item:ivec){cout << item << " ";}cout << endl;sort(ivec.begin(), ivec.end());auto uni_end = unique(ivec.begin(), ivec.end());ivec.erase(uni_end, ivec.end());for (string item:ivec){cout << item << " ";}cout << endl;return 0;
}

344 自定义排序函数

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;bool isShorter(const string &s1, const string &s2){return s1.size() < s2.size();
}void elimDups(vector<string> &words){sort(words.begin(), words.end(), isShorter);for (auto s:words){cout << s << " ";}cout << endl;// the red fox the red over slow quick jumps turtle
}int main() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};elimDups(vec);/*the red fox the red over slow quick jumps turtle*/
}

345 保持原有的顺序上面进行排序stable_sort

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;bool isShorter(const string &s1, const string &s2){return s1.size() < s2.size();
}void elimDups(vector<string> &words){sort(words.begin(), words.end());for (auto s:words){cout << s << " ";}cout << endl;auto end_unique = unique(words.begin(), words.end());for (auto s:words){cout << s << " ";}cout << endl;words.erase(end_unique, words.end());for (auto s:words){cout << s << " ";}cout << endl;stable_sort(words.begin(), words.end(), isShorter);for (auto s:words){cout << s << " ";}cout << endl;
}int main() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};elimDups(vec);/*fox jumps over quick red red slow the the turtlefox jumps over quick red slow the turtle thefox jumps over quick red slow the turtlefox red the over slow jumps quick turtle*/
}

10.11

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;bool isShorter(const string &s1, const string &s2){return s1.size() < s2.size();
}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() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};elimDups(vec);for (auto s:vec){cout << s << " ";}cout << endl;stable_sort(vec.begin(),vec.end(), isShorter);for (auto s:vec){cout << s << " ";}cout << endl;/** 后面添加的排序原则越优先,即先按照长短,再按照字母去排序* fox jumps over quick red slow the turtle* fox red the over slow jumps quick turtle*/
}

10.13

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;bool str_part(string &s){return s.size() >= 5;}int main() {vector<string> words = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };auto loc = partition(words.begin(), words.end(), str_part);cout << "loc " << *loc << endl;for (string item:words){cout << item << " ";}cout << endl;/** loc foxturtle quick jumps fox red over the slow red the* */return 0;
}

346 lambda

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;int main() {auto f = [] {return 42;};// lambda functioncout << f() << endl;return 0;}

347 lambda应用

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;bool isShorter(const string &s1, const string &s2){return s1.size() < s2.size();
}void elimDups(vector<string> &words){sort(words.begin(), words.end(), isShorter);for (auto s:words){cout << s << " ";}cout << endl;// the red fox the red over slow quick jumps turtle
}int main() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};elimDups(vec);/*the red fox the red over slow quick jumps turtle*/
}

349 使用lambda完成完整例子

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;void elimDups(vector<string> &words){sort(words.begin(), words.end());auto unqi = unique(words.begin(), words.end());words.erase(unqi, words.end());
}void biggies(vector<string> &words, vector<string>::size_type sz){elimDups(words);stable_sort(words.begin(), words.end(), [](const string &a, const string &b){return a.size()<b.size();});for (auto val:words){cout << val << " ";}cout << endl;// fox red the over slow jumps quick turtleauto wc = find_if(words.begin(), words.end(), [sz](const string &a){return a.size()>=sz;});auto count = words.end() - wc;cout << count << endl; // 5  5 elements with length >=4for_each(wc, words.end(),[](const string &s){cout << s << " ";});cout << endl;// over slow jumps quick turtle}int main() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};biggies(vec, 4);}

10.14

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;int main() {auto f = [](int a, int b){return a+b;};cout << f(1, 1) << endl;}

10.15

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;int main() {int num1 = 1;auto f = [num1](int num2){return num1+num2;};cout << f(3) << endl;}

10.18

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;void elimDups(vector<string> &words){sort(words.begin(), words.end());auto unqi = unique(words.begin(), words.end());words.erase(unqi, words.end());
}void biggies(vector<string> &words, vector<string>::size_type sz){elimDups(words);stable_sort(words.begin(), words.end(), [](const string &a, const string &b){return a.size()<b.size();});for (auto val:words){cout << val << " ";}cout << endl;// fox red the over slow jumps quick turtle//    auto wc = find_if(words.begin(), words.end(), [sz](const string &a){return a.size()>=sz;});
//    auto count = words.end() - wc;auto wc = partition(words.begin(), words.end(), [sz](const string &a){return a.size()<sz;});auto count = words.end() - wc;cout << count << endl; // 5  5 elements with length >=4for_each(wc, words.end(),[](const string &s){cout << s << " ";});cout << endl;// over slow jumps quick turtle}int main() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};biggies(vec, 4);}

350 lambda函数值捕获

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;int main() {size_t v1 = 42;// 普通捕获,copy一份,和函数局部变量无关,互不影响auto f = [v1] {return v1;};v1 = 0; cout << f() << endl; // 42// 引用捕获,影响原来的变量auto f2 = [&v1] {return v1;};v1 = 1;cout << f2() << endl; // 1}

350 捕获引用是有必要的,因为os对象是不能拷贝的

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;void elimDups(vector<string> &words){sort(words.begin(), words.end());auto unqi = unique(words.begin(), words.end());words.erase(unqi, words.end());
}void biggies(vector<string> &words, vector<string>::size_type sz, ostream &os=cout, char c=' '){elimDups(words);stable_sort(words.begin(), words.end(), [](const string &a, const string &b){return a.size()<b.size();});for (auto val:words){cout << val << " ";}cout << endl;// fox red the over slow jumps quick turtleauto wc = find_if(words.begin(), words.end(), [sz](const string &a){return a.size()>=sz;});auto count = words.end() - wc;cout << count << endl; // 5  5 elements with length >=4for_each(wc, words.end(),[&os, c](const string &s){os << s << c;});os << endl;// over slow jumps quick turtle}int main() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};biggies(vec, 4);}

352 可变lambda

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;int main() {size_t v1 = 42;// v1不可变
//    auto f = [v1]() {return ++v1;};auto f = [v1]()mutable {return ++v1;};v1 = 0;auto j= f();cout << j << endl;}

353 引用捕获不用mutable

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;int main() {size_t v1 = 42;// 引用捕获不需要加mutable,自己会根据原来变量是否有const去判断是否能够修改auto f = [&v1]()   {return ++v1;};v1 = 0;auto j= f();cout << j << endl;}

10.20

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};auto num = count_if(vec.begin(), vec.end(), [](string s){return s.size()>3;});cout << "number of s length > 3: " << num << endl;
}

10.21

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main() {int num = 10;auto f = [&num]() -> bool {if(num>0) {--num;return false;}else{return true;}};while(!f()){cout << num << endl;}cout << num << endl;
}

355 绑定函数bind的用法

#include <iostream>
#include <functional>
#include <string>
using namespace std;bool check_size(const std::string &s, std::string::size_type sz){return s.size() >= sz;
}int main() {auto check6 = bind(check_size, std::placeholders::_1, 6);std::string s = "hello world";bool b1 = check6(s);cout << b1 << endl;return 0;
}

355 使用bind函数代替lambda函数

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;void elimDups(vector<string> &words){sort(words.begin(), words.end());auto unqi = unique(words.begin(), words.end());words.erase(unqi, words.end());
}bool check_size(const string &s, string::size_type sz){return s.size()>=sz;
}void biggies(vector<string> &words, vector<string>::size_type sz, ostream &os=cout, char c=' '){elimDups(words);stable_sort(words.begin(), words.end(), [](const string &a, const string &b){return a.size()<b.size();});for (auto val:words){cout << val << " ";}cout << endl;// fox red the over slow jumps quick turtleauto wc = find_if(words.begin(), words.end(), bind(check_size, std::placeholders::_1, sz));auto count = words.end() - wc;cout << count << endl; // 5  5 elements with length >=4for_each(wc, words.end(),[&os, c](const string &s){os << s << c;});os << endl;// over slow jumps quick turtle}int main() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};biggies(vec, 4);}

356 bind实现重排参数

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using namespace std::placeholders;bool isShorter(int a, int b){return a <= b;
}int main() {vector<int> vec = {10, 12, 5, 7, 30};// asc 5 7 10 12 30sort(vec.begin(), vec.end(), isShorter);for (int val:vec){cout << val << " ";}cout << endl;// desc 30 12 10 7 5 sort(vec.begin(), vec.end(), bind(isShorter, _2, _1));for (int val:vec){cout << val << " ";}cout << endl;return 0;}

357 使用ref向bind函数传递引用对象

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using namespace std::placeholders;ostream &print(ostream &os, const string &s, char c){return os << s << c;
}void biggies(vector<string> &words, ostream &os=cout, char c=' '){//    for_each(words.begin(), words.end(),[&os, c](const string &s){os << s << c;});
//    error cannot copy print
//    for_each(words.begin(), words.end(), bind(print, os, _1, ' '));// using ref to transfer reference in bind functionfor_each(words.begin(), words.end(), bind(print, ref(os), _1, ' '));os << endl;// the quick red fox jumps over the slow red turtle}int main() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};biggies(vec);}

10.22

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using namespace std::placeholders;bool isLonger(string &s){return s.size() > 4;
}int main() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
//    auto number = count_if(vec.begin(), vec.end(), [](string &s){return s.size()>4;});auto number = count_if(vec.begin(), vec.end(), bind(isLonger, _1));cout << number << endl;}

10.24

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using namespace std::placeholders;bool check_size(int num, string s){return num > s.size();
}int main() {vector<int> vi = { 1,2,3,4,5,6 };string s("aaaa");auto result = find_if(vi.begin(), vi.end(), bind(check_size, _1, s));cout << *result << endl;return 0;}

10.25

#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
using namespace std::placeholders;void elimDups(vector<string> &words){sort(words.begin(), words.end());auto unqi = unique(words.begin(), words.end());words.erase(unqi, words.end());
}bool check_size(int num, string s){return s.size() < num;
}void biggies(vector<string> &words, vector<string>::size_type sz){elimDups(words);stable_sort(words.begin(), words.end(), [](const string &a, const string &b){return a.size()<b.size();});for (auto val:words){cout << val << " ";}cout << endl;// fox red the over slow jumps quick turtle//    auto wc = find_if(words.begin(), words.end(), [sz](const string &a){return a.size()>=sz;});
//    auto count = words.end() - wc;//    auto wc = partition(words.begin(), words.end(), [sz](const string &a){return a.size()<sz;});auto wc = partition(words.begin(), words.end(), bind(check_size, sz, _1));auto count = words.end() - wc;cout << count << endl; // 5  5 elements with length >=4for_each(wc, words.end(),[](const string &s){cout << s << " ";});cout << endl;// over slow jumps quick turtle}int main() {vector<string> vec= {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};biggies(vec, 4);}

358 inserter

#include <iostream>
#include <algorithm>
#include <iterator>
#include <list>
using namespace std;int main() {list<int> lst = {1, 2, 3, 4};list<int> lst2, lst3, lst4;copy(lst.cbegin(), lst.cend(), front_inserter(lst2));for (auto val:lst2){cout << val << " ";}cout << endl;// 4 3 2 1copy(lst.cbegin(), lst.cend(), inserter(lst3, lst3.begin()));for (auto val:lst3){cout << val << " ";}cout << endl;// 1 2 3 4copy(lst.cbegin(), lst.cend(), back_inserter(lst4));for (auto val:lst4){cout << val << " ";}cout << endl;// 1 2 3 4return 0;
}

10.27

#include <iostream>
#include <algorithm>
#include <iterator>
#include <list>
using namespace std;int main() {list<int> lst = {1, 2, 2, 3, 3, 4};list<int> lst2;unique_copy(lst.cbegin(), lst.cend(), back_inserter(lst2));for (auto val:lst2){cout << val << " ";}cout << endl;// 1 2 3 4return 0;
}

10.28

#include <iostream>
#include <algorithm>
#include <iterator>
#include <list>
using namespace std;int main() {list<int> lst = {1, 2, 2, 3, 3, 4};list<int> lst2, lst3, lst4;copy(lst.begin(), lst.end(), front_inserter(lst2));for (auto val:lst2){cout << val << " ";}cout << endl;copy(lst.begin(), lst.end(), back_inserter(lst3));for (auto val:lst3){cout << val << " ";}cout << endl;copy(lst.begin(), lst.end(), inserter(lst4, lst4.begin()));for (auto val:lst4){cout << val << " ";}cout << endl;return 0;
}

360 istream_iterator操作

#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
using namespace std;int main() {istream_iterator<int> int_it(cin);istream_iterator<int> int_eof;//    ifstream in("afile");
//    istream_iterator<string> str_in(in);vector<int> vec;istream_iterator<int> in_iter(cin);istream_iterator<int> eof;while (in_iter!=eof){vec.push_back(*in_iter++);}for (auto val:vec){cout << val << " ";}cout<< endl;return 0;
}

360 通过istream_iterator把输入流存入vector的最简单写法

#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
using namespace std;int main() {istream_iterator<int> in_iter(cin);istream_iterator<int> eof;vector<int> vec(in_iter, eof);return 0;
}

istream_iterator配合算法使用

#include <iostream>
#include <iterator>
#include <numeric>
using namespace std;int main() {istream_iterator<int> in(cin), eof;cout << accumulate(in, eof, 0) << endl;return 0;
}

ostream_iterator简介的打印数组

#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
using namespace std;int main() {vector<int> vec = {1, 2, 3, 4};ostream_iterator<int> out_iter(cout, " ");for (auto e: vec){*out_iter++=e;// out_iter = e;}cout << endl;// 1 2 3 4copy(vec.begin(), vec.end(), out_iter);// 1 2 3 4return 0;
}

10.29

#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<sstream>
#include<istream>
#include <iterator>using namespace std;int main() {string infile = "afile.txt";vector<string> svec;ifstream in(infile);if (in) {string buf;while (getline(in, buf)) {svec.push_back(buf);}}else {cerr << "can not open file: " << infile << endl;}ostream_iterator<string> out_iter(cout, " ");
//    copy(svec.begin(), svec.end(), out_iter);for(auto val:svec){*out_iter++ = val;}return 0;
}

10.30

#include<iostream>
#include<vector>
#include <iterator>
#include <algorithm>using namespace std;int main() {istream_iterator<int> iter(cin), eof;vector<int> vec(iter, eof);sort(vec.begin(), vec.end());ostream_iterator<int> oiter(cout, " ");copy(vec.begin(), vec.end(), oiter);return 0;
}

10.31-32

#include<iostream>
#include<vector>
#include <iterator>
#include <algorithm>using namespace std;int main() {istream_iterator<int> iter(cin), eof;vector<int> vec(iter, eof);sort(vec.begin(), vec.end());ostream_iterator<int> oiter(cout, " ");copy(vec.begin(), vec.end(), oiter);cout << endl;unique_copy(vec.begin(), vec.end(), oiter);return 0;
}

10.33

#include<iostream>
#include <iterator>
#include <fstream>
#include <algorithm>
using namespace std;int main() {string infile = "afile.txt";string outfileodd = "odd.txt";string outfileeven = "even.txt";ifstream  in(infile);ofstream out_odd(outfileodd);ofstream out_even(outfileeven);istream_iterator<int> in_iter(in), eof;ostream_iterator<int> out_iter_odd(out_odd, " "), out_iter_even(out_even, "\n");for_each(in_iter, eof, [&out_iter_odd, &out_iter_even](const int i){*(i%2?out_iter_odd:out_iter_even)++=i;});return 0;
}

363反向迭代器使用,排序使用

#include<iostream>
#include <iterator>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;int main() {vector<int> vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};for (auto r_iter=vec.crbegin(); r_iter!=vec.crend(); ++r_iter){cout << *r_iter << endl;}sort(vec.rbegin(), vec.rend());// 9 8 7 6 ....return 0;
}

365 反向迭代器与普通迭代器之间的转换

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;int main() {string line = "apple,banana,strawberry,cherry";auto comma = find(line.cbegin(), line.cend(), ',');cout << string(line.cbegin(), comma) << endl;// apple element before the first commaauto r_comma = find(line.crbegin(), line.crend(), ',');cout << string(line.crbegin(), r_comma) << endl;// yrrehccout << string(r_comma.base(), line.cend()) << endl;//  cherryreturn 0;
}

10.34

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
using namespace std;int main() {vector<int> ivec = {1, 2, 3, 4, 5, 6};ostream_iterator<int> oiter(cout, " ");copy(ivec.crbegin(), ivec.crend(), oiter);return 0;
}

10.36

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
using namespace std;int main() {vector<int> ivec = {1, 2, 3, 4, 5, 0, 6};auto pos = find(ivec.crbegin(), ivec.crend(), 0);cout << *pos << endl;return 0;

10.37

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
#include <list>
using namespace std;int main() {vector<int> ivec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};list<int> lst(10);copy(ivec.cbegin()+3, ivec.cbegin()+8, lst.rbegin());ostream_iterator<int> iter(cout, " ");copy(lst.begin(), lst.end(), iter);return 0;
}

splice 链表类型容器专有成员

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
#include <list>
using namespace std;int main() {list<int> lst = {0, 1, 2, 3, 4, 5, };list<int> lst2 = {6, 7, 8, 9};lst.splice(lst.begin(), lst2);ostream_iterator<int> iter(cout, " ");copy(lst.begin(), lst.end(), iter);// 6 7 8 9 0 1 2 3 4 5return 0;
}

10.42

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
#include <list>
using namespace std;int main() {list<int> lst = {0, 1, 2,  3, 4, 5, 5, 3};lst.sort();auto end_unique = unique(lst.begin(), lst.end());lst.erase(end_unique, lst.end());ostream_iterator<int> iter(cout, " ");copy(lst.begin(), lst.end(),iter);return 0;
}

C++ Primer 学习笔记 第十章 泛型算法相关推荐

  1. 【C++ Primer 学习笔记】: 容器和算法之【泛型算法】

    本系列博客主要是在学习 C++ Primer 时的一些总结和笔记. [C++ Primer 学习笔记]: 容器和算法之[泛型算法] 本文地址:http://blog.csdn.net/shanglia ...

  2. c++primer学习笔记

    c++ primer 5e学习笔记 第1章 1.标准库 类型和函数的集合,每个c++编译器都必须支持. 2.()运算符:调用运算符.跟随在函数名后,起调用函数的作用 第2章 1.p32:char在一些 ...

  3. Typescript 学习笔记七:泛型

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  4. Pytorch学习笔记-第十章

    Pytorch学习笔记-第十章图像描述 model data_preprocess data feature_extract main 记录一下个人学习和使用Pytorch中的一些问题.强烈推荐 &l ...

  5. C++ Primer 学习笔记(第四章:表达式)

    2019独角兽企业重金招聘Python工程师标准>>> ##C++ Primer 学习笔记(第四章:表达式) [TOC] ###4.1 基础 左值和右值: 当一个对象被用作右值的时候 ...

  6. 【学习笔记】多项式相关算法

    [学习笔记]多项式相关算法 手动博客搬家: 本文发表于20181125 13:19:28, 原地址https://blog.csdn.net/suncongbo/article/details/844 ...

  7. 《Go语言圣经》学习笔记 第十章 包和工具

    <Go语言圣经>学习笔记 第十章 包和工具 目录 包简介 导入路径 包声明 导入声明 包的匿名导入 包和命名 工具 注:学习<Go语言圣经>笔记,PDF点击下载,建议看书. G ...

  8. Apollo星火计划学习笔记——Apollo开放空间规划算法原理与实践

    文章目录 前言 1. 开放空间规划算法总体介绍 1.1 Task: OPEN_SPACE_ROI_DECIDER 1.2 Task: OPEN_SPACE_TRAJECTORY_PROVIDER 1. ...

  9. Boost库学习笔记(二)算法模块-C++11标准

    Boost库学习笔记(二)算法模块-C++11标准 一.综述 Boost.Algorithm是一系列人通用推荐算法的集合,虽然有用的通用算法很多,但是为了保证质量和体积,并不会将太多通用算法通过审查测 ...

最新文章

  1. Python爬虫入门教程 54-100 博客园等博客网站自动评论器
  2. Spring从菜鸟到高手(四)(上)使用JdbcTemplate类实现用户登陆验证、批量更新
  3. 膨胀腐蚀操作(MATLAB)
  4. 如何避免贫穷和忙碌,在2018年你需要这样提升自己 2018年01月07日 00:00:00 2099 热文导读 | 点击标题阅读 Java和Android架构2017年总结:文章精选 吊炸天!74
  5. 基于Arduino开发的简易“高水位报警系统解决方案”
  6. rfc mail content-type
  7. 上下文异常中的上下文属性_在没有适当上下文的情况下引发异常是一种不良习惯...
  8. linux内核测试,Linux内核测试的生命周期
  9. 【paper and code】StarGAN
  10. [翻译 EF Core in Action] 1.5 关于NoSql
  11. 个人笔记------无级分类格式化
  12. 追光者百度网盘提取码查询工具 v2.0726附使用方法
  13. 批量生成测试非重复命名的图片数据
  14. 51CTO学院 oracle相关视频地址
  15. druid安装与案例
  16. 什么是单工通信、半双工通信、全双工通信?3种通信方式的区别是什么?
  17. 2021年最强软件测试工程师Linux面试题及答案
  18. 【Gin框架】框架入门
  19. 中国联通dns服务器未响应,关于光猫设置的说明和常见问题
  20. 三年级信息技术用计算机娱乐,三年级上册信息技术教案

热门文章

  1. 天猫双十一成交额突破3723亿元
  2. nb iot 与java_NB-IoT物联网技术解析与案例详解 PDF 下载
  3. 三菱FX3U与欧姆龙E5CC温控器通讯实战程序
  4. 问题解决:error: ‘__s_getMD5Sum’ is not a member
  5. 一位博士生选择自杀,在论文中了顶会之后
  6. SaaS云收入的三种收费模式
  7. 【东方博宜】1112 - 【入门】查找子串并替换
  8. 【4 - 分组】Sql Server - 郝斌(分组group by、过滤having、聚合函数max() / count()、排序order by、select语句的执行顺序)
  9. 结对编程:软件工程训练
  10. C\C++ 向下\向上取整函数 floor() ceil()