【发布时间】:2013-01-18 20:30:10
【问题描述】:
cout << key;
cout << " " << temp_char << " - " << key_char << " " << (temp_char-key_char) << endl;
/*
* 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 、180 、210 和 250 与其前一个整数相差超过 1
这是怎么回事,为什么这些不是连续的?
【问题讨论】:
-
如果没有看到
StringToInteger()的实现、temp_char的值以及您对您要完成什么的解释,就无法知道发生了什么。 -
#StoryTeller (1) 添加了代码的其余部分 (2) 目标:用单个字符键映射的一组字符串。
-
114 与其前一个整数 113 相差不超过一。
-
那么您的
StringToInteger()函数中存在错误,或者您的输入已损坏。根据ideone.com/PhFmXi 看来是后者。 -
temp_char和key_char有哪些类型?char或unsigned char?
标签: c++ casting char sequential