Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > two issues(string processing)

Reply
Thread Tools

two issues(string processing)

 
 
thomas
Guest
Posts: n/a
 
      02-26-2008
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 help?
 
Reply With Quote
 
 
 
 
Micah Cowan
Guest
Posts: n/a
 
      02-26-2008
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 help?


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/
 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      02-26-2008
On 26 Ún, 10:57, thomas <(E-Mail Removed)> 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 help?


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;
}
 
Reply With Quote
 
thomas
Guest
Posts: n/a
 
      02-28-2008

> 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.
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      02-28-2008
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Router with NAT and two T1 and two ISP's John Cisco 4 10-16-2004 02:03 AM
Two ISP -Two Routers - 1 PIX James Parks Cisco 5 12-11-2003 08:55 PM
Cisco 2611 with Two T1 from two ISPs Adam Embrey Cisco 3 07-24-2003 10:22 PM



Advertisments