转换为整数的字符不是顺序的

时间:2013-01-18 20:30:11

标签: c++ casting char sequential

cout << key;    
cout << " " << temp_char << " - " << key_char << " " << (temp_char-key_char) << endl;

enter image description here

    /*
 * Function: StringToInteger
 * Usage: n = StringToInteger(s);
 */

int StringToInteger(string str)
{
    int returnVal;
    istringstream result(str);
    string rest;

    if ((result >> returnVal).fail()) {
    Error("Not a valid number!");
    }
    result >> rest;
    if (rest != "") {
    Error("Extra characters not allowed");
    }
    return returnVal;
}

void Lexicon::buildMapFromFile(string filename )  //map
{
    ifstream file;
    file.open(filename.c_str(), ifstream::binary);
    string mem, key;
    string wow;
    unsigned int x = 0;

    cout << endl;
    string temp;

    while(true) {
        getline(file, wow);
        if (file.fail()) break; //boilerplate check for error
        while (x < wow.length() ) {
            if (wow[x] == ',') { //look for csv deliniator
                key = mem;
                mem.clear();
                x++; //step over ','
            } else 
                mem += wow[x++];
        }

        char key_char = (char)StringToInteger(key); //cast integer into a char
        key = key_char; //change from char to string for the map

        cout << key_char << " is " << (temp[0]-key[0]) << " " << mem << " " << endl;

        tokenToChar_map[key] = mem; //char to string
        charToToken_map[mem] = key; //string to char
        mem.clear(); //reset memory
        x = 0;//reset index
        temp = key;
    }
    //printf("%d\n", tokenToChar_map.size());
    file.close();
}

输入文件的一部分:

103,089
104,090
105,091
106,092
107,093
108,094
109,095
110,096
111,097
112,098
113,099
114,100
115,101
116,102
117,103
118,104
119,105
120,106
121,107
122,108
123,109
124,110
125,111
126,112
127,113
128,114
129,115
130,116
131,117
132,118
133,119
134,120
135,121
136,122
137,123
138,124
139,125
140,126
141,127
142,128
143,129
144,130
145,131
146,132
147,133
148,134
149,135
150,136
151,137
152,138
180,RW4
181,R1
182,RW1
183,NBE
184,RW3
185,NB0
186,D1
187,EPS
188,C0
189,0M0
190,C1
191,RWY
192,D2
193,SB0
194,SBE
195,R2
196,RW2
197,RW5
210,0A1
211,0B2
212,0B3
213,0B4
214,0A5
250,*
251,?
252,z
253,test
254,test
255,test

整数7,8,9,10,11,12和13没有关联的字符

整数114 -255>,180 28,210 13和250 36。距离他们的前任整数

超过1

发生了什么,为什么这些不顺序?

1 个答案:

答案 0 :(得分:1)

查看输入数据:

[...]
152,138
180,RW4
[...]
197,RW5
210,0A1
[...]
214,0A5
250,*
[...]

(char)StringToInteger("152") - (char)StringToInteger("180")生成-28有什么问题?

如果您无法告诉我们您具体您希望程序输出的内容,我们无法帮助您解决此问题。