Stroustrup section 5.5 "vector of struct"

Discussion in 'C++' started by arnuld, Nov 8, 2006.

  1. arnuld

    arnuld Guest

    this is the code:

    -------------------------------------------------------------------------

    #include <iostream>
    #include <string>
    #include <vector>

    struct Pair {
    std::string name;
    double val;
    };

    std::vector<Pair> pairs;

    double& value(const std::string s) {
    for(int i=0; i < pairs.size(); i++)
    if(s == pairs.name) return pairs.val;

    Pair p = {s, -1.1};
    pairs.push_back(p); // pair added at the end

    return pairs[pairs.size() - 1].val;
    }


    int main() {

    Pair p0 = {"p0", 0.0};
    Pair p1 = {"p1", 1.0};
    Pair p2 = {"p2", 2.0};

    // add to "pairs"
    pairs.push_back(p0);
    pairs.push_back(p1);
    pairs.push_back(p2);

    const std::string s1;
    std::cout << "now we will check \"pairs\": ";
    std::cin >> s1;

    value(s1);
    }
    --------------------------------------------------------------------------

    basically it is a "vector" of "Pair" to which i added 3 "Pair values".
    then i called "value" function which returns a reference to "val"
    related to "string" & if "string" is not found in "pairs" then it
    simply creates a new "Pair" & adds it to the end of "pairs" .
    compilation gave me this error (using "g++ 4.1.1" on BLAG Linux. ):

    -------------------------------------------------------------------------
    [arnuld@localhost cpp]$ g++ 05_55-references.cpp

    05_55-references.cpp: In function 'int main()':
    05_55-references.cpp:41: error: no match for 'operator>>' in
    'std::cin >> s1'
    /usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/istream:131:
    note: candidates are: std::basic_istream<_CharT, _Traits>&
    std::basic_istream<_CharT,

    [SNIP]

    <_CharT, _Traits>::eek:perator>>(void*&) [with _CharT = char, _Traits =
    std::char_traits<char>]
    /usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/istream:230:
    note: std::basic_istream<_CharT, _Traits>&
    std::basic_istream<_CharT,
    _Traits>::eek:perator>>(std::basic_streambuf<_CharT, _Traits>*) [with
    _CharT = char, _Traits = std::char_traits<char>]

    [arnuld@localhost cpp]$
    -------------------------------------------------------------------------------

    what is wrong?
     
    arnuld, Nov 8, 2006
    #1
    1. Advertising

  2. arnuld

    Mike Wahler Guest

    "arnuld" <> wrote in message
    news:...
    > this is the code:


    Remarks in-line e
    >
    > -------------------------------------------------------------------------
    >
    > #include <iostream>
    > #include <string>
    > #include <vector>


    [...]

    > const std::string s1;


    Since you used the 'const' qualifer here, that's
    your promise that your code will *not* modify
    the object 's1'.

    > std::cout << "now we will check \"pairs\": ";
    > std::cin >> s1;


    .... but here you go trying to modify 's1'. :)

    >
    > value(s1);
    > }


    [...]
    > 05_55-references.cpp: In function 'int main()':
    > 05_55-references.cpp:41: error: no match for 'operator>>' in
    > 'std::cin >> s1'
    > /usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/istream:131:
    > note: candidates are: std::basic_istream<_CharT, _Traits>&
    > std::basic_istream<_CharT,


    [...]

    > what is wrong?


    You're trying to modify a const object. Not allowed.
    Remove 'const' from your definition of 's1'.

    I suspect this was just an 'absent-minded' thing you did, and you
    do understand what 'const' means. If not, say so, and we'll explain.

    -Mike
     
    Mike Wahler, Nov 8, 2006
    #2
    1. Advertising

  3. arnuld

    BobR Guest

    arnuld wrote in message
    <>...
    >this is the code:
    >
    >-------------------------------------------------------------------------
    >
    >#include <iostream>
    >#include <string>
    >#include <vector>
    >
    >struct Pair {
    > std::string name;
    > double val;
    >};
    >
    >std::vector<Pair> pairs;
    >
    >double& value(const std::string s) {
    > for(int i=0; i < pairs.size(); i++)
    > if(s == pairs.name) return pairs.val;
    >
    > Pair p = {s, -1.1};
    > pairs.push_back(p); // pair added at the end
    >
    > return pairs[pairs.size() - 1].val;
    >}
    >
    >
    >int main() {
    >
    > Pair p0 = {"p0", 0.0};
    > Pair p1 = {"p1", 1.0};
    > Pair p2 = {"p2", 2.0};
    >
    > // add to "pairs"
    > pairs.push_back(p0);
    > pairs.push_back(p1);
    > pairs.push_back(p2);
    >
    > const std::string s1;
    > std::cout << "now we will check \"pairs\": ";
    > std::cin >> s1;
    >
    > value(s1);
    >}
    >--------------------------------------------------------------------------
    >
    >basically it is a "vector" of "Pair" to which i added 3 "Pair values".
    >then i called "value" function which returns a reference to "val"
    >related to "string" & if "string" is not found in "pairs" then it
    >simply creates a new "Pair" & adds it to the end of "pairs" .
    >compilation gave me this error (using "g++ 4.1.1" on BLAG Linux. ):
    >
    >-------------------------------------------------------------------------
    >[arnuld@localhost cpp]$ g++ 05_55-references.cpp
    >
    >05_55-references.cpp: In function 'int main()':
    >05_55-references.cpp:41: error: no match for 'operator>>' in
    >'std::cin >> s1'
    >
    >[SNIP]
    >[arnuld@localhost cpp]$
    >----------------------------------------------------------------------------

    ---
    >
    >what is wrong?


    Look at std::string 's1'.

    > const std::string s1;


    The 'const' means you (or anything else in your program) is allowed to change
    it. Remove the 'const', and see if it will work.

    Usually you would use a const string like:

    const std::string name( "This will stay the same for the whole program" );

    --
    Bob R
    POVrookie
     
    BobR, Nov 8, 2006
    #3
  4. arnuld

    BobR Guest

    Re: Stroustrup section 5.5 "vector of struct" [correction]

    BobR wrote in message ...
    >
    >>what is wrong?

    >
    >Look at std::string 's1'.
    >> const std::string s1;

    >
    >The 'const' means you (or anything else in your program) is allowed to

    change
    >it. Remove the 'const', and see if it will work.


    The 'const' means you (or anything else in your program) is NOT allowed to
    change it. Remove the 'const', and see if it will work.

    --
    Bob R
    POVrookie
     
    BobR, Nov 8, 2006
    #4
  5. arnuld

    arnuld Guest

    Re: Stroustrup section 5.5 "vector of struct"

    > Mike Wahler wrote:

    > news:...
    > > this is the code:


    > Remarks in-line e


    what does that mean?

    > You're trying to modify a const object. Not allowed.
    > Remove 'const' from your definition of 's1'.


    > I suspect this was just an 'absent-minded' thing you did, and you
    > do understand what 'const' means. If not, say so, and we'll explain.


    i do understand what "const" means but that was really absent-minded
    thing i did. anyway i removed it & programme compiled & ran but it does
    not do what i intended. i also made some minor changes to function
    "value" to get output on my terminal:

    -----------------------------------------------------------
    double& value(const std::string s) {
    for(int i=0; i < pairs.size(); i++)
    if(s == pairs.name)
    {
    std::cout << pairs.val << "\n";
    return pairs.val;
    }

    Pair p = {s, -1.1};
    pairs.push_back(p); // pair added at the end
    std::cout << pairs[pairs.size() - 1].val << "\n";
    }
    -------------------------------------------------------------


    ------------------- OUTPUT --------------------------------
    [arnuld@localhost cpp]$ ./a.out
    now we will check "pairs": p2
    2
    [arnuld@localhost cpp]$ ./a.out
    now we will check "pairs": p9
    -1.1
    [arnuld@localhost cpp]$
    ---------------------------------------------------------------

    function "value" returns a "double&" not "int". then why did i get /2/
    which is of type /int/ instead of double /2.0/?
     
    arnuld, Nov 8, 2006
    #5
  6. arnuld

    Amit Guest

    Re: Stroustrup section 5.5 "vector of struct"

    Hi arnuld,

    If u want to view decimal precision values, u need to set some
    properties in cout

    try using these statments

    cout << showpoint;
    cout.precision(2);

    Regards.
    -Amit Gupta

    arnuld wrote:
    > > Mike Wahler wrote:

    >
    > > news:...
    > > > this is the code:

    >
    > > Remarks in-line e

    >
    > what does that mean?
    >
    > > You're trying to modify a const object. Not allowed.
    > > Remove 'const' from your definition of 's1'.

    >
    > > I suspect this was just an 'absent-minded' thing you did, and you
    > > do understand what 'const' means. If not, say so, and we'll explain.

    >
    > i do understand what "const" means but that was really absent-minded
    > thing i did. anyway i removed it & programme compiled & ran but it does
    > not do what i intended. i also made some minor changes to function
    > "value" to get output on my terminal:
    >
    > -----------------------------------------------------------
    > double& value(const std::string s) {
    > for(int i=0; i < pairs.size(); i++)
    > if(s == pairs.name)
    > {
    > std::cout << pairs.val << "\n";
    > return pairs.val;
    > }
    >
    > Pair p = {s, -1.1};
    > pairs.push_back(p); // pair added at the end
    > std::cout << pairs[pairs.size() - 1].val << "\n";
    > }
    > -------------------------------------------------------------
    >
    >
    > ------------------- OUTPUT --------------------------------
    > [arnuld@localhost cpp]$ ./a.out
    > now we will check "pairs": p2
    > 2
    > [arnuld@localhost cpp]$ ./a.out
    > now we will check "pairs": p9
    > -1.1
    > [arnuld@localhost cpp]$
    > ---------------------------------------------------------------
    >
    > function "value" returns a "double&" not "int". then why did i get /2/
    > which is of type /int/ instead of double /2.0/?
     
    Amit, Nov 8, 2006
    #6
  7. arnuld

    arnuld Guest

    Re: Stroustrup section 5.5 "vector of struct"

    Amit wrote:
    > Hi arnuld,
    >
    > If u want to view decimal precision values, u need to set some
    > properties in cout
    >
    > try using these statments
    >
    > cout << showpoint;
    > cout.precision(2);


    1st, do not top-post.

    2nd, you did not get my point. my input was "2.0" ( Pair p2 = { "p2",
    2.0} )

    i wanted to know why compiler changed it to /int 2/. on the contrary,
    if i input / Pair p3 = { "p3", 3.3} / i get 3.3 as output. so this time
    compiler did not change it to /int 3/. why so?
     
    arnuld, Nov 8, 2006
    #7
  8. arnuld

    BobR Guest

    Re: Stroustrup section 5.5 "vector of struct"

    Amit wrote in message
    <>...
    >Hi arnuld,
    >
    >If u want to view decimal precision values, u need to set some
    >properties in cout
    >
    >try using these statments
    >
    > cout << showpoint;
    > cout.precision(2);
    >
    >Regards.
    >-Amit Gupta


    Nice top-post. Been takeing lessons long?


    (http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.4).

    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    BobR, Nov 8, 2006
    #8
  9. arnuld

    BobR Guest

    Re: Stroustrup section 5.5 "vector of struct"

    arnuld wrote in message ...
    >Amit wrote:
    >> Hi arnuld,
    >> If u want to view decimal precision values, u need to set some
    >> properties in cout
    >>
    >> try using these statments
    >>
    >> cout << showpoint;
    >> cout.precision(2);

    >
    >1st, do not top-post.
    >
    >2nd, you did not get my point. my input was "2.0" ( Pair p2 = { "p2",
    >2.0} )
    >
    >i wanted to know why compiler changed it to /int 2/. on the contrary,
    >if i input / Pair p3 = { "p3", 3.3} / i get 3.3 as output. so this time
    >compiler did not change it to /int 3/. why so?


    What evidence do you have that it is an 'int'? What you see in the output may
    be very different than what the variable holds.

    Put this in main() and try it:

    {
    using std::cout;
    cout.setf( std::ios_base::fixed );
    cout.precision( 20 );
    double d1 = 8.126e9;
    cout <<"d1 = "<< d1 <<std::endl;

    cout.setf( std::ios_base::scientific );
    cout.precision(2);
    cout <<"d1 = "<< d1 <<std::endl;
    cout.precision(6);
    cout <<"d1 = "<< d1 <<std::endl;
    cout.setf( std::ios_base::fixed );
    }

    --
    Bob R
    POVrookie
     
    BobR, Nov 8, 2006
    #9
  10. arnuld

    arnuld Guest

    Re: Stroustrup section 5.5 "vector of struct"

    > BobR wrote:

    > What evidence do you have that it is an 'int'?


    becuase i get plain 2 ( not 2.0 )

    > What you see in the output may
    > be very different than what the variable holds.


    then how will i get the desired output? or how will i know what the
    variable holds?
    2nd, is it really necessary, practically, to know answers to these 2
    questions i have asked?

    BTW, what is the "type" of "2" (the output i got)?

    > Put this in main() and try it:
    >
    > {
    > using std::cout;
    > cout.setf( std::ios_base::fixed );
    > cout.precision( 20 );
    > double d1 = 8.126e9;
    > cout <<"d1 = "<< d1 <<std::endl;
    >
    > cout.setf( std::ios_base::scientific );
    > cout.precision(2);
    > cout <<"d1 = "<< d1 <<std::endl;
    > cout.precision(6);
    > cout <<"d1 = "<< d1 <<std::endl;
    > cout.setf( std::ios_base::fixed );
    > }


    this is the output from "g++ 4.1.1 on BLAG Linux":

    -------------------------------------------------------------------
    [arnuld@localhost ~]$ ./a.out
    d1 = 8126000000.00000000000000000000
    d1 = 8.1e+09
    d1 = 8.126e+09
    [arnuld@localhost ~]$
    ---------------------------------------------------------------------

    you have used what "Amit" told to me: / cout.precision(n) / with your
    mysterious / cout.setf( std::ios_base::fixed ); /
     
    arnuld, Nov 8, 2006
    #10
  11. Re: Stroustrup section 5.5 "vector of struct"

    * arnuld:
    >> BobR wrote:

    >
    >> What evidence do you have that it is an 'int'?

    >
    > becuase i get plain 2 ( not 2.0 )


    The presentation does not tell you anything about the type.

    Also, in the case of floating point it only tells you the value
    /approximately/, because the internal representation of a floating point
    number is finite, and thus cannot represent all numbers exactly.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 8, 2006
    #11
  12. arnuld

    arnuld Guest

    Re: Stroustrup section 5.5 "vector of struct"

    > Alf P. Steinbach wrote:
    > The presentation does not tell you anything about the type.


    :-(

    > Also, in the case of floating point it only tells you the value
    > /approximately/, because the internal representation of a floating point
    > number is finite, and thus cannot represent all numbers exactly.


    OK, now i got it

    thanks Alf

    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?


    these signatures taught me how irritating a top-post is. i never did
    that since i read your signatures some months ago. thanks for putting
    them.

    [OT]
    BTW, how can i put my signatures in my post so that they appear
    everytime automatically. i googled for it, i have signatures in my
    gmail but i am not able to know how to do the same at newsgroups.
    [/OT]
     
    arnuld, Nov 8, 2006
    #12
  13. arnuld

    BobR Guest

    Re: Stroustrup section 5.5 "vector of struct"

    arnuld wrote in message ...
    >> BobR wrote:

    >
    >BTW, what is the "type" of "2" (the output i got)?


    char.

    >
    >this is the output from "g++ 4.1.1 on BLAG Linux":
    >-------------------------------------------------------------------
    >[arnuld@localhost ~]$ ./a.out
    >d1 = 8126000000.00000000000000000000
    >d1 = 8.1e+09
    >d1 = 8.126e+09
    >[arnuld@localhost ~]$
    >---------------------------------------------------------------------
    >
    >you have used what "Amit" told to me: / cout.precision(n) / with your
    >mysterious / cout.setf( std::ios_base::fixed ); /
    >


    Doc: "The GNU C++ Iostream Library"
    "Choices in formatting"
    "Changing stream properties using manipulators"

    'setf()' is Set Flags.

    --
    Bob R
    POVrookie
     
    BobR, Nov 8, 2006
    #13
  14. arnuld

    Default User Guest

    Re: Stroustrup section 5.5 "vector of struct"

    arnuld wrote:


    > [OT]
    > BTW, how can i put my signatures in my post so that they appear
    > everytime automatically. i googled for it, i have signatures in my
    > gmail but i am not able to know how to do the same at newsgroups.
    > [/OT]



    Google Groups does not have that capability, along with many other
    features of a real newsreading system. A possible remedy is to get
    access to a genuine newsfeed, and then use the newsreader (there are
    many available) that has the features you desire.

    Your ISP may provide access to a news server. If not, an inexpensive
    alternative is http://news.individual.net (the German server), which
    costs 10 euro per year (about $13 US currently) for access to text
    newsgroups.

    There are some free ones. Generally their reliability and retention
    aren't as good as NIN, but they are worth looking into if you don't
    want to pay.




    Brian
     
    Default User, Nov 8, 2006
    #14
    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. arnuld
    Replies:
    7
    Views:
    371
    Salt_Peter
    Oct 30, 2006
  2. arnuld

    Stroustrup section 2.5.1

    arnuld, Oct 31, 2006, in forum: C++
    Replies:
    3
    Views:
    307
    Default User
    Oct 31, 2006
  3. arnuld
    Replies:
    2
    Views:
    297
    arnuld
    Nov 8, 2006
  4. arnuld
    Replies:
    6
    Views:
    385
    arnuld
    Nov 8, 2006
  5. arnuld

    Stroustrup section 1.5.4, word counting

    arnuld, Mar 9, 2007, in forum: C Programming
    Replies:
    3
    Views:
    310
    Keith Thompson
    Mar 10, 2007
Loading...

Share This Page