SetUnion函数的字符串数组参数

C++ - String Array Parameter for SetUnion function

本文关键字:数组 参数 字符串 函数 SetUnion      更新时间:2023-10-16

我写了一个函数来计算两个集合的并集。

我遇到了几个编译错误,我相信这部分是由于我如何制作StringUnion数组并声明它,但到目前为止我所做的一切都不起作用。

这是我的头文件。

#ifndef StringSet_header
#define StringSet_header
#include <memory>
#include <string>
using std::string;
using std::unique_ptr;
using std::make_unique;
class StringSet{
public:
    //create an empty set
    StringSet() = default;
    StringSet(int capacity);
    //copy a set
    StringSet(const StringSet &);
    StringSet& operator[](const int);
    //Insert a string to the set
    bool insert(string);
    //Remove a string from the set
    bool remove(string);
    //Test whether a string is in the set
    int find(string) const;
    //Get the size of the set
    int size() const;
    //get string at position i
    string get(int i) const;
    //Return the set union of the set and another StringSet
    StringSet setunion(const StringSet&) const;
    //Return the intersection of the set and another StringSet
    StringSet intersection(const StringSet&) const;
    //Return the set diffference of the set and another StringSet
    StringSet difference(const StringSet&) const;
    //prevent default copy assignment
    StringSet& operator=(const StringSet&) = delete;
    int NOT_FOUND = -1;
    static constexpr int def_capacity {4};
private:
    int arrSize {def_capacity};
    int currentSize {0};
    unique_ptr<string[]> arr {make_unique<string[]>(def_capacity)};
};
#endif

这是SetUnion函数的实现。

StringSet StringSet::setunion(const StringSet &Array2) const
{
    StringSet StringUnion = make_unique<string[]>(arrSize);
    if (currentSize > 0)
    {
        for (auto i=0; i < currentSize; i++)
        {
            auto s = arr[i];
            StringUnion.insert(s);
        }
        for (auto i=0; i < Array2.currentSize; i++)
        {
            auto s = Array2[i];
            if (StringUnion.find(s) == NOT_FOUND)
            {
                StringUnion.insert(s);
            }
        }
    }
    else    
    {
        auto result = StringSet();
        return result;          //return empty StringSet}
    }
}

错误:

|error: conversion from 'std::_MakeUniq<std::basic_string<char> []>::__array {aka std::unique_ptr<std::basic_string<char> []>}' to non-scalar type 'StringSet' requested|
error: passing 'const StringSet' as 'this' argument discards qualifiers [-fpermissive]
error: no matching function for call to 'StringSet::find(StringSet&)'
error: no matching function for call to 'StringSet::insert(StringSet&)'

插入和查找工作的预期,我能够使用插入和查找函数在我的删除函数和其他一些,所以为什么我不能在这里使用它们?

在你的行

StringSet StringUnion = make_unique<string[]>(arrSize);

RHS使用c++14结构,它接受一个std::size_t,并返回一个内部指向数组的std::unique_ptr<std::string>

然而,LHS是一个StringSet对象。

您没有定义接受这种类型的构造函数,因此这是一个问题。

看看你的代码,StringSet确实有一个std::unique_ptr<std::string>成员,所以你可以添加一个接受这样一个对象的函数,并从它初始化成员。但是,由于您已经有了一个

,因此不清楚这样一个actor的好处是什么。
StringSet(int capacity);

实际上已经做了相同的事情。

正如Leon所写的,你应该使用这一行而不是你的

StringSet StringUnion(arrSize);

编译器提供的错误看起来很清楚。让我们检查一下。

  • 请求std::make_unique ...转换为非标量类型StringSet

这是因为函数std::make_unique的定义,它返回一个std::unique_ptr<T>。但是你试图将它分配给StringSet类型的值。没有构造函数或操作符可以从std::unique_ptr创建StringSet,所以编译器抱怨他不能这样做。

  • 错误:没有匹配'StringSet::find(StringSet&)'的函数

你的类StringSet有一个operator[],它返回对StringSet的引用,所以auto s = Array2[i];StringSet类型。但是您的函数findinsert需要std::string。由于没有构造函数可以提供从StringSetstd::string的隐式转换,编译器会报错。