forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex10_01_02.cpp
43 lines (37 loc) · 994 Bytes
/
ex10_01_02.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//!
//! @author Yue Wang
//! @date 01.12.2014
//!
//! Exercise 10.1:
//! The algorithm header defines a function named count that, like find,
//! takes a pair of iterators and a value.count returns a count of how
//! often that value appears.
//! Read a sequence of ints into a vector and print the count of how many
//! elements have a given value.
//!
//! Exercise 10.2:
//! Repeat the previous program, but read values into a list of strings.
//!
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
int main()
{
//! Exercise 10.1
std::vector<int> v{1,2,3,4,5,6,6,6,2};
std::cout << "ex 10.01: "
<< std::count(v.cbegin(), v.cend(), 6)
<< std::endl;
//! Exercise 10.2
std::list<std::string> ls = {"aa","aaa","aa","cc"};
std::cout << "ex 10.02: "
<< std::count(ls.cbegin(), ls.cend(), "aa")
<< std::endl;
return 0;
}
//! output:
//!
//ex 10.01: 3
//ex 10.02: 2