forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex9_50.cpp
36 lines (31 loc) · 810 Bytes
/
ex9_50.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
// @Yue Wang
//
// Exercise 9.50:
// Write a program to process a vector<string>s whose elements represent integral values.
// Produce the sum of all the elements in that vector.
// Change the program so that it sums of strings that represent floating-point values.
//
#include <iostream>
#include <string>
#include <vector>
auto sum_for_int(std::vector<std::string> const& v)
{
int sum = 0;
for (auto const& s : v)
sum += std::stoi(s);
return sum;
}
auto sum_for_float(std::vector<std::string> const& v)
{
float sum = 0.0;
for (auto const& s : v)
sum += std::stof(s);
return sum;
}
int main()
{
std::vector<std::string> v = { "1", "2", "3", "4.5" };
std::cout << sum_for_int(v) << std::endl;
std::cout << sum_for_float(v) << std::endl;
return 0;
}