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
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.
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
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
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.
Re: Reading from a map using indexing.
Quote:
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.
Re: Reading from a map using indexing.
Quote:
Originally Posted by
laserlight
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
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
Quote:
The instruction at "0x00404d90" referenced memory at "0x00000000". The memory could not be "read".
for the asd definiton statement.