2015-12-15 49 views
-1

我想寫來檢查,如果一個字符串是一個字謎與否代碼。但是,我不斷收到錯誤的那個「你不能分配給一個恆定的變量」。我明白這意味着什麼,但是對此的解決方案是什麼?查找兩個字符串是否字謎或者在C++中

#include <iostream> 
    #include <algorithm> 
    #include <string> 
    using namespace std; 

    bool check_str(const string& a, const string& b) 
    { 

    // cant be the same if the lenghts are not the same 
    if (a.length() != b.length()) 
     return false; 
    //both the strings are sorted and then char by char compared 
    sort(a.begin(), a.end()); 
    sort(b.begin(), b.end()); 

    for (int i = 0; i < a.length(); i++) 
    { 
     if (a[i] != b[i]) //char by char comparison 
      return false; 
    } 

    return true; 
} 

int main() 
{ 
    string a = "apple"; 
    string b = "ppple"; 

    if (check_str(a, b)) 
    { 
     cout << "Yes same stuff" << endl; 
    } 
    else 
    { 
     cout << "Not the same stuff" << endl; 
    } 
    system("pause"); 
} 
+0

a和b是常數。你不能對它們進行排序。 – drescherjm

+2

隨着你已經得到的答案,請注意,你不需要明確地比較char字符。只需'返回一個== b;'將單獨比較字符。 –

+0

該修改的重點是什麼?另外,如前所述,你在函數結尾添加的'if ... else ...'可以簡化爲'return a == b;'。 –

回答

0

這兩個字符串都是常量引用,但您嘗試對它們進行排序。這明顯改變了字符串,因此是無效的。你可以複製你的字符串,或者通過值而不是引用傳遞。

7

你試圖std::sort你的輸入字符串,它會修改它們,但你也宣佈他們const(通過將它們傳遞爲const std::string&),禁止修改它們。

傳值,即

bool check_str(string a, string b) 

或非const引用,即

bool check_str(string& a, string& b) 

代替。後者會修改你的原始字符串,前者不會。另外,第一個變體將接受臨時對象,第二個變體不會。

在我看來,按值傳遞將何去何從一些函數調用check_str修改它的投入似乎和直覺的方式。

最後一句話:正如評論已經提到的,你不需要使用循環來比較字符串,你可以簡單地用a == b比較。

0

由於字符串常量是,你不能對它們進行排序。 讓他們的副本可能是一個解決辦法,但我認爲當字符串很大,它使空間複雜度爲O(n),如果要排序,使時間複雜度爲O(nlgn)。我喜歡這種方式,時間爲O(n),空間O(1):

#define SIZE CHAR_MAX + 1 
bool check(const char a[], const char b[]) { 
    if (strlen(a) != strlen(b)) return false; 
    int length = strlen(a); 
    int char_count_a[SIZE] = {0}; 
    int char_count_b[SIZE] = {0}; 
    for (int i = 0; i < length; ++i) char_count_a[a[i]]++; 
    for (int i = 0; i < length; ++i) char_count_b[b[i]]++; 
    for (int i = 0; i < SIZE; ++i) 
     if (char_count_a[i] != char_count_b[i]) return false; 
    return true; 
} 
+1

你確定'sizeof(char)'是你想要的嗎,而不是'CHAR_MAX + 1'嗎? –

+0

@BenjaminLindley是的......錯誤 – xhg

0

只是爲了好玩:

#include <algorithm> 
#include <string> 

bool check_str(const std::string& a, const std::string& b) { 
    if (a == b) 
     return true; 
    std::string temp(a); 
    std::sort(temp.begin(), temp.end()); 
    while (std::next_permutation(temp.begin(), temp.end()) 
     if (temp == b) 
      return true; 
    return false; 
} 
+0

葉奧爾德'O(N!)'algorithme。 – Barry

+0

這將需要幾天來檢查字符串與數百個字符,oops – xhg

+0

真棒解決方案 - 現在寫在ook! :-)) –

0

最簡單的工作代碼,用const引用參數

bool check_str(std::string const &a, std::string const &b) { 
    if (a == b)  return true; 
    std::string t1(a); 
    std::string t2(b); 
    std::sort(t1.begin(), t1.end()); 
    std::sort(t2.begin(), t2.end()); 
    if(t1 == t2) return true; 
    return false; 
} 

或具有傳遞值

bool check_str(std::string a,std::string b) { 
    if (a == b)  return true; 
    std::sort(a.begin(), a.end()); 
    std::sort(b.begin(), b.end()); 
    if(a == b) return true; 
    return false; 
} 
相關問題