PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Is this code to find an int in a string okay ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Is this code to find an int in a string okay ?

Réponse
 
LinkBack Outils de la discussion
Vieux 06/12/2007, 14h51   #1
Diwa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Is this code to find an int in a string okay ?

Hi Guys,

Is there any better way than below to find an int in a string (e.g.
"30" in "KFStat30A")

// --------------------------------------------------------------
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
vector<string> strs ;
strs.push_back("KFStat30A");
strs.push_back("KFStat2A");
strs.push_back("555555");
strs.push_back("KKKKKK");
strs.push_back("KKKK555");

std::string str;

for (int i=0; i<strs.size(); i++)
{
str = "";

int beg = strs[i].find_first_of("0123456789");

if (beg != string::npos)
{
int end = strs[i].find_first_not_of("0123456789", beg);
str.assign(strs[i], beg, end-beg);
}

cout << strs[i] << " " << str << "\n";
}
return 0;
}

// --------------------------------------------------------------

Thanks
-- Diwa
  Réponse avec citation
Vieux 06/12/2007, 22h31   #2
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this code to find an int in a string okay ?

"Diwa" <shettydiwakar@gmail.com> wrote in message
news:85ec7280-977f-4651-a3f4-2199b4150d10@j44g2000hsj.googlegroups.com...
> Hi Guys,
>
> Is there any better way than below to find an int in a string (e.g.
> "30" in "KFStat30A")
>
> // --------------------------------------------------------------
> #include <iostream>
> #include <sstream>
> #include <vector>
>
> using namespace std;
>
> int main()
> {
> vector<string> strs ;
> strs.push_back("KFStat30A");
> strs.push_back("KFStat2A");
> strs.push_back("555555");
> strs.push_back("KKKKKK");
> strs.push_back("KKKK555");
>
> std::string str;
>
> for (int i=0; i<strs.size(); i++)
> {
> str = "";
>
> int beg = strs[i].find_first_of("0123456789");
>
> if (beg != string::npos)
> {
> int end = strs[i].find_first_not_of("0123456789", beg);
> str.assign(strs[i], beg, end-beg);
> }
>
> cout << strs[i] << " " << str << "\n";
> }
> return 0;
> }


This code looks okay. Although I wonder what you would want with a string
such as
"KFStat30A02". Your code could be described as finding the first continuous
number in a string. I really don't think you can simply if too much though
from what you already have, other than making it a function returning a
std::string.


  Réponse avec citation
Vieux 06/12/2007, 22h50   #3
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this code to find an int in a string okay ?

Jim Langston wrote:
> "Diwa" <shettydiwakar@gmail.com> wrote in message
> news:85ec7280-977f-4651-a3f4-2199b4150d10@j44g2000hsj.googlegroups.com...
>> Hi Guys,
>>
>> Is there any better way than below to find an int in a string (e.g.
>> "30" in "KFStat30A")
>>
>> // --------------------------------------------------------------
>> #include <iostream>
>> #include <sstream>
>> #include <vector>
>>
>> using namespace std;
>>
>> int main()
>> {
>> vector<string> strs ;
>> strs.push_back("KFStat30A");
>> strs.push_back("KFStat2A");
>> strs.push_back("555555");
>> strs.push_back("KKKKKK");
>> strs.push_back("KKKK555");
>>
>> std::string str;
>>
>> for (int i=0; i<strs.size(); i++)
>> {
>> str = "";
>>
>> int beg = strs[i].find_first_of("0123456789");
>>
>> if (beg != string::npos)
>> {
>> int end = strs[i].find_first_not_of("0123456789", beg);
>> str.assign(strs[i], beg, end-beg);
>> }
>>
>> cout << strs[i] << " " << str << "\n";
>> }
>> return 0;
>> }

>
> This code looks okay. Although I wonder what you would want with a
> string such as
> "KFStat30A02". Your code could be described as finding the first
> continuous number in a string. I really don't think you can simply
> if too much though from what you already have, other than making it a
> function returning a std::string.


