tags:

views:

117

answers:

5
+5  A: 

The code is equivalent to:

weight[65] = 500.98;
weight[80] = 455.49;

Which of course only works if the vector holds at least 81 elements.

sepp2k
Thanks, I have corrected it
ladyfafa
@sepp2k: It looks like it holds 128 so we should be fine.
Martin York
+5  A: 

You should not. Use std::map for that purpose

For example

std::map<char,double> Weight;

Weight.insert(std::make_pair('A',500.98)); //include <algorithm>
Weight.insert(std::make_pair('P',455.49));

std::cout<< Weight['A']; //prints 500.98

You can also iterate over the map using std::map<char,double>::iterator

For example

std::map<char,double>::iterator i = Weight.begin();
for(; i != Weight.end(); ++i)
  std::cout << "Weight[" << i->first << "] : " << i->second << std::endl;

/*prints 
    Weight['A'] : 500.98
    Weight['P'] : 455.49
*/
Prasoon Saurav
@Prasoon, Okay, I will change it
ladyfafa
A character is an integer type so is a valid index into an array (or vector). By changing from a vector to map you are changing the access charastics from O(1) to O(log(n)) not a good trade off.
Martin York
@Martin : I never said it was not a valid index. I just said that he/she should not use that because that would indeed be more confusing. Using a `std::map` is cleaner. `you are changing the access charastics from O(1) to O(log(n)) not a good trade off`. That would have a significant impact if n is a very large value. For a small value of `n` and looking at the cleaner syntax of `std::map` I would recommend it. This was not at all a good reason to downvote. You should downvote when the answer is completely wrong or misleading.
Prasoon Saurav
@Prasoon Saurav: I do consider the answer wrong. And I disagree that using std::map is cleaner to me is seems to complicate things (and complicating a simple concept is a bad trade off). But thanks on the tutorial on how and why to vote.
Martin York
@Martin York : You make me laugh. What does your definition of `wrong` say? `But thanks on the tutorial on how and why to vote`. My pleasure.
Prasoon Saurav
Martin York
@Martin : So the community is with you. Kudos! :) BTW no offense meant :)
Prasoon Saurav
+6  A: 

Character literals (like 'A' and 'P') can be automatically converted to integers using their ASCII values. So 'A' is 65, 'B' is 66, etc.

So your code is the same as:

weight[65] = 500.98;
weight[80] = 455.49;

The reason you'd ever want to do this is if the weight array has something to do with characters. If it does, then assigning weights to a character literal makes the code more readable than assigning to an integer. But it's just for "documentation", the compiler sees it as integers either way.

SoapBox
@SoapBox: Okay, I see, these character literals can be converted to ints
ladyfafa
For counting the number of occurences of ASCII characters (for example prior to compute a Huffman coding tree) it's okay, however changing the character set to hold more than the lower ASCII would invalidate this scheme.
Matthieu M.
A: 

If you want this, you could use a std::map<char, double>. Technically, it would also be possible using a std::vector<double>, but there'd be all sorts of integral conversions from characters to integers and the program would just be confusing.

DeadMG
@DeadMG: thanks!
ladyfafa
+1  A: 

So I understand that char literals are turned into Integers. Does C++ support extended ASCII table ?? For example if I had a

char * blah = 'z'+'z';

what would happen ??? eg.

'z' = 122 in ASCII

therefore

'z'+'z' = 244  ?? or ?? 
Muggen
Try :)char* blah = 'z';won't compile, as you are mixing a char ('z') and a char* (blah).If you use char blah: It depends whether a "char" is a "signed char" or "unsigned char" for your compiler. For mine, it is signed and above math results in -12.
Frank