简体   繁体   中英

Assign value to a certain index of std::string

#include <iostream>
#include <string>
using namespace std;   
int main()
{
    string x("5");
    int y = 3;
    x[0] = char(y);
    cout << x;
    return 0;
}

In this sample code: I tried to cast integer y and assign it to the first character of the string x , I expected to see the value '3' inside the string, but I get a different value. what's the best way to assign integer value to an index of a string?

This:

x[0] = char(y);

Means "take the integer y (which is 3) and store it as the first byte of the string x."

The problem is that the integer 3 is not the character '3'. Look at an ASCII table: http://www.asciitable.com/ and see that the character '3' is actually the integer value 51. So you either need to change y to 51, or change your assignment to convert any integer 0-9 like this:

x[0] = '0' + y;

This works so long as y is between 0 and 9, because the clever designers of ASCII made those characters consecutive.

You seem to be confusing the digit "3" and the number three.

When you set y equal to three, you are setting it equal to the number three, the number that is "11" in binary, the number that is one more than two. That number is represented as "3" in base 10, but that is just one way to represent it. You can represent it as "III" or "one more than two" if you want. It's still the value three.

When you do cout << x; to output a character, that does not output the character in base ten. What would that even mean? So there is no reason to expect the representation of the number three in base ten when you do that.

I expected to see the value '3' inside the string, but I get a different value.

Your expectation is unreasonable. There is no connection between outputting characters and how numerical values are represented in base ten.

You did get some representation of the value three. Just not the digit we use to represent the value three in base 10. But, again, why would outputting a character have anything to do with representing values in base ten? It's a character output routine, not a numerical one. It doesn't try to represent numerical values in particular numerical bases.

Non-programmers and non-mathematicians don't usually have to think too deeply about the difference between values and representations of values. But it's vital for programmers to do so because we perform operations on pure values and also values that represent representations of values.

For example, if we convert the number one-hundred to a string, we will have a variable whose value consists of whatever values the system uses to represent the digits '1', '0', '0' in that sequence likely followed by something that indicates the end of the string. This will probably be forty-nine, forty-eight, forty-eight, zero on your system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM