c++ - 两个字符串的交集和并集

标签 c++

我必须消除字符串 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++ 中设置 difference , intersection , 和 union算法已经在标准库中实现。这些可以像任何容器类一样应用于字符串。

这是一个演示(您可以使用简单的 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;
}

Run it!

如何在 operation 类中实现它留作练习。

关于c++ - 两个字符串的交集和并集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7936654/

相关文章:

c++ - Windows中动态链接库的属性(dll)

c++ - 当我尝试清理STL vector 时发生未处理的异常

c++ - 如何在 C++ 中清晰地生成内联结果

c++ - 当你使用 C++ 时会发生什么?

c++ - 简单的 SFINAE 问题有条件地声明成员函数

c++ - 连接 boost::mpl::string

c++ - Qt离线安装器和兼容的c++编译器安装

c++ - 为什么我的串行通过串行监视器在 Arduino IDE 上打印两次?

c++ - 如何测试 shared_ptr 是空的还是什么都不拥有

c++ - Linux gnu++11,运行时获取 "Enable multithreading to use std::thread: Operation not permitted"