File tree Expand file tree Collapse file tree 1 file changed +15
-25
lines changed Expand file tree Collapse file tree 1 file changed +15
-25
lines changed Original file line number Diff line number Diff line change 1
- // ! @Alan
1
+ // ! @Yue Wang
2
2
// !
3
3
// ! Exercise 9.50:
4
4
// ! Write a program to process a vector<string>s whose elements represent integral values.
10
10
#include < string>
11
11
#include < vector>
12
12
13
-
14
- int sum (const std::vector<std::string> &v);
15
- float sum_f (const std::vector<std::string> &v);
16
-
17
- int main ()
18
- {
19
- std::vector<std::string> v = {" 1" ," 2" ," 3" ," 4.5" };
20
- std::cout << sum (v)<<" \n " ;
21
- std::cout << sum_f (v);
22
- return 0 ;
23
- }
24
-
25
-
26
- int sum (const std::vector<std::string> &v)
13
+ int sum_for_int (const std::vector<std::string> const & v)
27
14
{
28
- int sum=0 ;
29
- for (auto &s : v)
30
- {
15
+ int sum = 0 ;
16
+ for (auto const & s : v)
31
17
sum += std::stoi (s);
32
- }
33
-
34
18
return sum;
35
19
}
36
20
37
-
38
- float sum_f (const std::vector<std::string> &v)
21
+ float sum_for_float (const std::vector<std::string> const & v)
39
22
{
40
23
float sum = 0.0 ;
41
- for (auto &s : v)
42
- {
24
+ for (auto const & s : v)
43
25
sum += std::stof (s);
44
- }
45
26
return sum;
46
27
}
28
+
29
+ int main ()
30
+ {
31
+ std::vector<std::string> v = { " 1" , " 2" , " 3" , " 4.5" };
32
+ std::cout << sum_for_int (v) << std::endl;
33
+ std::cout << sum_for_float (v) << std::endl;
34
+
35
+ return 0 ;
36
+ }
You can’t perform that action at this time.
0 commit comments