Afficher un message
Vieux 06/02/2008, 16h19   #9
Reetesh Mukul
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting a substring to Integer

On Feb 6, 6:35 am, "Daniel T." <danie...@earthlink.net> wrote:
> Reetesh Mukul <reetesh.mu...@gmail.com> wrote:
> > You can try this:-

>
> > #include <iostream>
> > #include <sstream>
> > #include <cctype>

>
> > int main()
> > {
> > std::string s = "dsdhjsahdk dsdjdsaj 36782367 sdjdhak";

>
> > int j = 0;
> > std::stringstream cstr;
> > cstr << s;
> > int i = 0;
> > char ch;

>
> > while( cstr >> ch )
> > {
> > if( std::isdigit(ch) )
> > {
> > cstr.putback(ch);
> > break;
> > }

>
> From here:
> > if( ( j = s.find(" ",j) )== std::string::npos )
> > {
> > cstr.seekg(-1);
> > break;
> > }

>
> > cstr.seekg(++j, std::ios::beg);

>
> to here. Is this block of code necessary?
>
>
>
> > }

>
> > if(cstr)
> > {
> > cstr >> i;
> > }
> > std::cout << i;

>
> > return 0;
> > }

>
> Wouldn't this be a simpler way of doing the same thing?
>
> int main()
> {
> std::string s = "dsdhjsahdk dsdjdsaj 36782367 sdjdhak";
>
> int i = 0;
> std::stringstream cstr( s );
> char ch = 0;
> while( cstr >> ch && !isdigit( ch ) )
> { }
> cstr.putback( ch );
> cstr >> i;
> cout << i;
>
> }


Yes, indeed.
  Réponse avec citation
 
Page generated in 0,05079 seconds with 9 queries