Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何检查两个字符串是否包含相等的字符?_C++ - Fatal编程技术网

C++ 如何检查两个字符串是否包含相等的字符?

C++ 如何检查两个字符串是否包含相等的字符?,c++,C++,我正在尝试创建一个函数,该函数将从用户处获取两个字符串,并检查它们是否包含相等的字符。例如test和set,这两个字符串都包含字母t和s,因此输出应该是true。我是几周前开始的,所以我在正确执行代码方面遇到了一些问题。有人能解释一下我做错了什么吗?谢谢大家 #include <iostream> #include <string> bool equal(char first, char second); using namespace std; int main(

我正在尝试创建一个函数,该函数将从用户处获取两个字符串,并检查它们是否包含相等的字符。例如test和set,这两个字符串都包含字母t和s,因此输出应该是
true
。我是几周前开始的,所以我在正确执行代码方面遇到了一些问题。有人能解释一下我做错了什么吗?谢谢大家

#include <iostream>
#include <string>

bool equal(char first, char second);

using namespace std;

int main() {
  string first;

  string second;

  bool equal;
  int se;
  cout << "enter a string " << endl;
  getline(cin, first);
  cout << "enter another string " << endl;
  getline(cin, second);
  equal(first ,second);

  if (equal) {
    cout << "strings are equal" << endl;
  } else if (!equal) {
    cout << "strings not " << endl;
  }
  return 0;
}

bool equal(string first, string second) {
  for (int i = 0; i < first.length(); i++) {
    for (int j = j + 1; i < second.length(); i++) {
      if (i == j)
        return true;
      else
        return false;
    }
  }
}
#包括
#包括
布尔相等(字符第一,字符第二);
使用名称空间std;
int main(){
先串;
弦秒;
布尔相等;
int-se;

coutequal()
的内部循环错误,应该是:

for (int j = 0; j < second.size(); j++)
    if (first[i] == second[j])
for(int j=0;j
而不是

for (int j = j + 1; i < second.length(); i++) {
    if (i == j)
for(int j=j+1;i
我们可以猜测错误消息(不过不要让我们这么做!)。您声明了
bool-equal(char-first,char-second);
但定义了
bool-equal(string-first,string-second)
。因此,当您尝试调用它时,编译器会抱怨参数类型不正确

下一个问题是在运行时,因为
equal
的实现工作不正常。但这是一个算法问题;您应该考虑它应该如何工作。当有一个匹配时,您不能返回,但当有一个不匹配时,您可以返回

使用
中的函数还有一个聪明的技巧,但我不会透露。不过有一个提示:很难比较“test”和“set”,因为字母的顺序不同。

您可以使用

bool contain_same_letters(const std::string& lhs, const std::string& rhs)
{
    return std::set<char>(lhs.begin(), lhs.end())
        == std::set<char>(rhs.begin(), rhs.end());
}
bool包含相同的字母(常量std::string和lhs,常量std::string和rhs)
{
返回std::set(lhs.begin(),lhs.end())
==std::set(rhs.begin(),rhs.end());
}

test==sett是否需要为true?它们都包含t和s,因此程序应输出包含类似字母的字符串。您能否澄清“包含相等字符”对您的意义?例如test和set,这两个字符串都包含字母t和s,因此输出应为true
bool equal(char first,char second);
bool equal
bool equal(string first,string second)
(和
std::equal
,感谢
使用名称空间std;
)…从注释中,OP想看看是否存在非空集交集。@juanchopanza:确实,OP想要什么,
“set”
“test”
对这两种解释都有效,但OP只是在
's'
't'
(忽略
'e'
)上证明它是正确的。谢谢你的提示!如果我使用bool equal(字符串第一,字符串第二)在参数中,它告诉我字符串是未声明的标识符。。当我试图声明函数时,它也会给我错误。@vladimrminchenko:你知道使用名称空间std的
做什么吗?现在看看你把它放在哪里了。是的,我的错。但是当我试图调用函数时,它仍然给我错误,而且它说类型名不是允许。@vladimrminchenko:在你还在学习的时候,看起来要努力一点。这几乎肯定是一个愚蠢的错误,就像
使用名称空间std;
的问题一样。你需要学习如何发现这些小错误,因为你会不断遇到它们(如果只是因为打字错误-我仍然偶尔插入:而不是a;当我错过sihft键时)看不到它..也许我调用函数的位置不对?