开发者

C++ - Issue when using .at() method with string

I have tried Googling and trying everything I can think of, but I can't overcome the problem. So I have come to the masters at StackOverflow!

I am having trouble using the .at() string method:

std::string letters = "QWERTYUIO";

// &(letters.at(0)) now evaluates to QWERTYUIO
// &(letters.at(5)) now evaluates to YUIO
// etc.

What I want is t开发者_如何学运维he nth character of the string, not a string starting at the nth character.

I tried the [] operator, but I now have a new problem. The code is currently (using the Allegro library, but I don't think that is affecting this):

std::string letters = "QWERTYUIO";
char letter;
char StringY[5];
int row = 0;

for(int l = 0; l <= 2; ++l){
    letter = letters[row + l];

    textout_ex(buffer, font, &letter, l * 30, 20, makecol(255, 255, 255), -1);

    sprintf(StringY, "%d", inventoryNum[row + l + 9]);
    textout_ex(buffer, font, StringY, l * 30, 20, makecol(255, 255, 255), -1);
}

What is happening is the letter variable seems to be appending the StringY of the previous iteration to itself for no apparent reason. This is driving me insane.

Any help is appreciated.

Many thanks,

Will.


at() returns a reference to the specified character in the string, so when you take its address with &, you get a pointer to that character (of type char *).

The problem is that you are then giving this pointer to something that expects a pointer to an array of chars. This also has the type char * so it type checks just fine, but it gets treated as a character array rather than a single character.

You probably want to just drop the & and use letters.at(5).


Some people suggested using operator[] instead. It would still cause the same issue if you wrote &(letters[5]). The difference between at() and operator[] is that the former does bounds checking while the latter does not.


see string::at

use it just as:

(letters.at(0));
(letters.at(5));


Use operator[].

char c = letters[5];

Edit: Your problem is the Allegro library. It's decades old and written in C. You really need something from the current century. For example, with your sprintf, then I sure hope it doesn't need more than 5 characters, and you never wrote a 0 terminator or anything like that.

You should move to something more modern, like Direct3D itself. D3DX has a Font class that can render text transparently without the user having to manually write it to a texture, and you can use your own C++ string handling instead of the disgusting handling in the code posted. This is at least a small improvement:

std::string letters = "QWERTYUIO";
std::stringstream strstream;    
for(int l = 0, row = 0; l <= 2; ++l){
    char letter;
    letter = letters[row + l];

    textout_ex(buffer, font, &letter, l * 30, 20, makecol(255, 255, 255), -1);

    strstream << inventoryNum[row + l + 9];
    textout_ex(buffer, font, strstream.str().c_str(), l * 30, 20, makecol(255, 255, 255), -1);
    strstream.clear();
}


Just say letters[5], or whatever position you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