Skip to content

Commit

Permalink
Update ex9_50.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy authored and pezy committed Jun 24, 2015
1 parent 0e69d55 commit 8fed2a5
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions ch09/ex9_50.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! @Alan
//! @Yue Wang
//!
//! Exercise 9.50:
//! Write a program to process a vector<string>s whose elements represent integral values.
Expand All @@ -10,37 +10,27 @@
#include <string>
#include <vector>


int sum(const std::vector<std::string> &v);
float sum_f(const std::vector<std::string> &v);

int main()
{
std::vector<std::string> v = {"1","2","3","4.5"};
std::cout << sum(v)<<"\n";
std::cout << sum_f(v);
return 0;
}


int sum(const std::vector<std::string> &v)
int sum_for_int(const std::vector<std::string> const& v)
{
int sum=0;
for(auto &s : v)
{
int sum = 0;
for (auto const& s : v)
sum += std::stoi(s);
}

return sum;
}


float sum_f(const std::vector<std::string> &v)
float sum_for_float(const std::vector<std::string> const& v)
{
float sum = 0.0;
for(auto &s : v)
{
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;
}

0 comments on commit 8fed2a5

Please sign in to comment.