3

Earlier I asked this question which was on how to make my own set operations class e.g. intersection, union etc.

The answer I chose as my solution recommended the algorithm library which have these operations already implemented. I want to get these operations working on my data types like this:

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

so it was suggested that I include these things in my struct (or class):

  • A public copy constructor.
  • A public assignment operator.
  • A public destructor.

I'm relatively new to C/C++ so please could someone provide me with these three things for the example struct that I defined here? Then also how to use one of the operations on my class (let's say set_intersection(...)?

Thank you.

Community
  • 1
  • 1
ale
  • 11,636
  • 27
  • 92
  • 149

1 Answers1

2

The compiler provides a suitable implementation of all three in this case - there is no need to write anything extra, and doing so would be bad style, IMHO. However, what you probably do need is a constructor that takes parameters, to construct a properly initialised object, and an implementation of operator<() so that your structs can be compared.

Without knowing what your struct does, it's hard to provide these, but assuming your set members will have unique labels, something like this is what you need:

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

    my_data_type( int l, const string & s ) : label( l ), str( s ) {}

    bool operator<( const my_data_type & t ) const {
          return label < t.label;
    }
};
  • There's no need for `operator<` to be a member function. – Fred Foo Jun 09 '11 at 14:21
  • Thanks Neil. Just a few (probably basic) questions on this then. What is this constructor actually doing here? I don't fully understand what a so called 'copy constructor' actually is. Also, how would I call `set_intersection(...)` in the algorithm library to return the intersection of a `vector` of `my_data_type`s? Thanks for all the help. – ale Jun 09 '11 at 14:22
  • 1
    @vivid A copy constructor is one that is used to make copies of an object. Which C++ book are you using that doesn't cover this? StackOverflow is not going to teach you the very basics of C++ programming. –  Jun 09 '11 at 14:24
  • that was me being lazy.. I will read up on the copy constructor online :). An example of the `set_intersection` using a vector of `my_data_types` would be awesome though (i.e. `vector`) – ale Jun 09 '11 at 14:26
  • @vivid I strongly advise you NOT to attempt to learn C++ from online resources, most of which are very poor indeed. Buy a book - there is a list here http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. –  Jun 09 '11 at 14:29
  • @Neil really.. I've been looking a lot at http://www.cplusplus.com/ - any good? Will mark your solution as solved but struggling with how to use set_intersection with my data types. Any ideas would be great. Thanks for the tips. – ale Jun 09 '11 at 14:35
  • @vivid-colours: Set intersection requires two or more sets. What does intersection of a `vector` mean? Is it supposed to be `result.x = intersect(v[0].x, v[1].x, ..., v.end()->x)`? – Ben Voigt Jun 09 '11 at 15:48