F
Frederick Ding
Hi,guys!
Please look at this problem, when I use "int i", it success, but when I
use string::size_type i, it fail with a out_of_range exception.
//---------------- begin----------------------------
int str_to_num(const string& s)
{
int num = 0;
int interval = 1;
int i; // success
//string::size_type i; //always cause a out_of_range exception
for (i = s.length() - 1; i >= 0; --i) {
num += static_cast<int>(s.at(i) - 'a' + 1) * interval;
interval *= 26;
}
return num;
}
//-----------------end------------------------------
//a b c .... z aa ab ac .... az ba bb bc .... yz za zb zc .... zz aaa aab
aac ..
//.. change these string to number:
//1 2 3 .... 26 27 28 29 .... 52 53 54 55 .... 676 677 678 679 .... 702 703
704
//705 ....
IDE/Compiler: Borland C++ Builder 6
I know that the string::at() function cause this out_of_range exception, but
why size_type cause this?
Any help is appreciated, thanks!
--
Please look at this problem, when I use "int i", it success, but when I
use string::size_type i, it fail with a out_of_range exception.
//---------------- begin----------------------------
int str_to_num(const string& s)
{
int num = 0;
int interval = 1;
int i; // success
//string::size_type i; //always cause a out_of_range exception
for (i = s.length() - 1; i >= 0; --i) {
num += static_cast<int>(s.at(i) - 'a' + 1) * interval;
interval *= 26;
}
return num;
}
//-----------------end------------------------------
//a b c .... z aa ab ac .... az ba bb bc .... yz za zb zc .... zz aaa aab
aac ..
//.. change these string to number:
//1 2 3 .... 26 27 28 29 .... 52 53 54 55 .... 676 677 678 679 .... 702 703
704
//705 ....
IDE/Compiler: Borland C++ Builder 6
I know that the string::at() function cause this out_of_range exception, but
why size_type cause this?
Any help is appreciated, thanks!
--