【问题标题】:casted integers to chars are not sequential将整数转换为字符不是连续的
【发布时间】: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_charkey_char有哪些类型? charunsigned char?

标签: c++ casting char sequential


【解决方案1】:

查看您的输入数据:

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

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

如果你不能告诉我们你具体你希望你的程序输出什么,我们无法帮助你解决这个问题。

【讨论】:

  • 是的,是的,是的……我应该被扔进脑死堆;我将输入文件重新排序为连续的 0 到 255,不确定如何引入从 152 到 180 的中断,但确实如此,我没有注意到
  • 我正在审查我最被否决的问题,以从过去的错误中吸取教训。这个问题应该被删除——我认为这个问题对任何人都没有帮助。除了在堆栈上询问之前检查您的工作的课程。
猜你喜欢
  • 2022-11-10
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 2016-09-01
  • 1970-01-01
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多