提交 57002cb9 编写于 作者: J jzplp

10.34-10.42

上级 3d0aa7f7
#include<iostream>
#include<vector>
int main()
{
std::vector<int> v{1,2,3,4,5,6,7,8,9};
for(std::vector<int>::const_reverse_iterator rp = v.crbegin(); rp != v.crend(); ++rp)
std::cout << *rp << " ";
std::cout << std::endl;
return 0;
}
#include<iostream>
#include<vector>
int main()
{
std::vector<int> v{1,2,3,4,5,6,7,8,9};
for(std::vector<int>::const_iterator rp = v.cend() - 1; rp != v.cbegin(); --rp)
std::cout << *rp << " ";
if(v.size() != 0)
std::cout << *(v.cbegin()) << std::endl;
return 0;
}
#include<iostream>
#include<algorithm>
#include<list>
int main()
{
std::list<int> l{1,2,3,4,5,6,7,8,0,9};
std::list<int>::const_reverse_iterator rp = std::find(l.crbegin(), l.crend(), 0);
std::cout << *rp << std::endl;
return 0;
}
#include<iostream>
#include<algorithm>
#include<list>
#include<vector>
int main()
{
std::vector<int> v{1,2,3,4,5,6,7,8,9,10};
std::vector<int>::const_reverse_iterator rpb = v.crbegin() + 3;
std::vector<int>::const_reverse_iterator rpe = v.crbegin() + 8;
std::list<int> l(rpb, rpe);
for(int i : l)
std::cout << i << " ";
std::cout << std::endl;
return 0;
}
#include<iostream>
#include<algorithm>
#include<list>
#include<string>
void elimDups(std::list<std::string> &words)
{
words.sort();
words.unique();
}
int main()
{
std::list<std::string> words{"123","234","12","246","123","456","246","1","234"};
elimDups(words);
for(const std::string &word : words)
std::cout << word << " ";
std::cout << std::endl;
return 0;
}
......@@ -123,4 +123,47 @@ inserter 是在容器的一个位置插入
[10.33程序代码](10.33.cpp)
* **练习10.34**
[10.34程序代码](10.34.cpp)
* **练习10.35**
[10.35程序代码](10.35.cpp)
* **练习10.36**
[10.36程序代码](10.36.cpp)
* **练习10.37**
[10.37程序代码](10.37.cpp)
* **练习10.38**
1. 输入迭代器
== != ++ 解引用和-> 仅用于读取
2. 输出迭代器
++ 解引用 仅用于输出
3. 前向迭代器
== != ++ 解引用和-> 读取和输出
4. 双向迭代器
== != ++ -- 解引用和-> 读取和输出
5. 随机访问迭代器
== != ++ -- +n -n [n] it1-it2 解引用和-> 读取和输出
* **练习10.39**
list的迭代器属于双向迭代器
vector上的迭代器属于随机访问迭代器
* **练习10.40**
copy要求输入迭代器和输出迭代器
reverse要求双向迭代器
unique要求前向迭代器
* **练习10.41**
replace(beg, end, old_val, new_val);
[beg, end)中用新值替换旧值
replace_if(beg, end, pred, new_val);
在[beg, end)中调用pred非0的用新值替换旧值
replace_copy(beg, end, dest, old_val, new_val);
在[beg, end)中用新值替换旧值,复制到dest中
replace_copy_if(beg, end, dest, pred, new_val);
在[beg, end)中调用pred非0的用新值替换旧值,复制到dest中
* **练习10.42**
[10.42 用list改写elimDups程序代码](10.42.cpp)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册