剑指offer55 字符流中第一个不重复的字符(最典型错误)
生活随笔
收集整理的這篇文章主要介紹了
剑指offer55 字符流中第一个不重复的字符(最典型错误)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
典型并且基礎的錯誤:
class Solution { public://Insert one char from stringstreamvoid Insert(char ch){if(result[ch] == -1)result[ch] = index;else if(result[ch] >= 0)result[ch] = -2;index++;}//return the first appearence once char in current stringstreamchar FirstAppearingOnce(){char ch = '\0';int minindex = 999;for(int i = 0;i < 256;i++){if(result[i] >= 0){if(result[i] < minindex){minindex = result[i];ch = (char)i;}}}return ch;}int result[256];for(int i = 0;i < 256;i++)result[i] = -1;int index = 0; };錯誤顯示:
編譯錯誤:您提交的代碼無法完成編譯 In file included from a.cc:2: ./solution.h:29:5: error: expected member name or ';' after declaration specifiers for(int i = 0;i < 256;i++) ^~~ a.cc:13:19: warning: comparison of integers of different signs: 'int' and 'size_t' (aka 'unsigned long') [-Wsign-compare] for(int i = 0;i < strlen(str);i++) { ~ ^ ~~~~~~~~~~~ 1 warning and 1 error generated.c++的類中只能有屬性和方法(也可以說函數)。
方法就是你定義的類中的那些實現函數,屬性相當于類中的那些參數。
上面代碼中報錯的for循環,目的是對result數組進行初始化,但這個for循環不是一個函數,在類里面是不允許這種執行語句的,必須轉換成函數才行。因為result本身是這個class類的屬性,所以對result的初始化,其實相當于對類屬性的初始化,可以通過構造函數來實現。
構造函數本身就是實現初始化類對象的數據成員。
轉載于:https://www.cnblogs.com/ymjyqsx/p/7245726.html
總結
以上是生活随笔為你收集整理的剑指offer55 字符流中第一个不重复的字符(最典型错误)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#如何判断字符串是否含中文
- 下一篇: easyUI下datagrid嵌套显示