two issues(string processing)

Discussion in 'C++' started by thomas, Feb 26, 2008.

  1. thomas

    thomas Guest

    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?
     
    thomas, Feb 26, 2008
    #1
    1. Advertising

  2. thomas

    Micah Cowan Guest

    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/
     
    Micah Cowan, Feb 26, 2008
    #2
    1. Advertising

  3. thomas

    Ondra Holub Guest

    On 26 Ún, 10:57, 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.
    >
    > ---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;
    }
     
    Ondra Holub, Feb 26, 2008
    #3
  4. thomas

    thomas Guest


    > 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.
     
    thomas, Feb 28, 2008
    #4
  5. thomas

    Micah Cowan Guest

    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/
     
    Micah Cowan, Feb 28, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hubert Hung-Hsien Chang
    Replies:
    2
    Views:
    524
    Michael Foord
    Sep 17, 2004
  2. R. Bernstein
    Replies:
    7
    Views:
    499
    Steven Bethard
    Jan 26, 2006
  3. GenxLogic
    Replies:
    3
    Views:
    1,329
    andrewmcdonagh
    Dec 6, 2006
  4. philly_bob
    Replies:
    3
    Views:
    272
    Terry Reedy
    May 11, 2008
  5. JP SIngh

    ASP Processing - Performance Issues

    JP SIngh, Jul 29, 2005, in forum: ASP General
    Replies:
    3
    Views:
    156
    Bob Barrows [MVP]
    Jul 30, 2005
Loading...

Share This Page