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 > two issues(string processing)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
two issues(string processing)

Réponse
 
LinkBack Outils de la discussion
Vieux 26/02/2008, 09h57   #1
thomas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut two issues(string processing)

1. about string processing

---input--
30
40 thomas chang
--input--

I want to read 30 to an integer, 40 to an integer, "thomas chang" as a
whole to a string;
I used two method:

---code---
cin>>num; getline(cin, string1, '\n');
---code---
then do some processing to string1, but I found that string1 is null.

---code--
cin>>num; cin>>score; cin>>name; //here "name" is a string
---code--
it also will not work for "name" variable.

How to handle it?

2. about "map".
map<string, int> map1;
I want to use "count_if(map1.begin(), map2.end(), great)" to count the
numbers of values which has a "int" greater than map1["thomas chang"].
So I expect the "great" function to be:

int great(const pair<string, int> &v1, const pair<string, int> &v2){
return v1.second > v2.second;
}

but I don't know how to pass the parameter "v2", can anyone ?
  Réponse avec citation
Vieux 26/02/2008, 11h25   #2
Micah Cowan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: two issues(string processing)

thomas wrote:
> 1. about string processing
>
> ---input--
> 30
> 40 thomas chang
> --input--
>
> I want to read 30 to an integer, 40 to an integer, "thomas chang" as a
> whole to a string;
> I used two method:
>
> ---code---
> cin>>num; getline(cin, string1, '\n');
> ---code---
> then do some processing to string1, but I found that string1 is null.


Yeah. The first (cin>>num) will read in the characters that match a
number, and stop reading right after that (I'm assuming you've imported
the std namespace). That leaves the first newline from the first line,
still in the input stream, waiting to be read. What's left to be read,
then, is "\n40 thomas chang\n", which is why you just get an empty string.

You could construct an istream::sentry object between the two
statements, so that any remaining whitespace (i.e., that newline) would
get swallowed:

istream::sentry s(cin);

> ---code--
> cin>>num; cin>>score; cin>>name; //here "name" is a string
> ---code--
> it also will not work for "name" variable.


What do you mean by "it also will not work"? What specifically happens?

I would expect it to read the string "thomas" into the variable called
"name". Is that what happens? Normally, reading into a string stops at
the first whitespace character.

> 2. about "map".
> map<string, int> map1;
> I want to use "count_if(map1.begin(), map2.end(), great)" to count the
> numbers of values which has a "int" greater than map1["thomas chang"].
> So I expect the "great" function to be:
>
> int great(const pair<string, int> &v1, const pair<string, int> &v2){
> return v1.second > v2.second;
> }
>
> but I don't know how to pass the parameter "v2", can anyone ?


Probably use a binder. Something similar to:

count_if(map1.begin(), map2.end(), bind2nd( ptr_fun(great), v2 ));
^^
Needs <functional>. Except my example won't actually work, because it
would involve instantiation of a "reference to a reference" type
(there's no ptr_fun_ref() :/ ). Read about functors, function adapters,
and binders to figure out what to do.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
  Réponse avec citation
Vieux 26/02/2008, 14h41   #3
Ondra Holub
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: two issues(string processing)

On 26 Ún, 10:57, thomas <FreshTho...@gmail.com> wrote:
> 1. about string processing
>
> ---input--
> 30
> 40 thomas chang
> --input--
>
> I want to read 30 to an integer, 40 to an integer, "thomas chang" as a
> whole to a string;
> I used two method:
>
> ---code---
> cin>>num; getline(cin, string1, '\n');
> ---code---
> then do some processing to string1, but I found that string1 is null.
>
> ---code--
> cin>>num; cin>>score; cin>>name; //here "name" is a string
> ---code--
> it also will not work for "name" variable.
>
> How to handle it?

1. Read it this way:

int n1, n2;
std::string text;

std::cin >> n1 >> n2;
std::getline(std::cin, text);

// Maybe you will need to remove leading white spaces from text

>
> 2. about "map".
> map<string, int> map1;
> I want to use "count_if(map1.begin(), map2.end(), great)" to count the
> numbers of values which has a "int" greater than map1["thomas chang"].
> So I expect the "great" function to be:
>
> int great(const pair<string, int> &v1, const pair<string, int> &v2){
> return v1.second > v2.second;
> }
>
> but I don't know how to pass the parameter "v2", can anyone ?


2. You cannot use great function, because it takes 2 arguments, but
for count_if you need one-argument function. So you can compare with
some fixed value with binder as mentioned Micah Cowan or you can write
your own function:

int great(const pair<string, int> &v1){
return v1.second > 10;
}
  Réponse avec citation
Vieux 28/02/2008, 01h16   #4
thomas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: two issues(string processing)


> You could construct an istream::sentry object between the two
> statements, so that any remaining whitespace (i.e., that newline) would
> get swallowed:
>
> istream::sentry s(cin);
>
> > ---code--
> > cin>>num; cin>>score; cin>>name; //here "name" is a string
> > ---code--
> > it also will not work for "name" variable.

>
> What do you mean by "it also will not work"? What specifically happens?
>
> I would expect it to read the string "thomas" into the variable called
> "name". Is that what happens? Normally, reading into a string stops at
> the first whitespace character.
>


thanks. well.., for "it also will not work", I mean that it will not
read the entire line (here "thomas chang"), which is what I need.
  Réponse avec citation
Vieux 28/02/2008, 04h05   #5
Micah Cowan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: two issues(string processing)

thomas wrote:
>> What do you mean by "it also will not work"? What specifically happens?
>>
>> I would expect it to read the string "thomas" into the variable called
>> "name". Is that what happens? Normally, reading into a string stops at
>> the first whitespace character.
>>

> thanks. well.., for "it also will not work", I mean that it will not
> read the entire line (here "thomas chang"), which is what I need.


Ah. Well for that, you want to be using getline(), rather than the
extraction operator.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
  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 07h18.


É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,11924 seconds with 13 queries