我正在尝试编写一个代码来检查字符串是否是一个字谜.但是我不断得到错误"你不能分配给一个恒定的变量".我理解这意味着什么,但是这个解决方法/解决方案是什么?
#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 …Run Code Online (Sandbox Code Playgroud)