C++ Prime 第十章 泛型算法
2023-04-26~2023-04-28
10.1节练习
练习10.1:
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
vector<int> vecInt;
int temp;
while (cin >> temp)
{
vecInt.push_back(temp);
}
int val = 1;
auto ret = count(vecInt.cbegin(), vecInt.cend(), val);
cout << ret << endl;
system("pause");
return 0;
}
练习10.2:
#include<vector>
#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
int main()
{
list<string> vecStr;
string temp;
while (cin >> temp)
{
vecStr.push_back(temp);
}
string val = "qwe";
auto ret = count(vecStr.cbegin(), vecStr.cend(), val);
cout << ret << endl;
system("pause");
return 0;
}
10.2.1节练习
练习10.3:
#include<vector>
#include<iostream>
#include<algorithm>
#include<list>
#include<numeric>
using namespace std;
int main()
{
vector<int> vecInt = { 1, 2, 3, 4 };
auto ret = accumulate(vecInt.cbegin(), vecInt.cend(), 0);
cout << ret << endl;
system("pause");
return 0;
}
练习10.4:
#include<vector>
#include<iostream>
#include<algorithm>
#include<list>
#include<numeric>
using namespace std;
int main()
{
vector<double> vecInt = { 1.1, 2.2, 3.3, 4.4 };
auto ret = accumulate(vecInt.cbegin(), vecInt.cend(), 0);
cout << ret << endl; // 10,将小数点后的数据都舍弃掉了
system("pause");
return 0;
}
练习10.5:
#include<vector>
#include<iostream>
#include<algorithm>
#include<list>
#include<numeric>
using namespace std;
int main()
{
vector<string> vecStr1 = { "qwer", "asdf" };
vector<string> vecStr2 = { "qwer", "asdf" };
cout << &vecStr1[0] << endl;
cout << &vecStr2[0] << endl;
cout << &vecStr1[1] << endl;
cout << &vecStr2[1] << endl;
auto ret1 = equal(vecStr1.cbegin(), vecStr1.cend(), vecStr2.cbegin());
// equal内部使用==运算符比较两个string,而string类内部重载了运算符,比较的是内容及长度,所以返回true
cout << ret1 << endl; // true
vector<const char*> exam1;
vector<const char*> exam2;
char temp1[] = "qwer";
char temp2[] = "qwer";
char temp3[] = "asdf";
char temp4[] = "asdf";
exam1.push_back(temp1);
exam2.push_back(temp2);
exam1.push_back(temp3);
exam2.push_back(temp4);
cout << &exam1[0] << endl;
cout << &exam2[0] << endl;
cout << &exam1[1] << endl;
cout << &exam2[1] << endl;
auto ret2 = equal(exam1.cbegin(), exam1.cend(), exam2.cbegin());
cout << ret2 << endl; // false
// equal使用==运算符比较c风格的字符串,两个c风格的字符串在比较时比较的是两个指针(地址)是否相同与内容无关,所以返回false
system("pause");
return 0;
}
10.2.2节练习
练习10.6:
#include<vector>
#include<iostream>
#include<algorithm>
#include<list>
#include<numeric>
using namespace std;
int main()
{
vector<int> vecInt = { 1, 2, 3, 4, 5 };
fill_n(vecInt.begin(), vecInt.size(), 0);
for (auto temp : vecInt)
{
cout << temp << endl;
}
system("pause");
return 0;
}
练习10.7:
(a):
#include<vector>
#include<iostream>
#include<algorithm>
#include<list>
#include<numeric>
using namespace std;
int main()
{
vector<int> vec;
list<int> lst;
int i;
while (cin >> i)
{
lst.push_back(i);
}
copy(lst.cbegin(), lst.cend(), back_inserter(vec));
system("pause");
return 0;
}
(b):
#include<vector>
#include<iostream>
#include<algorithm>
#include<list>
#include<numeric>
using namespace std;
int main()
{
vector<int> vec;
fill_n(back_inserter(vec), 10, 0);
system("pause");
return 0;
}
练习10.8:
标准库不会直接对所操作的容器修改容器大小,back_inserter是通过push_back向容器添加元素
10.2.3节练习
练习10.9:
#include<vector>
#include<iostream>
#include<algorithm>
#include<list>
#include<numeric>
using namespace std;
void elimDups(vector<string> &vecStr)
{
sort(vecStr.begin(), vecStr.end());
auto endIter = unique(vecStr.begin(), vecStr.end());
for (auto temp : vecStr)
{
cout << temp << endl;
}
vecStr.erase(endIter, vecStr.end());
for (auto temp : vecStr)
{
cout << temp << endl;
}
}
int main()
{
vector<string> vecStr = { "qwandx", "qwandx", "the", "solider", "nice", "nice" };
elimDups(vecStr);
system("pause");
return 0;
}
练习10.10:
确保代码的稳定性、可靠性
10.3.1 节练习
练习10.11:
#include<vector>
#include<iostream>
#include<algorithm>
#include<list>
#include<numeric>
using namespace std;
bool isShorter(const string& s1, const string& s2)
{
return s1.size() < s2.size();
}
void elimDups(vector<string> &vecStr)
{
stable_sort(vecStr.begin(), vecStr.end(), isShorter);
for (auto temp : vecStr)
{
cout << temp << endl;
}
cout << "----------------------" << endl;
auto endIter = unique(vecStr.begin(), vecStr.end());
vecStr.erase(endIter, vecStr.end());
for (auto temp : vecStr)
{
cout << temp << endl;
}
}
int main()
{
vector<string> vecStr = { "a", "a", "d", "c", "aice", "aice" };
elimDups(vecStr);
system("pause");
return 0;
}
练习10.12:
#include<vector>
#include<iostream>
#include<algorithm>
#include<list>
#include<numeric>
using namespace std;
class Sales_data
{
public:
Sales_data(string is)
{
this->isbn = is;
}
string isbn;
};
bool compareIsbn(Sales_data& s1, Sales_data& s2)
{
return s1.isbn < s2.isbn;
}
int main()
{
Sales_data s1("100-001");
Sales_data s2("100-002");
Sales_data s4("100-004");
Sales_data s3("100-003");
vector<Sales_data> vecSales = { s2, s1, s4, s3 };
for (auto temp : vecSales)
{
cout << temp.isbn << endl;
}
sort(vecSales.begin(), vecSales.end(), compareIsbn);
for (auto temp : vecSales)
{
cout << temp.isbn << endl;
}
system("pause");
return 0;
}
练习10.13:
#include<vector>
#include<iostream>
#include<algorithm>
#include<list>
#include<numeric>
using namespace std;
bool isLengthThan(const string& s)
{
return s.length() >= 5;
}
int main()
{
vector<string> words = { "123456", "123451", "123", "1234" };
auto end_iter = partition(words.begin(), words.end(), isLengthThan);
words.erase(end_iter, words.end());
for (auto temp : words)
{
cout << temp << endl;
}
system("pause");
return 0;
}
10.3.2节练习
练习10.14:
#include<iostream>
using namespace std;
int main()
{
auto f = [](int a, int b) {return a + b; };
auto ret = f(1, 2);
cout << ret << endl;
system("pause");
return 0;
}
练习10.15:
#include<iostream>
using namespace std;
void test(int a)
{
auto f = [a](int b) {return a + b; };
auto ret = f(2);
cout << ret << endl;
}
int main()
{
test(1);
system("pause");
return 0;
}
练习10.16:
#include<vector>
#include<algorithm>
#include<list>
#include<numeric>
#include<iostream>
using namespace std;
string make_plural(size_t ctr, const string& word, const string& ending)
{
return (ctr > 1) ? word + ending : word;
}
void elimDups(vector<string>& words)
{
sort(words.begin(), words.end());
auto endIter = unique(words.begin(), words.end());
words.erase(endIter, 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 beginIter = find_if(words.begin(), words.end(),
[sz](const string& a) {return a.size() >= sz; });
auto count = words.end() - beginIter;
cout << count << " " << make_plural(count, "word", "s") << endl;
for_each(beginIter, words.end(),
[](const string& s) {cout << s << endl; });
}
int main()
{
vector<string> vecStr = { "123", "1234", "12345", "123456", "134567" };
biggies(vecStr, 5);
system("pause");
return 0;
}
练习10.17:
#include<vector>
#include<iostream>
#include<algorithm>
#include<list>
#include<numeric>
using namespace std;
class Sales_data
{
public:
Sales_data(string is)
{
this->isbn = is;
}
string isbn;
};
int main()
{
Sales_data s1("100-001");
Sales_data s2("100-002");
Sales_data s4("100-004");
Sales_data s3("100-003");
vector<Sales_data> vecSales = { s2, s1, s4, s3 };
for (auto temp : vecSales)
{
cout << temp.isbn << endl;
}
sort(vecSales.begin(), vecSales.end(),
[](Sales_data& data1, Sales_data& data2) {return data1.isbn < data2.isbn; });
for (auto temp : vecSales)
{
cout << temp.isbn << endl;
}
system("pause");
return 0;
}
练习10.18:
#include<vector>
#include<algorithm>
#include<list>
#include<numeric>
#include<iostream>
using namespace std;
string make_plural(size_t ctr, const string& word, const string& ending)
{
return (ctr > 1) ? word + ending : word;
}
void elimDups(vector<string>& words)
{
sort(words.begin(), words.end());
auto endIter = unique(words.begin(), words.end());
words.erase(endIter, 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 beginIter = partition(words.begin(), words.end(),
[sz](const string& a) {return a.size() < sz; });
auto count = words.end() - beginIter;
cout << count << " " << make_plural(count, "word", "s") << endl;
for_each(beginIter, words.end(),
[](const string& s) {cout << s << endl; });
}
int main()
{
vector<string> vecStr = { "123", "1234", "12345", "123456", "134567" };
biggies(vecStr, 5);
system("pause");
return 0;
}
练习10.19:
#include<vector>
#include<algorithm>
#include<list>
#include<numeric>
#include<iostream>
using namespace std;
string make_plural(size_t ctr, const string& word, const string& ending)
{
return (ctr > 1) ? word + ending : word;
}
void elimDups(vector<string>& words)
{
sort(words.begin(), words.end());
auto endIter = unique(words.begin(), words.end());
words.erase(endIter, 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 beginIter = stable_partition(words.begin(), words.end(),
[sz](const string& a) {return a.size() < sz; });
auto count = words.end() - beginIter;
cout << count << " " << make_plural(count, "word", "s") << endl;
for_each(beginIter, words.end(),
[](const string& s) {cout << s << endl; });
}
int main()
{
vector<string> vecStr = { "123", "1234", "12345", "123456", "134567" };
biggies(vecStr, 5);
system("pause");
return 0;
}
可变lambda
- 默认情况下,对于一个值被拷贝的变量,lambda不会改变其值,如果我们希望能够改变它,就需要在参数列表后面加上mutable
#include<iostream>
using namespace std;
void func3()
{
size_t v1 = 42;
auto f = [v1]() mutable {return ++v1; };
v1 = 0;
auto j = f(); // 43
cout << j << endl;
}
int main()
{
func3();
system("pause");
return 0;
}
10.3.3节练习
练习10.20:
auto ret = count_if(words.begin(), words.end(),
[](string a) {return a.length() > 6; });
练习10.21:
#include<iostream>
using namespace std;
void test()
{
int i = 10;
auto func = [=]() mutable ->bool
{
while (i > 0)
{
--i;
}
return true;
};
bool ret = func();
cout << ret << endl;
}
int main()
{
test();
system("pause");
return 0;
}
bind函数
基本使用
#include<vector>
#include<algorithm>
#include<list>
#include<numeric>
#include<iostream>
#include<functional>
using namespace std;
using namespace std::placeholders;
bool check_size(string& s, size_t sz)
{
return s.length() > sz;
}
int main()
{
int sz = 6;
vector<string> words = { "123", "12345", "123456789" };
auto end_iter = find_if(words.begin(), words.end(), bind(check_size, _1, sz));
cout << *end_iter << endl;
system("pause");
return 0;
}
调整入参顺序
#include<vector>
#include<algorithm>
#include<list>
#include<numeric>
#include<iostream>
#include<functional>
using namespace std;
using namespace std::placeholders;
void test(int a, int b, int c, int d, int e)
{
cout << "a" << ": " << a << endl;
cout << "b" << ": " << b << endl;
cout << "c" << ": " << c << endl;
cout << "d" << ": " << d << endl;
cout << "e" << ": " << e << endl;
}
int main()
{
int b = 6;
auto func = bind(test, _4, b, _3, _2, _1);
func(1, 2, 3, 4);
system("pause");
return 0;
}
bind绑定引用参数
- bind本质是通过拷贝来将参数传递到函数中,对于无法进行拷贝的对象可以使用函数ref来解决。
- 函数ref返回一个对象,包含给定的引用,该返回的对象是可以拷贝的,所以该对象可以传递给bind函数作为参数
#include<vector>
#include<algorithm>
#include<list>
#include<numeric>
#include<iostream>
#include<functional>
using namespace std;
using namespace std::placeholders;
void print(ostream& os, const string& s1, const string& s2)
{
os << s1 << s2 << endl;
}
int main()
{
ostream &os = cout;
auto func = bind(print, ref(os), _2, _1);
func("s1", "s2");
system("pause");
return 0;
}
10.3.4节练习4
练习10.22:
#include<vector>
#include<algorithm>
#include<list>
#include<numeric>
#include<iostream>
#include<functional>
using namespace std;
using namespace std::placeholders;
bool checkLength(string& s, size_t sz)
{
return s.length() >= sz;
}
int main()
{
int sz = 6;
vector<string> vecStr = { "12345", "1233456", "1234567", "12345678" };
auto endIter = find_if(vecStr.begin(), vecStr.end(), bind(checkLength, _1, sz));
while (endIter != vecStr.end())
{
cout << *endIter << endl;
++endIter;
}
system("pause");
return 0;
}
练习10.23:
至少两个参数
练习10.24:
#include<vector>
#include<algorithm>
#include<list>
#include<numeric>
#include<iostream>
#include<functional>
using namespace std;
using namespace std::placeholders;
bool check_size(string& s, int num)
{
return num > s.length();
}
int main()
{
string s = "qwer";
vector<int> vecInt = { 1, 2, 3, 4, 5, 6 };
auto beginIter = find_if(vecInt.begin(), vecInt.end(), bind(check_size, s, _1));
cout << *beginIter << endl;
system("pause");
return 0;
}
练习10.25:
略
10.4.1节练习
练习10.26:
inserter:任意位置插入
back_inserter:每次向尾部插入
front_inserter:每次向头部插入
练习10.27:
#include<vector>
#include<algorithm>
#include<list>
#include<numeric>
#include<iostream>
#include<functional>
using namespace std;
using namespace std::placeholders;
int main()
{
vector<int> vecInt = { 1, 2, 3, 4 ,5, 5, 5, 6, 7 };
list<int> lstInt(vecInt.size());
auto endIter = unique_copy(vecInt.begin(), vecInt.end(), lstInt.begin());
lstInt.erase(endIter, lstInt.end());
for (auto temp : lstInt)
{
cout << temp << endl;
}
system("pause");
return 0;
}
练习10.28:
#include<vector>
#include<algorithm>
#include<list>
#include<numeric>
#include<iostream>
#include<functional>
using namespace std;
using namespace std::placeholders;
int main()
{
vector<int> vecInt = { 1, 2, 3, 4 ,5, 6, 7, 8, 9 };
// back_inserter
vector<int> vecInt1;
copy(vecInt.begin(), vecInt.end(), back_inserter(vecInt1));
for (auto temp : vecInt1)
{
cout << temp << endl;
}
// inserter
vector<int> vecInt2;
copy(vecInt.begin(), vecInt.end(), inserter(vecInt2, vecInt2.begin()));
for (auto temp : vecInt2)
{
cout << temp << endl;
}
// front_inserter
list<int> lstInt3;
copy(vecInt.begin(), vecInt.end(), front_inserter(lstInt3));
for (auto temp : lstInt3)
{
cout << temp << endl;
}
system("pause");
return 0;
}
10.4.2节练习
练习10.29:
#include<vector>
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
// 从文件中读取数据
ifstream infile("data.txt");
// 创建流迭代器
istream_iterator<string> start(infile), end;
// 通过流迭代器创建vector
vector<string> vecInt(start, end);
// 输出vector中的数据
for (auto temp : vecInt)
{
cout << temp << " ";
}
cout << endl;
// 输出vector中的数据,方法2
copy(vecInt.begin(), vecInt.end(), ostream_iterator<string>(cout, " "));
system("pause");
return 0;
}
练习10.30:
#include<vector>
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int main()
{
// 创建流迭代器从标准输入中读入整数,创建尾后迭代器end
istream_iterator<int> start(cin), end;
// 创建流迭代器从将数据输出到标准输出中,每次输入带上一个空字符串
ostream_iterator<int> os(cout, " ");
vector<int> vecInt;
while (start != end)
{
vecInt.push_back(*start++);
}
// 排序
sort(vecInt.begin(), vecInt.end());
// 将vecInt中的数据据输出到标准输出中
copy(vecInt.begin(), vecInt.end(), os);
// 刷新缓冲区
cout << endl;
system("pause");
return 0;
}
练习10.31:
#include<vector>
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int main()
{
istream_iterator<int> start(cin), end;
vector<int> vecInt;
while (start != end)
{
vecInt.push_back(*start++);
}
sort(vecInt.begin(), vecInt.end());
vector<int> vecIntTemp(vecInt.size());
auto endIter = unique_copy(vecInt.begin(), vecInt.end(), vecIntTemp.begin());
vecIntTemp.erase(endIter, vecIntTemp.end());
copy(vecIntTemp.begin(), vecIntTemp.end(), ostream_iterator<int>(cout, " "));
system("pause");
return 0;
}
练习10.32:
#include<vector>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<numeric>
using namespace std;
class Sales_data
{
friend istream& operator>>(istream& in, Sales_data& s);
public:
Sales_data() = default;
Sales_data(const string& book, unsigned units, double price)
: bookNo(book), unitsSold(units), revenue(price* units) {};
void print() const
{
cout << " bookNO: " << this->bookNo
<< " unitsSold: " << this->unitsSold
<< " revenue: " << this->revenue << endl;
}
Sales_data& operator+(Sales_data& s)
{
this->unitsSold += s.unitsSold;
this->revenue += s.revenue;
return *this;
}
string bookNo;
private:
unsigned unitsSold = 0;
double revenue = 0.0;
};
istream& operator>>(istream& in, Sales_data& s)
{
double price = 0.0;
cout << "bookNo: ";
in >> s.bookNo;
// 每次标准输入后都检查以下是否还有输入流
if (in)
{
cout << "unitsSold: ";
in >> s.unitsSold;
if (in)
{
cout << "price: ";
in >> price;
s.revenue = s.unitsSold * price;
}
}
return in;
}
bool compareIsbn(const Sales_data& s1, const Sales_data& s2)
{
return s1.bookNo < s2.bookNo;
}
int main()
{
// 创建流迭代器,从输入流接收Sales_data数据
istream_iterator<Sales_data> start(cin), end;
// 使用vector容器保存输入流中的Sales_data数据
vector<Sales_data> vecSales;
// 无输入直接退出程序
if (!cin)
{
return 0;
}
while (start != end)
{
vecSales.push_back(*start++);
}
// 排序
sort(vecSales.begin(), vecSales.end(), compareIsbn);
// 创建一个vector记录有多少个不同的isbn的Sales_data数据
vector<string> uniqueData;
for (auto temp : vecSales)
{
uniqueData.push_back(temp.bookNo);
}
// 去重
auto endIter = unique(uniqueData.begin(), uniqueData.end());
// 删除空白项
uniqueData.erase(endIter, uniqueData.end());
// 以去重后的isbn的数量作为循环的次数
int loopTimes = uniqueData.size();
int times = 0;
// 首次循环-获得第一个元素的isbn
string beginIsbn = vecSales[0].bookNo;
// 获得首元素的迭代器
auto beginIter = vecSales.begin();
// 开始循环,每次遇到不同的isbn时就对这个范围内的SalesData数据进行累加并输出累加后的数据
while (times < loopTimes)
{
// 寻找首次出现的不同的isbn元素的迭代器位置
auto otherIsbnIter = find_if(
beginIter, vecSales.end(),
[&beginIsbn](Sales_data& s) mutable -> bool
{
// 找到不同的isbn后,替换该isbn作为下次寻找的条件
if (s.bookNo != beginIsbn)
{
beginIsbn = s.bookNo;
return true;
}
else
{
return false;
}
}
);
// 对范围内的数据进行累加并输出,该isbn数据只有一项时,直接输出信息
if (beginIter != otherIsbnIter)
{
auto retData = accumulate(beginIter+1, otherIsbnIter, *beginIter);
retData.print();
}
else
{
(*beginIter).print();
}
// 将初始位置替换,下次寻找不同isbn将从这个累加的范围后的第一个数据进行开始
beginIter = otherIsbnIter;
++times;
}
system("pause");
return 0;
}
练习10.33:
#include<vector>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<numeric>
using namespace std;
void func(const string& inFileName, const string& outFileName1, const string& outFileName2)
{
ifstream infile("intFile.txt");
istream_iterator<int> start(infile), end;
ofstream osOdd(outFileName1);
ofstream osEven(outFileName2);
ostream_iterator<int> iterOdd(osOdd, " "), iterEven(osEven, "\n");
while (start != end)
{
if (*start % 2 == 0)
{
*iterEven++ = *start;
}
else
{
*iterOdd++ = *start;
}
++start;
}
infile.close();
osOdd.close();
osEven.close();
}
int main()
{
func("intFile.txt", "oddFile.txt", "evenFile.txt");
system("pause");
return 0;
}
10.4.3节练习
练习10.34:
#include<vector>
#include<iostream>
using namespace std;
int main()
{
vector<int> vecInt = { 1, 2, 3, 4 ,5 };
for (auto iter = vecInt.crbegin(); iter != vecInt.crend(); ++iter)
{
cout << *iter << endl;
}
system("pause");
return 0;
}
练习10.35:
#include<vector>
#include<iostream>
using namespace std;
int main()
{
vector<int> vecInt = { 1, 2, 3, 4 ,5 };
for (auto iter = vecInt.cend(); iter != vecInt.begin(); --iter)
{
cout << *(iter-1) << endl;
}
system("pause");
return 0;
}
练习10.36:
#include<list>
#include<iostream>
using namespace std;
int main()
{
list<int> lstInt = { 1, 2, 3, 0, 4 ,5, 0 };
auto iter = find(lstInt.crbegin(), lstInt.crbegin(), 0);
cout << *iter << endl;
system("pause");
return 0;
}
练习10.33:
#include<vector>
#include<list>
#include<iostream>
using namespace std;
void copyVec(vector<int>::const_reverse_iterator iter, int indexA, int indexB)
{
auto iterA = iter + indexA + 1;
auto iterB = iter + indexB;
list<int> temp(iterA, iterB);
for (auto i : temp)
{
cout << i << endl;
}
}
int main()
{
vector<int> lstInt = { 1, 2, 3, 4, 5 ,6, 7, 8, 9, 10 };
copyVec(lstInt.crbegin(), 3, 7);
system("pause");
return 0;
}
10.5.1节练习
练习10.38:
输入迭代器:只读不写、支持前置++、后置++、解引用*、==、!=运算符、箭头运算符
输出迭代器:只写不读、支持前置++、后置++、解引用*、==、!=运算符、箭头运算符
前向迭代器:支持输入、输出迭代器的所有操作、迭代器支持单向序列移动、多次读写同一个元素
双向迭代器:支持前向迭代器的所有操作,支持双向序列移动(前置--,后置--)
随机访问迭代器:支持双向迭代器的所有操作、支持迭代器之间<、>、>=、<=、-、运算,支持迭代器与整数运算(+、-、+=、-=),支持下标运算符
练习10.39:
list迭代器是双向迭代器(不支持随机访问),forward_list是单向迭代器,vector是随机访问迭代器
练习10.40:
copy前两个参数要求至少是输入迭代器,第三个参数至少要求是输出迭代器
reverse要求至少双向迭代器
unique随机访问迭代器
10.5.3节练习
练习10.41:
replace(beg, end, old_val, new_val);
将beg到end迭代器范围内的old_val替换成new_val
replace_if(beg, end, pred, new_val);
查找beg到end迭代器范围内令pred为真的数据并将其内容替换为new_val
replace_if(beg, end, dest, old_val, new_val);
将beg到end迭代器范围内的数据全部拷贝到dest中,并将其中的old_val替换成new_val
replace_if(beg, end, dest, old_val, new_val);
将beg到end迭代器范围内的数据全部拷贝到dest中,并将其中令pred为真的数据的内容替换成new_val
#include<vector>
#include<list>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
vector<int> lstInt = { 1, 2, 3, 4, 5 ,6, 7, 8, 9, 10 };
replace_if(lstInt.begin(), lstInt.end(), [](int a)
{
return a % 2 > 0;
}, 0
);
for (auto temp : lstInt)
{
cout << temp << endl;
}
vector<int> lstInt2;
replace_copy_if(lstInt.begin(), lstInt.end(), back_inserter(lstInt2), [](int a)
{
return a % 2 > 0;
}, 0
);
for (auto temp : lstInt2)
{
cout << temp << endl;
}
system("pause");
return 0;
}
10.6节练习
练习10.42:
#include<iostream>
#include<list>
using namespace std;
void elimDups(list<string>& lstStr)
{
lstStr.sort();
lstStr.unique();
for (auto temp : lstStr)
{
cout << temp << endl;
}
}
int main()
{
list<string> lstcStr = { "qwandx", "qwandx", "the", "solider", "nice", "nice" };
elimDups(lstcStr);
system("pause");
return 0;
}
勇敢和愚蠢只有一剑之差