通用集合操作类,即交集,并集,减等 [英] A generic set operations class i.e. intersection, union, minus etc

查看:246
本文介绍了通用集合操作类,即交集,并集,减等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个C ++类,它提供了对字符串向量和我自己的数据类型的向量进行集合运算。有没有任何简单的方法这样做,而不是为每个数据类型写一个不同的函数?到目前为止我已经编写了字符串向量的操作。下面显示了我的集合并集的示例:

  vector< string& SetOperations :: set_union(vector< string> set1,
vector< string> set2){
for(std :: vector< int> :: size_type i = 0; i< set1.size ; i ++){
set1.push_back(set2.at(i));
}
return set1;
}

所以我想要同样的东西,但 string my_data_type ,它是各种成员的结构。让我们假设它看起来像这样:

  struct my_data_type {
int label;
vector< string> X;
vector< string> y;
string str
};

每个数据类型的函数也不会像我的 set_union (...)函数,因为在设置交集的情况下,我需要测试 my_data_type >

此外,我对C ++非常新,所以对现有函数的任何意见也将受到赞赏。



解决方案

已经有这样的算法(union,intersection,sorting ...): http://www.cplusplus.com/reference/algorithm/



您的元素只需满足STL容器元素的要求(请参见 http:/ /msdn.microsoft.com/fr-fr/library/bb385469.aspx ):


插入的所有引用类型
到STL / CLR容器中必须至少有
a的以下元素:



公共拷贝构造函数。



公共赋值运算符。



公共析构函数。



container
如set和map必须定义一个public
比较运算符,它是
运算符<默认。容器上的一些操作
也可能需要一个
的public default构造函数和一个
的公共等价运算符来定义



类似于引用类型,值类型和
处理要引用到
的引用类型的句柄插入到关联的
容器中必须具有比较
运算符,例如operator<定义。
公共副本的要求
构造函数,公共赋值
运算符和公共析构函数
对于值类型不存在,或者对参考类型处理
。 p>

您可以在WikiBook上找到有关操作符重载的信息(要在自定义类中实现): http://en.wikibooks.org/wiki/C++_Programming/Operators/Operator_Overloading


I want to write a C++ class that offers set operations that work on vectors of strings and vectors of my own data type. Are there any easy ways of doing this rather than writing a different function for each data type? So far I have written operations for string vectors. Below shows an example of my set union:

vector<string> SetOperations::set_union(vector<string> set1,
                                        vector<string> set2) {
    for(std::vector<int>::size_type i = 0; i < set1.size(); i++) {
        set1.push_back(set2.at(i));
    }
    return set1;
}

So I want the same thing again but where string is say my_data_type which is a struct of various members. Let's say it looks like this:

struct my_data_type {
    int label;
    vector<string> x;
    vector<string> y;
    string str;
};

A function for each data type would also not be as simple as my set_union(...) function because surely I would need to test for equality on each member of my_data_type in the case of set intersection?

Also, I'm quite new to C++ so any comments on my existing function would be appreciated too.

Many thanks.

解决方案

There are already such algorithms (union, intersection, sorting, ...): http://www.cplusplus.com/reference/algorithm/

Your elements simply need to meet the requirements for STL container elements (see http://msdn.microsoft.com/fr-fr/library/bb385469.aspx):

All reference types that are inserted into STL/CLR containers must have, at a minimum, the following elements:

A public copy constructor.

A public assignment operator.

A public destructor.

Furthermore, associative containers such as set and map must have a public comparison operator defined, which is operator< by default. Some operations on containers might also require a public default constructor and a public equivalence operator to be defined.

Like reference types, value types and handles to reference types that are to be inserted into an associative container must have a comparison operator such as operator< defined. The requirements for a public copy constructor, public assignment operator, and a public destructor do not exist for value types or handles to reference types.

You can find information about operator overloading (to be implemented in your custom class) on that WikiBook: http://en.wikibooks.org/wiki/C++_Programming/Operators/Operator_Overloading

这篇关于通用集合操作类,即交集,并集,减等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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