I would probably add the ability to extract the substring as a number
in a given base. E.g., "KFStat30A02" in base 10 would yield "30" and
in base 16 would yield "F"... :-) Or make it smarter and able to
recognize the 'x' or 'X' after the leading 0...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 07/12/2007, 12h26   #4
Daniel T.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this code to find an int in a string okay ?

In article
<85ec7280-977f-4651-a3f4-2199b4150d10@j44g2000hsj.googlegroups.com>,
Diwa <shettydiwakar@gmail.com> wrote:

> Hi Guys,
>
> Is there any better way than below to find an int in a string (e.g.
> "30" in "KFStat30A")
>
> // --------------------------------------------------------------
> #include <iostream>
> #include <sstream>
> #include <vector>
>
> using namespace std;
>
> int main()
> {
> vector<string> strs ;
> strs.push_back("KFStat30A");
> strs.push_back("KFStat2A");
> strs.push_back("555555");
> strs.push_back("KKKKKK");
> strs.push_back("KKKK555");
>
> std::string str;
>
> for (int i=0; i<strs.size(); i++)
> {
> str = "";
>
> int beg = strs[i].find_first_of("0123456789");
>
> if (beg != string::npos)
> {
> int end = strs[i].find_first_not_of("0123456789", beg);
> str.assign(strs[i], beg, end-beg);
> }
>
> cout << strs[i] << " " << str << "\n";
> }
> return 0;
> }
>
> // --------------------------------------------------------------
>
> Thanks


What you have is fine, just wrap it into a function and it will look
like this:

string get_int( const string& s )
{
string result;
string::size_type front = s.find_first_of( "0123456789" );
if ( front != string::npos )
{
string::size_type back =
s.find_first_not_of( "0123456789", front );
result.assign( s, front, back - front );
}
return result;
}

.... or you can get creative:

bool is_digit( char c )
{
return isdigit( c );
}

string get_int( const string& s )
{
string::const_iterator front = find_if( s.begin(), s.end(),
&is_digit );
string::const_iterator back = find_if( front, s.end(),
compose1( logical_not<bool>(), ptr_fun( &is_digit ) ) );
return string( front, back );
}

(note: compose1 is part of STL, not part of the standard.)

Once you have the above function, you can operate on your array with
transform.

int main()
{
vector<string> strs ;
strs.push_back("KFStat30A");
strs.push_back("KFStat2A");
strs.push_back("555555");
strs.push_back("KKKKKK");
strs.push_back("KKKK555");

vector<string> ints;

transform( strs.begin(), strs.end(),
back_inserter( ints ), &get_int );

for ( int i = 0; i < strs.size(); ++i )
cout << strs[i] << ' ' << ints[i] << '\n';
}
  Réponse avec citation
Vieux 07/12/2007, 16h18   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this code to find an int in a string okay ?

On Dec 6, 3:51 pm, Diwa <shettydiwa...@gmail.com> wrote:
> Is there any better way than below to find an int in a string
> (e.g. "30" in "KFStat30A")


boost::regex.

If for some reason, you can't use Boost (most people probably
can't), then beg, borrow or steal some other regular expression
class. You don't want to do anything with text without one.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 07/12/2007, 18h53   #6
Diwa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this code to find an int in a string okay ?

On Dec 6, 5:31 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "Diwa" <shettydiwa...@gmail.com> wrote in message
>
> news:85ec7280-977f-4651-a3f4-2199b4150d10@j44g2000hsj.googlegroups.com...
> > Hi Guys,

>
> > Is there any better way than below to find an int in a string (e.g.
> > "30" in "KFStat30A")

>
> > // --------------------------------------------------------------
> > #include <iostream>
> > #include <sstream>
> > #include <vector>

>
> > using namespace std;

