[英]Intersection and union of two strings
I have to eliminates any occurrence of string 2 in string 1 and also find the intersection of two strings. 我必须消除字符串1中出现的字符串2,并找到两个字符串的交集。
Here is what I have tried: 这是我尝试过的:
#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);
}
I am getting compiler errors and also I am not sure what I am trying is correct or not. 我收到了编译器错误,我也不确定我在尝试是否正确。
The errors are as follows: 错误如下:
C2228: left of .erase must have class/struct/union
C2228: left of .find must have class/struct/union
Happily, in C++ set difference , intersection , and union algorithms have already been implemented in the standard library. 令人高兴的是,在C ++中,设置差异 , 交集和联合算法已经在标准库中实现。 These can be applied to strings like any container class. 这些可以应用于任何容器类的字符串。
Here is a demonstration (you could do this with simple char
arrays, but I'm using std::string
for clarity): 这是一个演示(您可以使用简单的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;
}
How you implement this in your operation
class is left as an exercise. 如何在operation
类中实现它作为练习。
如果是strcpy( string1...
编译那么string1
是一个char*
而不是一个std::string
。你似乎在混合std::string
C和C ++功能。选择一个并坚持下去(我说std::string
自你在做C ++)
This is C++ (not C: C doesn't have operator overloading) 这是C ++(不是C:C没有运算符重载)
You need to show us your class definition before we can help identify the compile error. 在我们帮助识别编译错误之前,您需要向我们展示您的类定义。
If you don't have a class definition for "operator", that alone explains the error :) 如果你没有“operator”的类定义,那就单独解释了错误:)
If you're using C++, you should probably be using standard C++ "string" (instead of C "char []" arrays). 如果您使用的是C ++,则应该使用标准C ++“string”(而不是C“char []”数组)。 Using "string" will also affect your implementation code. 使用“string”也会影响您的实现代码。
Q: This wouldn't be a homework assignment, would it? 问:这不是家庭作业,不是吗? If so, please add "homework" to your tags. 如果是这样,请在标签中添加“作业”。
Thanx in advance .. PSM 提前完成.. PSM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.