以下程序由 Teddy van Jerry (我自己)编写并运行,基本保证正确性。(有时可能会为优化程序超前使用某些内容)
Contents
Before we comb through the codes
(I am still working on this chapter.)
Review:
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter 1(第一章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter 2(第二章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter 3(第三章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter 4(第四章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter 5(第五章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter 6(第六章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter 7(第七章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter 8(第八章)
TIP:标有(C++/11)者为C++/11标准下可以使用的题,若为老版本应加以修改。
Exercise 9.1
(
a
)
(a)
(a) list
(中间要插入)
(
b
)
(b)
(b) deque
(只有头尾操作)
(
c
)
(c)
(c) vector
(没其他理由就用vector
)
Exercise 9.2
list<deque<int>> container;
Exercise 9.3
根据我的理解,迭代器使用end
为 one pass the last(最后一个元素的右边一个),目的在于兼容多种情况。当没有元素时,begin
=end
,这样不会出现begin
与end
位置颠倒的情况,而且是否为空判断很方便。此外,这也符合 C++ 的一些习惯,比如数组从 0 开始计,最后一个元素正好是数组容量减一,因而 for 循环是这么写的:
double arr[5] = {0, 1, 2, 3, 4};
for(int i = 0; i != 5; i++)
{
// do something
}
因而,类似地:
double arr[5] = {0, 1, 2, 3, 4};
for(auto iter = begin(arr); i != end(arr); i++)
{
// do something
}
std::vector<double> arr {0, 1, 2, 3, 4};
for(auto iter = arr.begin(); i != arr.end(); i++)
{
// do something
}
Exercise 9.4
bool hasFound(const std::vector<int> vec, int target)
{
for(auto iter = vec.begin(); iter != vec.end(); iter++)
{
if(*iter == terget) return true;
}
return false;
}
或者直接使用 range for。range for 可以使用的要求其实也是存在迭代器:
bool hasFound(const std::vector<int> vec, int target)
{
for(const auto& iter : vec)
{
if(*iter == terget) return true;
}
return false;
}
Exercise 9.5
如果找不到这个元素,则返回 end
。
std::vector<int>::std::iterator find(const std::vector<int> vec, int target)
{
for(auto iter = vec.begin(); iter != vec.end(); iter++)
{
if(*iter == terget) return iter;
}
return vec.end();
}
Exercise 9.6
在 list
中不支持迭代器 <
运算符,可以将其改为 !=
。
Exercise 9.7
vector<int>::size_type
Exercise 9.8
Read only:
list<string>::const_iterator
Read and write:
list<string>::iterator
Exercise 9.9
begin: plain
cbegin: const
Exercise 9.10
it1
iterator
it2
const_iterator
it3
const_iterator
it4
const_iterator
Exercise 9.11
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
inline void print(T container)
{
for (const auto& c_ : container)
{
cout << c_ << " ";
}
cout << endl;
}
int main(int argc, char** argv)
{
vector<int> vec1;
vector<int> vec2(5);
vector<int> vec3{ 0,1,2,3,4,5,6,7 };
vector<int> vec4 = { 100, 200, 300 };
vector<int> vec5(vec3);
vector<int> vec6 = vec4;
vector<int> vec7(5, -3);
vector<int> vec8(vec3.cbegin(), vec3.cend() - 2);
print(vec1);
print(vec2);
print(vec3);
print(vec4);
print(vec5);
print(vec6);
print(vec7);
print(vec8);
return 0;
}
Exercise 9.12
一整个容器:整个拷过去完事了
使用迭代器:可指定范围且容器类型可以不同(array
不可用)
Exercise 9.13
list<int> List;
vector<int> Vec;
vector<double> vec1(List.cbegin(), List.cend());
cevtor<double> vec2(Vec); // or use '='
Next Chapter
ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。