c++ - Converting a character range to int -


suppose have character range:

// example const char *valuebegin="123blahblah"; // beginning of value const char *valueend=valuebegin+3;    // 1 past end of value 

..., , want convert int:

int value=...// given valuebegin , valueend, calculate               // number stored starting @ valuebegin 

what c++11 ways that?

obviously can create std::string , use stoi, or copy temporary nul-terminated character array , it's easy (e.g., via atoi or strtol).

think way doesn't involve copying characters temporary array/object - in other words function works on character data in-place.

update:

lots of answers, please think before answer. range not nul terminated, hence need valueend . don't know beyond value (i.e., perhaps valueend beyond buffer containing value), if answer not use valueend, wrong. also, if answer creates temporary std::string object, not within guidelines of question.

use boost::lexical_cast:

std::cout << boost::lexical_cast<int>(sbegin, 3) << std::endl; 

this not create temporaries , supports kind of character range. it's quite fast.

if want avoid length specifier can use boost::iterator_range:

std::cout << boost::lexical_cast<int>(boost::make_iterator_range(begin, end)) << std::endl; 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -