大多數(shù)算法定義在algorithm中讨韭。標(biāo)準(zhǔn)庫(kù)還在頭文件numeric中定義了一組數(shù)值泛型算法
練習(xí)10.1:頭文件algorithm中定義了一個(gè)名為count的函數(shù)首装,它類似find,接受一對(duì)迭代器和一個(gè)值作為參數(shù)漩仙。count返回給定值在序列中出現(xiàn)的次數(shù)掩宜。編寫(xiě)程序,讀取int序列存入vector中忧便,打印有多少個(gè)元素的值等于給定值,
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> nums = {123,456,789,123,456,789,1};
cout << count(nums.cbegin(), nums.cend(), 123) << endl;
return 0;
}
練習(xí)10.3:用accumulate求一個(gè)vector<int>中的元素之和
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
vector<int> nums = {1,2,3,4,5,6};
cout << accumulate(nums.cbegin(), nums.cend(), 0);
return 0;
}
練習(xí)10.9:實(shí)現(xiàn)你自己的elimDups帽借。測(cè)試你的程序茬腿,分別在讀取輸入后、調(diào)用unique后以及調(diào)用erase后打印vector的內(nèi)容宜雀。
void elimDups(vector<string> & words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
練習(xí)10.11:編寫(xiě)程序,使用stable_sort和isShorter將傳遞給你的elimDups版本的vector排序握础。打印vector的內(nèi)容辐董,驗(yàn)證你程序的正確性
#include <iostream>
#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);
int main()
{
vector<string> words = {"the", "quick", "red", "fox", "jumps", "over",
"the", "slow", "red", "turtle"};
elimDups(words);
stable_sort(words.begin(), words.end(), isShorter);
for (auto i : words)
cout << i << " ";
return 0;
}
void elimDups(vector<string> & words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
練習(xí)10.13:標(biāo)準(zhǔn)庫(kù)定義了名為partition的算法,它接受一個(gè)謂詞禀综,對(duì)容器內(nèi)容進(jìn)行劃分简烘,使得謂詞為true的值會(huì)排在容器的前半部分,而未日次false的值會(huì)在后半部分定枷。算法返回一個(gè)迭代器指向最后一個(gè)使謂詞位true的元素之后的位置孤澎。編寫(xiě)一個(gè)函數(shù),接受string欠窒,返回bool覆旭,指出string是否有5個(gè)或更多字符。使用此函數(shù)劃分words打印長(zhǎng)度大于等于5的元素,
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
bool compareWords(const string &);
int main()
{
vector<string> words = {"the", "quick", "red", "fox", "jumps", "over",
"the", "slow", "red", "turtle"};
auto end = partition(words.begin(), words.end(), compareWords);
for (auto i = words.begin(); i != end; ++i)
{
cout << *i << " ";
}
return 0;
}
bool compareWords(const string & words)
{
return words.size() > 4;
}
lambda
lambda表達(dá)式形式:
[capture list] (parameter list) -> return type { function body}
- 捕獲列表只用于局部非靜態(tài)變量型将。
- 可以直接使用局部靜態(tài)變量和外部變量
練習(xí)10.14:編寫(xiě)一個(gè)lambda寂祥,接受兩個(gè)int,返回他們的和七兜。
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 4;
auto f = [](const int a, const int b) {return a + b;};
cout << f(a, b) << endl;
}
練習(xí)10.15:編寫(xiě)一個(gè)lambda丸凭,捕獲它所在函數(shù)的int,并接受一個(gè)int參數(shù)腕铸。lambda應(yīng)該返回捕獲的int和int參數(shù)的和惜犀。
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 4;
auto f = [a](const int b) {return a + b;};
cout << f(b) << endl;
}
從寫(xiě)biggies用partition代替find_if
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();});
auto wz = partition(words.begin(), words.end(),
[sz](string &a)
{ return a.size() < sz;});
for_each( wz, words.end(),
[](const string &a)
{cout << a << " ";});
cout << endl;
}
在創(chuàng)建lambda的時(shí)候捕獲列表就保存了捕獲的值
修改捕獲列表的值必須使用mutable
auto f = [&a] () mutable {return ++a;};
返回其他類型使用尾置返回類型
可以使用bind代替lambda
引用 ref bind(print, ref(os), _1, ' ')
placeholders命名空間
using namespace std::placeholders;
練習(xí)10.22:重寫(xiě)統(tǒng)計(jì)長(zhǎng)度小于等于5的單詞數(shù)量的程序。使用函數(shù)代替lambda狠裹;
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
using namespace std;
using namespace std::placeholders;
void biggies(vector<string> &words, vector<string>::size_type sz);
void elimDups(vector<string> &words);
bool check6(const string &a, string::size_type sz); //bind
int main()
{
vector<string> words = {"the", "quick", "red", "fox", "jumps", "over",
"the", "slow", "red", "turtle"};
biggies(words, 5);
}
void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto no_unique = unique(words.begin(), words.end());
words.erase(no_unique, 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();});
auto wz = find_if(words.begin(), words.end(),
bind(check6, _1, sz));
for_each(words.begin(), wz,
[](const string &a)
{cout << a << " ";});
cout << endl;
}
bool check6(const string &a, string::size_type sz)
{
return a.size() > sz;
}
練習(xí)10.23:bind接受幾個(gè)參數(shù)虽界?
auto newCallable = bind(callable, arg_list);
練習(xí)10.26:解釋三種插入迭代器的不同之處
- back_inserter:創(chuàng)建使用push_back的迭代器
- front_inserter創(chuàng)建一個(gè)使用push_front的迭代器
- inserter:創(chuàng)建一個(gè)使用insert的迭代器。此函數(shù)接受第二個(gè)參數(shù)酪耳,這個(gè)參數(shù)必須是一個(gè)指向給定容器的迭代器浓恳。元素將被插入到給定迭代器所表示的元素之前。
練習(xí)10.27:使用unique_copy函數(shù)將不重復(fù)的字母copy到一個(gè)初始化為空的list中
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <list>
using namespace std;
int main()
{
vector<string> words = {"the", "quick", "red", "fox", "jumps", "over",
"the", "slow", "red", "turtle"};
list<string> wwww;
sort(words.begin(), words.end());
unique_copy(words.cbegin(), words.cend(), inserter(wwww, wwww.begin()));
for (auto i : words)
cout << i << " ";
cout << endl;
for (auto i : wwww)
cout << i << " ";
cout << endl;
return 0;
}
練習(xí)10.28:一個(gè)vector中保存1-9用三種迭代器copy到三種不同的容器
練習(xí)10.29:編寫(xiě)程序碗暗,使用流迭代器讀取一個(gè)文本文件颈将,存入一個(gè)vector中的string里
#include <iostream>
#include <vector>
#include <iterator>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream in("1.txt");
istream_iterator<string> str_it(in);
istream_iterator<string> eof;
vector<string> str(str_it, eof);
ostream_iterator<string> out_iter(cout, " ");
copy(str.cbegin(), str.cend(), out_iter);
cout << endl;
return 0;
}
練習(xí)10.30:使用流迭代器、sort和copy從標(biāo)準(zhǔn)輸入讀取一個(gè)整數(shù)序列言疗,將其排序晴圾,并將結(jié)構(gòu)寫(xiě)到標(biāo)準(zhǔn)輸出
#include <iostream>
#include <vector>
#include <iterator>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
istream_iterator<int> in_iter(cin);
istream_iterator<int> eof;
vector<int> nums(in_iter, eof);
sort(nums.begin(), nums.end());
ostream_iterator<int> out_iter(cout, " ");
copy(nums.cbegin(), nums.cend(), out_iter);
return 0;
}
練習(xí)10.34:使用reverse_iterator逆序打印一個(gè)vector
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
int main()
{
vector<int> vec = {1,2,3,4,5,6,7,8};
for (auto r_iter = vec.crbegin();
r_iter != vec.crend();
++r_iter)
cout << *r_iter << endl;
return 0;
}
練習(xí)10.37:給定一個(gè)包含10個(gè)元素的vector,將3-7之間的元素逆序拷貝到list中
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
vector<int> num = {1,2,3,4,5,6,7,8,9,0};
list<int> nums(num.crbegin() + 3, num.crend() - 2);
for (auto i : nums)
cout << i << " ";
cout << endl;
return 0;
}
練習(xí)10.38:列出5個(gè)迭代器類別噪奄,以及每類迭代器所支持的操作死姚。
輸入迭代器:只讀,不寫(xiě)勤篮;單遍掃描都毒,只能遞增
輸出迭代器:只寫(xiě),不讀:?jiǎn)伪閽呙枧龅蓿荒苓f增
前向迭代器:可讀寫(xiě)账劲;多遍掃描,只能遞增
雙向迭代器:可讀寫(xiě)金抡;多遍掃描瀑焦,可遞增遞減
隨機(jī)訪問(wèn)迭代器:可讀寫(xiě),多遍掃描梗肝,支持全部迭代器運(yùn)算