Skip to content

Instantly share code, notes, and snippets.

@wengxt
Last active August 29, 2015 14:04
Show Gist options
  • Save wengxt/fbbf84fcad5021416455 to your computer and use it in GitHub Desktop.
Save wengxt/fbbf84fcad5021416455 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main() {
std::string s = "13579";
int result = 0;
std::for_each(s.begin(), s.end(), [&](char n){result = result * 10 + n - '0'; });
std::cout << result << std::endl;
std::vector<int> v;
std::transform(s.begin(), s.end(), std::back_inserter(v), [](char n){ return n - '0';});
int x = 0;
std::for_each(v.begin(), v.end(), [&](int y){x = x * 10 + y;});
std::cout << x << std::endl;
std::cout << std::accumulate(s.begin(), s.end(), 0, [](int x, char y){ return x * 10 + y - '0';}) << std::endl;
std::vector<int> v2;
std::transform(s.begin(), s.end(), std::back_inserter(v2), [](char n){ return n - '0';});
std::cout << std::accumulate(v2.begin(), v2.end(), 0, [](int x, int y){ return x * 10 + y;}) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment