[ create a new paste ] login | about

Link: http://codepad.org/cRfMp9vp    [ raw code | output | fork ]

C++, pasted on May 10:
#include <iostream>
#include <sstream>
#include <vector>
#include <string>

template<typename T, typename U> T my_converter(const U &rhs){
	T result; std::stringstream ss;
	ss << rhs; ss >> result;
	return result;
}

std::vector<std::string> func_(const std::string &line){
	std::vector<std::string> res;
	std::string buff;
	std::string::const_iterator 
		beg = line.begin(),  //beg = line.сbegin(), 
		end = line.end();    //end = line.end();

	for(; beg != end; ++beg)
	{
		if(isdigit(*beg))
		{
			do
			{
				buff += *beg++;
			} while( (beg != end) && isdigit(*beg));	

			res.push_back(buff);
			buff.clear();
		}
	}

	return res;
}

int main(){

	std::string line = "100dsfgdfsg100dsfg  300 fsg fdg 100dsfsg"; // 100 + 100 + 300 + 100 = 600
	std::vector<std::string> res = func_(line);
	std::vector<std::string>::const_iterator
		beg = res.begin(), //beg = res.cbegin() 
		end = res.end();   //end = res.cend();

	int res_sum = 0;
	for(; beg != end; ++beg){
		std::cout << *beg << " ";
		res_sum += my_converter<int>(*beg);
	}

	std:: cout << "\nResult: " << res_sum << '\n';

	return 0;
}


Output:
1
2
100 100 300 100 
Result: 600


Create a new paste based on this one


Comments: