10.1 and 10.2
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
vector vec = {1,1,1,3,3,3,3,3,3,4,5,6,7,7};
cout << count(vec.cbegin(), vec.cend(), 3) << endl;
list vec1 = {"a", "a", "a", "dd", "d", "xx"};
cout << count(vec1.cbegin(), vec1.cend(), "a") << endl;
return 0;
}
10.3
//以后特别短的简单的就不写全了
cout << accumulate(vec.cbegin(), vec.cend(), 0) << endl;
10.4
会把 double 当 int 来算
10.5
容器里存的是c风格字符串的首地址(char*),它们不能用操作符比较
比较c风格字符串要用 strcmp
10.6
fill_n(c.begin, c.size(), 0);
10.7
(a)
copy函数中 copy的目的序列大小 >= 输入序列的大小(前两个迭代器框定的范围)
因此 加一句: vec.resize(lst.size());
或者: copy(lst.cbegin(), lst.cend(), back_inserter(vec));
(b)
vec.reverse(10);
这句话只是分配了空间,vec的size仍然是0, 修改方法和(a)一样
10.8
标准库算法只是把值传给迭代器,而改变容器的操作是由迭代器来完成的
10.9
void elimDups(vector &vec)
{
sort(vec.begin(), vec.end());
auto end_unique = unique(vec.begin(), vec.end());
vec.erase(end_unique, vec.end());
for (auto i : vec)
cout << i << " ";
cout << endl;
}
10.11
我照这教材来的,有漏洞,因为通过stable_sort后相同的单词并没有排在一起,这种情况下执行unique函数就无法达到目的。改造办法是补充 isShort函数,在size相同情况下继续比较字母排序
#include
#include
#include
using namespace std;
inline bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
void elimDups(vector &vec)
{
stable_sort(vec.begin(), vec.end(), isShorter);
auto end_unique = unique(vec.begin(), vec.end());
vec.erase(end_unique, vec.end());
for (auto &i : vec)
cout << i << " ";
cout << endl;
}
int main(int argc, char *argv[])
{
vector vec = {"dog", "cat", "orange", "apple", "fish", "bird", "cat", "dog"};
elimDups(vec);
return 0;
}
10.12
compareIsbn函数:
bool compareIsbn(const Sales_data& sd1, const Sales_data& sd2)
{
return sd1.isbn() < sd2.isbn();
}
sort函数:
sort(v.begin(), v.end(), compareIsdn);
10.13
#include
#include
#include
#include
using namespace std;
inline bool cmp(const string &s)
{
return s.size() >= 5;
}
bool fun(vector &v)
{
auto part = partition(v.begin(), v.end(), cmp);
if(*part == *v.begin())
return false;
for (auto it = v.begin(); it != part; it++)
cout << *it < vec = {"dog", "cat", "orange", "apple", "fish", "bird", "cat", "dog"};
fun(vec); //可以设定个值接受它,表示vec中是否有大于等于5个字符的元素
return 0;
}
10.14
[ ](const int &x, const int &y){return x + y;}
10.15
[x](int y){return x + y;}
10.17
sort(v.begin(), v.end(), [](const Sales_data& sd1, const Sales_data& sd2){return sd1.isbn() < sd2.isbn();});
10.20
count_if(v.cbegin(), v.cend(), [](const string &s){return s.size() > 6;});
10.21
auto mylambda = [&i](){
if(i == 0)
return true;
else
i--;
return false;
};
10.22
头文件可真多
#include
#include
#include
#include
#include
using namespace std;
using namespace std::placeholders;
bool cmp(const string &s, size_t size)
{
return s.size() <= size;
}
int main(int argc, char *argv[])
{
vector vec = {"dog", "cattttt", "orange", "apple", "fishhh", "bird", "cat", "dog"};
cout << count_if(vec.cbegin(), vec.cend(), bind(cmp, _1, 5)) <
10.23
1 + 第一个参量(函数)的参量个数
10.24
#include
#include
#include
#include
#include
using namespace std;
using namespace std::placeholders;
bool check_size(const int &i, size_t size)
{
return i > size;
}
int main(int argc, char *argv[])
{
string s = "12345";
vector vec = {2, 4, 5, 7, 3, 4, 6};
cout << *find_if(vec.cbegin(), vec.cend(), bind(check_size, _1, s.size())) <
10.26
back_inserter: 相当于push_back
front_inserter: 相当于push_font
inserter: 多了一个参数用于指定插入位置,注意插入元素后该迭代器指向插入位置的后一位
而insert(ite, value)的返回值是指向执行插入位置,这是它俩的不同点
10.27
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
vector vec = {2, 3, 5, 7, 3, 4, 6, 7, 1};
list lst;
sort(vec.begin(), vec.end());
unique_copy(vec.cbegin(), vec.cend(), inserter(lst, lst.begin()));
for (auto i : lst)
cout << i << " ";
cout << endl;
return 0;
}
//输出: 1 2 3 4 5 6 7
10.28
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
const vector vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
list lst1;
list lst2;
list lst3;
copy(vec.cbegin(), vec.cend(), inserter(lst1, lst1.begin()));
copy(vec.cbegin(), vec.cend(), back_inserter(lst2));
copy(vec.cbegin(), vec.cend(), front_inserter(lst3));
for (auto i : lst1)
cout << i << " ";
cout << endl;
for (auto i : lst2)
cout << i << " ";
cout << endl;
for (auto i : lst3)
cout << i << " ";
cout << endl;
return 0;
}
//输出:
//1 2 3 4 5 6 7 8 9
//1 2 3 4 5 6 7 8 9
//9 8 7 6 5 4 3 2 1
10.34 and 10.35
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
const vector vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
//10.34
for(auto rit = vec.crbegin(); rit != vec.crend(); rit++)
cout << *rit << " ";
cout << endl;
//10.35
for(auto rit = vec.cend(); rit != vec.cbegin(); )
cout << *--rit << " "; //注意哦, *cend()没值,需先--
cout << endl;
return 0;
}
10.36
find(lst.crbegin(), lst.crend(), 0);
10.37
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
vector vec ={0,1,2,3,4,5,6,7,8,9};
list lst;
copy(vec.crbegin() + 2, vec.crbegin() + 7, back_inserter(lst));
for(auto i : lst)
cout << i << " ";
cout << endl;
return 0;
}
//输出:
//7 6 5 4 3
10.38
Input iterators : ==, !=, ++, *, ->
Output iterators : ++, *
Forward iterators : ==, !=, ++, *, ->
Bidirectional iterators : ==, !=, ++, --, *, ->
Random-access iterators : ==, !=, <, <=, >, >=, ++, --, +, +=, -, -=, -(two iterators), *, ->, iter[n] == * (iter + n)
10.39
list 属于双向迭代器 (所以啊咱不能 itr += 2; 只能advance(itr, 2);)
vecor 属于随机访问迭代器 (咱还可以下标运算)
10.40
copy : 第一个和第二个参数是输入迭代器, 第三个参数是输出迭代器
reverse : 双向迭代器
unique : 前向迭代器
10.41
(a)在范围内,用new_val替代old_val
(b)在范围内,用new_val替代使pred为真多元素
(c)就是(a)的复制版本,不改变原容器,把改变的复制到新容器
(d)就是(b)的复制版本
10.42
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
list lst = {"cat", "dog", "apple", "dog", "bird", "pig", "cat"};
lst.sort();
lst.unique();
for(auto &i : lst)
cout << i << " ";
cout << endl;
return 0;
}
//输出:
//apple bird cat dog pig