Reading from a map using indexing.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Reading from a map using indexing.

  1. #1
    Join Date
    Apr 2008
    Posts
    156

    Reading from a map using indexing.

    Hello,

    I would like to validate one of my approach.
    I am creating a map like

    std::map<unsigned int, double> key_value_pair;

    I insert the value like as follows

    1 25.0
    0 30.0
    5 90.0
    2 22.0

    Then i would like to read the value like as follows

    double value = key_value_pair[0];

    I am able to compile & there is no error.

    question is does this will give 30.0 always.?

    I am using VS2005.

    Please help.

    -Dave

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,269

    Re: Reading from a map using indexing.

    The following code

    Code:
    #include <iostream>
    #include <map>
    using namespace std;
    
    typedef pair<unsigned int, double> uidpair;
    typedef map<unsigned int, double> uidmap;
    
    int main()
    {
    uidmap kvp;
    
    	kvp.insert(uidpair(1, 25.0) );
    	kvp.insert(uidpair(0, 30.0) );
    	kvp.insert(uidpair(5, 90.0) );
    	kvp.insert(uidpair(2, 22.0) );
    
    	cout << 0 << " " << kvp[0] << endl;
    	cout << 1 << " " << kvp[1] << endl;
    	cout << 2 << " " << kvp[2] << endl;
    	cout << 5 << " " << kvp[5] << endl;
    
    	return 0;
    }
    always produces the output

    Code:
    0 30
    1 25
    2 22
    5 90
    Note that when accessing the map via the operator [] and the requested key does not match an element in the map, then the element is inserted using its default constructor.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2008
    Posts
    156

    Re: Reading from a map using indexing.

    Hi

    I am satisfy with the answer. But this approach is worth when using VS compiler.
    When i used this approach in 'Release' mode in VS 2005 a random failure is detected.(A null value returned)
    I ensured the key is valid always with a proper value.

    One more info from my issue :-
    Key is of type unsigned int and value is of type std::string.
    Code:
    std::map<unsigned int, std::string> key_value_pair;
    //! Reference :- http://www.cplusplus.com/reference/map/map/operator%5B%5D/
    Any restriction when using std::string with map, like a null character ?

    Any guess ...

    -Dave
    Last edited by Dave1024; September 21st, 2013 at 02:19 AM.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Reading from a map using indexing.

    There is no such thing as a null std::string. Empty yes, null, no.

    The code may compile, but storing nulls in a std::string is undefined behaviour.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

    Re: Reading from a map using indexing.

    Quote Originally Posted by Paul McKenzie
    The code may compile, but storing nulls in a std::string is undefined behaviour.
    I don't think so: std::string is not a null terminated string type, so there is nothing special about null characters with respect to std::string. Hence, I see no reason why storing a null character in a std::string will result in undefined behaviour.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,269

    Re: Reading from a map using indexing.

    When i used this approach in 'Release' mode in VS 2005 a random failure is detected.(A null value returned)
    I ensured the key is valid always with a proper value.
    What do you mean by a 'random' failure? Are you using map in multi-threaded code? Can you post a simple example that demonstrates the issue? Altering my simple test program in post #2 to have value of type std::string always produces the expected output on my computer.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Reading from a map using indexing.

    Quote Originally Posted by laserlight View Post
    I don't think so: std::string is not a null terminated string type, so there is nothing special about null characters with respect to std::string. Hence, I see no reason why storing a null character in a std::string will result in undefined behaviour.
    Maybe I should clarify.

    There is a difference between storing null characters in a std::string, and assigning a null to a std::string object.
    Code:
    std::string s = NULL;
    This is undefined behaviour. The reason is that the conversion rules for NULL will cause std::string to believe it is being assigned a pointer to char. Once that happens, all bets are off.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,269

    Re: Reading from a map using indexing.

    On my MSVC, the following code

    Code:
    #include "string"
    #include "iostream"
    using namespace std;
    
    int main()
    {
    string asd = NULL;
    
         cout << "asd: '" << asd << "'" << endl;
    }
    compiles but produces a run-time error
    The instruction at "0x00404d90" referenced memory at "0x00000000". The memory could not be "read".
    for the asd definiton statement.
    Last edited by 2kaud; September 21st, 2013 at 02:29 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center