two issues(string processing)

T

thomas

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?
 
M

Micah Cowan

thomas said:
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.
 
O

Ondra Holub

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;
}
 
T

thomas

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);


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.
 
M

Micah Cowan

thomas said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top