两个字符串的交集和并集
Intersection and union of two strings
我必须消除字符串1中出现的任何字符串2,并找到两个字符串的交集。
这是我尝试过的:
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"
class operation
{
public:
char string1[100];
char string2[50];
operation(){};
operation(char a[100], char b[50]);
operation operator+(operation);
operation operator-(operation);
operation operator*(operation);
};
operation::operation(char a[100], char b[50])
{
strcpy(string1, a);
strcpy(string2, b);
}
operation operation::operator +(operation param)
{
operation temp;
strcpy(param.string1, temp.string1);
strcpy(param.string2, temp.string2);
strcat(temp.string1, temp.string2);
return (temp);
}
operation operation::operator -(operation param)
{
operation temp;
strcpy(param.string1, temp.string1);
strcpy(param.string2, temp.string2) ;
for (int i = 0; i<strlen(temp.string2); i++)
{
temp.string1.erase(i, 1);
}
return (temp);
}
operation operation::operator *(operation param)
{
operation temp;
strcpy(param.string1, temp.string1);
strcpy(param.string2, temp.string2);
char result[50];
for(int i = 0; i<strlen(temp.string2); i++)
{
if( temp.string1.find( temp.string2[i] ) != string::npos )
result = result + temp.string2[i];
}
return (temp);
}
我得到编译器错误,我也不确定我正在尝试的是正确的或不。
错误如下:
C2228: left of .erase must have class/struct/union
C2228: left of .find must have class/struct/union
令人高兴的是,在c++中集合差分、交集和并集算法已经在标准库中实现了。它们可以像任何容器类一样应用于字符串。
下面是一个演示(您可以使用简单的char
数组,但为了清晰起见,我使用std::string
):
#include <string>
#include <algorithm>
#include <iostream>
int main()
{
std::string string1 = "kanu";
std::string string2 = "charu";
std::string string_difference, string_intersection, string_union;
std::sort(string1.begin(), string1.end());
std::sort(string2.begin(), string2.end());
std::set_difference(string1.begin(), string1.end(), string2.begin(), string2.end(), std::back_inserter(string_difference));
std::cout << "In string1 but not string2: " << string_difference << std::endl;
std::set_intersection(string1.begin(), string1.end(), string2.begin(), string2.end(), std::back_inserter(string_intersection));
std::cout << "string1 intersect string2: " << string_intersection << std::endl;
std::set_union(string1.begin(), string1.end(), string2.begin(), string2.end(), std::back_inserter(string_union));
std::cout << "string1 union string2: " << string_union << std::endl;
}
运行它!
如何在你的operation
类实现这是一个练习。
如果strcpy( string1...
编译,则string1
是char*
而不是std::string
。你似乎混合了C和c++的字符串功能。选择一个并坚持使用它(我会说std::string
,因为你正在做c++)
-
这是c++(不是C: C没有操作符重载)
-
在我们帮助识别编译错误之前,您需要向我们展示您的类定义。
-
如果你没有"operator"的类定义,这就解释了错误:)
-
如果你正在使用c++,你可能应该使用标准的c++ "string"(而不是C的"char[]"数组)。使用"string"也会影响你的实现代码
-
问:这不会是家庭作业吧?如果是,请在标签中添加"homework"
提前感谢……PSM
相关文章:
- 两个字符串在 c++ 中不相等
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 将字符串拆分为标记,并将标记拆分为两个单独的数组
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 比较两个字符串后卡在无限循环中
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 使用XOR查找O(n)-解决方案中的两个字符串是否为变位符
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 我需要帮助在一个类中输入两个字符串并获取其输出
- 这两个C++语句连接字符串有什么区别?
- 两个字符串之间的数学运算
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- 有没有办法我可以在 C++ 中使用 if 语句来说明如果一个字符串不是两个名称的 eqal,它会给出一个输出,ATM i
- 如何在 main 函数中输出两个新字符串C++?
- sscanf() 有两个字符串参数
- 我的字符串数组一次打印出前两个字符串
- 两个 c++ 字符串上的异或
- 函数返回两个字符串的第一个常用字符
- 目标是找到两个 c 字符串之间的公共前缀(必须使用特定的函数标头)