The implementation method of C + + simple Collection class _c language

Source: Internet
Author: User
Tags arrays set set

A topic from the C + + programming program. Implements a collection class that requires the following 4 actions to be implemented.
1. Add elements to the collection, if the elements already exist in the collection are not added
2. Remove elements from the collection, before removing the need to determine whether the elements in the collection exist
3. Overload + operator, to achieve the set of the set operation
4. Overloaded * operator, to achieve the intersection of sets of operations

1. The overall design of the class
the problem needs to simulate the implementation collection class, we can use arrays to simulate the collection, and then use int items[100] to hold the data in the collection. To achieve the traversal of an array, this requires an integer to represent the number of elements in the array, and the number of elements in the array is represented by int numbers, and the following four functions are designed to achieve the requirements of the topic:
1. Add elements to an array using the Add_item (int item) member function
2). To remove an element from an array by using the Remove_item (int item) member function
3. Overload operator+ Representation set operation
4. Overloaded operator* representation set of the intersection operation
Because you must ensure that the element does not exist in the collection before you add an element to the collection, you must ensure that the element exists in the collection before you remove the element from the collection, so you add the is_exist (int item) method to determine if the element exists in the collection, and to display the collection, add display Method, the basic design is as follows:

 Class Set
{public
:
  int items[100];//define an array as a container for storing 100 sets
  of element int number;//define number I to represent the numbers of elements in the collection
  / Constructors and Destructors
  Set () {
    this->number = 0;
    memset (this->items,0,sizeof (items));
  }
  Initialization method
  int init (int items[], int num);
  add Element
  bool Add_item (int item);
  Delete element
  bool Remove_item (int item);
  The Set
  operator+ (set Set2) is obtained by the set-set;
  To find the intersection set
  operator* (set Set2) of the set;
  Displays the collection element
  int display ();
  Determines whether the item exists in the collection, returns the position of the element in the collection, and does not have a return-1
  int is_exist (int item);
 

2. Constructor function

 Set () {
  this->number = 0;
  memset (this->items,0,sizeof (items));
} 

In the constructor, we initialize the arrays, after declaring the array, if we do not initialize, the array elements are random values, in C language, the variables are not initialized will be assigned random values. To avoid this, we use the Memset function to assign all elements of an array of items to 0 and, since there are no elements in the arrays, that is, the number of elements is 0, we should assign a value of 0.

3. Determine if the array contains element item

 int set::is_exist (int item)
{for
  (int i=0; i< this->number; i++) {
    if (this->items[i] = = Item) { return
      i;
    }
  }
  return-1;
} 

This function is used to determine whether an item element exists in an array, and if so, returns the position of the item element, or 1 if it does not exist. The method of judging is very simple, write a For loop from items[0]-items[number-1] one to traverse. If equal, return directly I, at this time I is the position of the item element in the array; If you traverse the full array, you do not find an element that is equal to the item, stating that there is no item in the array, and then return-1.

4. Add elements to the array

 BOOL Set::add_item (int item)
{
  if (is_exist (item) >= 0 | | this->number >=) {return
    false;< c15/>}
  This->items[this->number] = Item;
  this->number++;
  return true;
} 

To first determine if the element exists in the array, if it exists, you can no longer add elements to the collection, return directly to false, or, if not, add the element to the location pointed to by number in the array, and then count as indicator +1 of the array element, which completes the add element.

5. Protect array elements from being modified
written here, we found that the array element number indicator This->number, for the problem of several algorithms have played a central role, first of all, we rely on array elements to traverse the array element indicator, if the number value is modified, can not traverse the array. For example, when we call the following statement:

 Set Set1;
Set1.add_item (1);
Set1.add_item (2);
Set1.add_item (3); 

The array items in the collection Set1 become [1,2,3], the array element number indicator number=3, and if we want to add element 20 to the collection Set1, we need to use the number=3 indicator so that set1.items[number]=20 And let Number+1 point to the next position, that is, number=4. However, if the user manually modifies the number value, such as set1.number=50, then our number will no longer indicate the correct position of the array element, causing the number to be invalidated by all of the above algorithms, so we need the array itself, and an array element indicator number is privatized to avoid arbitrary tampering by the user. So:

 Class set
{public
:
  //constructor and destructor
  Set () {
    this->number = 0;
    memset (this->items,0,sizeof (items));
  }
  Initialization method
  int init (int items[], int num);
  add Element
  bool Add_item (int item);
  Delete element
  int remove_item (int item);
  The Set
  operator+ (set Set2) is obtained by the set-set;
  To find the intersection set
  operator* (set Set2) of the set;
  Displays the collection element
  int display ();
  Determines whether an item exists in the collection, returns the position of the element in the collection, and does not have a return-1
  int is_exist (int item);
Private:
  int items[100];//define an array as a container for storing 100 set elements
  int number;//define number I to represent the numbers of elements in the collection
}; 

6. Removing elements from the collection

 BOOL Set::remove_item (int item)
{
  int pos = is_exist (item);
  if (pos = = 1) return false;
  for (int i=pos; i< this->number-1; i++) {
    This->items[i] = this->items[i+1];
  }
  this->number--;
  return true;
} 

First, check that the element to be removed is present in the Union, and if it does not, return false directly, and then navigate to the position of the element in the collection, and then start at this position to move the remaining elements in the collection forward, one at the last set element indicator-1, and return true.

7. Find the intersection of two sets

 Set set::operator* (set Set2)
{
  set result;
  for (int i=0; i< this->number; i++) {
    if (set2.is_exist (this->items[i)) >= 0) {
      result.items[ Result.number] = this->items[i];
      result.number++
    }
  }
  return result;
} 

The algorithm is simple, traversing the elements in set a, determining if there is a in set B for each element in a, and if it exists, adding it to the set C, and finally returning the set C

8. Find the set of two sets

 Set set::operator+ (set Set2)
{
  set result;
  for (int i=0; i<this->number; i++) {
    Result.items[result.number] = this->items[i];
    result.number++;
  }
  for (int j=0; j<set2.number; J + +) {
    if (result.is_exist (set2.items[j)) = = 1) {
      Result.items[result.number] = SET2.ITEMS[J];
      result.number++
    }
  }
  return result;
} 

First, iterate through set a, add all the elements in set A to the set C, and then traverse set B, and for each element in B, first determine if it exists in a, and if not, add it to the set C, and finally return the set C

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.