两个字符串的交叉和联合 [英] Intersection and union of two strings

查看:147
本文介绍了两个字符串的交叉和联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须消除字符串1中字符串2的任何出现,并找到两个字符串的交集。

I have to eliminates any occurrence of string 2 in string 1 and also find the intersection of two strings.

这是我试过的:

#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.

错误如下:

C2228: left of .erase must have class/struct/union
C2228: left of .find must have class/struct/union


推荐答案

很高兴在C ++中设置差异交集 union 算法已经在标准库中实现。

Happily, in C++ set difference, intersection, and union algorithms have already been implemented in the standard library. These can be applied to strings like any container class.

这里是一个演示(你可以用简单的 char 数组,但为了清晰起见,我使用 std :: string ):

Here is a demonstration (you could do this with simple char arrays, but I'm using std::string for clarity):

#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;
}

运行它!

如何在操作作为练习。

这篇关于两个字符串的交叉和联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