hi can someone show me how to split numbers?
for example
12345678910
the output will be 12 34 56 78 910
can some one give me some idea?please?
Printable View
hi can someone show me how to split numbers?
for example
12345678910
the output will be 12 34 56 78 910
can some one give me some idea?please?
substr() ?
You can use 'stringstreams':
Code:#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int num = 123456;
//Turn num into a string:
ostringstream myOutputStr;
myOutputStr<<num;
string myStr = myOutputStr.str();
//Get a substring of num:
string str1 = myStr.substr(0,2);
//If you need to, turn the substring into an int:
istringstream myInputStr(str1);
int num1 = 0;
myInputStr>>num1;
//Display the result:
cout<<num1<<" * 2 = "<<num1 * 2<<endl;
return 0;
}
Question #1: *how* do you want to split them?
(Off Topic) Sorry but just had to ask, to 7stud:
Is using stringstreams better (faster) then using itoa and atoi to convert integers to strings and the way arround?
If so, why?
>> Is using stringstreams better (faster) then using itoa and atoi to convert integers to strings and the way arround? If so, why?
Does speed denote betterness? There are many qualifications for good code.
itoa() isn't part of the C++ standard. MS made it up.Quote:
Is using stringstreams better (faster) then using itoa and atoi to convert integers to strings and the way arround?
atoi is part of C89...
Yep, I was researching that, then I changed my post, and then I saw your post. :(