2015-12-15 52 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; 
} 
相关问题