Skip to content

Commit 8fed2a5

Browse files
Mooophypezy
authored andcommitted
Update ex9_50.cpp
1 parent 0e69d55 commit 8fed2a5

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

ch09/ex9_50.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! @Alan
1+
//! @Yue Wang
22
//!
33
//! Exercise 9.50:
44
//! Write a program to process a vector<string>s whose elements represent integral values.
@@ -10,37 +10,27 @@
1010
#include <string>
1111
#include <vector>
1212

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)
2714
{
28-
int sum=0;
29-
for(auto &s : v)
30-
{
15+
int sum = 0;
16+
for (auto const& s : v)
3117
sum += std::stoi(s);
32-
}
33-
3418
return sum;
3519
}
3620

37-
38-
float sum_f(const std::vector<std::string> &v)
21+
float sum_for_float(const std::vector<std::string> const& v)
3922
{
4023
float sum = 0.0;
41-
for(auto &s : v)
42-
{
24+
for (auto const& s : v)
4325
sum += std::stof(s);
44-
}
4526
return sum;
4627
}
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+
}

0 commit comments

Comments
 (0)