>
> > int main()
> > {
> > vector<string> strs ;
> > strs.push_back("KFStat30A");
> > strs.push_back("KFStat2A");
> > strs.push_back("555555");
> > strs.push_back("KKKKKK");
> > strs.push_back("KKKK555");

>
> > std::string str;

>
> > for (int i=0; i<strs.size(); i++)
> > {
> > str = "";

>
> > int beg = strs[i].find_first_of("0123456789");

>
> > if (beg != string::npos)
> > {
> > int end = strs[i].find_first_not_of("0123456789", beg);
> > str.assign(strs[i], beg, end-beg);
> > }

>
> > cout << strs[i] << " " << str << "\n";
> > }
> > return 0;
> > }

>
> This code looks okay. Although I wonder what you would want with a string
> such as "KFStat30A02".
> - Show quoted text -


The strings come from a database. The "30" in that string represents
the 30 yr treasury bond. Only the first two strings (KFStat30A and
KFStat2A) were valid ones. I had put the other three strings just for
testing purpose.
  Réponse avec citation
Vieux 07/12/2007, 18h56   #7
Diwa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this code to find an int in a string okay ?

On Dec 7, 7:26 am, "Daniel T." <danie...@earthlink.net> wrote:
> In article
> <85ec7280-977f-4651-a3f4-2199b4150...@j44g2000hsj.googlegroups.com>,
>
>
>
>
>
> Diwa <shettydiwa...@gmail.com> wrote:
> > Hi Guys,

>
> > Is there any better way than below to find an int in a string (e.g.
> > "30" in "KFStat30A")

>
> > // --------------------------------------------------------------
> > #include <iostream>
> > #include <sstream>
> > #include <vector>

>
> > using namespace std;

>
> > int main()
> > {
> > vector<string> strs ;
> > strs.push_back("KFStat30A");
> > strs.push_back("KFStat2A");
> > strs.push_back("555555");
> > strs.push_back("KKKKKK");
> > strs.push_back("KKKK555");

>
> > std::string str;

>
> > for (int i=0; i<strs.size(); i++)
> > {
> > str = "";

>
> > int beg = strs[i].find_first_of("0123456789");

>
> > if (beg != string::npos)
> > {
> > int end = strs[i].find_first_not_of("0123456789", beg);
> > str.assign(strs[i], beg, end-beg);
> > }

>
> > cout << strs[i] << " " << str << "\n";
> > }
> > return 0;
> > }

>
> > // --------------------------------------------------------------

>
> > Thanks

>
> What you have is fine, just wrap it into a function and it will look
> like this:
>
> string get_int( const string& s )
> {
> string result;
> string::size_type front = s.find_first_of( "0123456789" );
> if ( front != string::npos )
> {
> string::size_type back =
> s.find_first_not_of( "0123456789", front );
> result.assign( s, front, back - front );
> }
> return result;
>
> }
>
> ... or you can get creative:
>
> bool is_digit( char c )
> {
> return isdigit( c );
>
> }
>
> string get_int( const string& s )
> {
> string::const_iterator front = find_if( s.begin(), s.end(),
> &is_digit );
> string::const_iterator back = find_if( front, s.end(),
> compose1( logical_not<bool>(), ptr_fun( &is_digit ) ) );
> return string( front, back );
>
> }
>


What would the above func return if the string (e.g. KFStat30A55BB)
contains 2 ints instead of one ?
  Réponse avec citation
Vieux 07/12/2007, 18h59   #8
Diwa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is this code to find an int in a string okay ?

On Dec 7, 11:18 am, James Kanze <james.ka...@gmail.com> wrote:
> On Dec 6, 3:51 pm, Diwa <shettydiwa...@gmail.com> wrote:
>
> > Is there any better way than below to find an int in a string
> > (e.g. "30" in "KFStat30A")

>
> boost::regex.
>
> If for some reason, you can't use Boost (most people probably
> can't), then beg, borrow or steal some other regular expression
> class. You don't want to do anything with text without one.
>


Thanks. That was what I originally wanted to use since we are using
boost anyway in our code. But later we decided against it since we do
not require any regex parsing except for this one. Boost regex would
have been an overkill especially considering the fact that boost regex
requires its library to be linked to the app. (If I am not wrong)
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h15.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19991 seconds with 16 queries