Given the C++ vector as below:
vector<double> weight;
weight.resize(128, 0);
Can weight be used as:
weight['A'] = 500.98;
weight['P'] = 455.49;
What does this mean, and how does one use these values? Could anyone give me an example?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With