ICollection interface in C,

Source: Internet
Author: User

ICollection interface in C,

I. Collection class:

 

1.1 ICollection Interface

Previously we learned arrays, which are the most basic set types defined by. net Framework. In addition to arrays,. net Framework also defines many set types to meet programming needs.

Common collections include:

  • Vector;
  • Linked List;
  • Dual-end queue;
  • Stack;
  • Tree;
  • Hash table;
  • Hash ing.

All these Collection types must implement an important interface in. net Framework:ICollection Interface. The ICollection interface defines two main specifications for implementing its class:

  • Number of set elements (expressedCount attribute);
  • The iterator used to access the set.GetEnumerator Method).

The GetEnumerator method is the parent interface of ICollection.IEnumerable InterfaceInherited.

The ICollection interface defines a collection for storing and retrieving object-type object references. Therefore, you can store and obtain reference or value-type objects of various reference types.

 

1.2IEnumerable Interface

  The IEnumerable interface is the parent interface of ICollection.Iteratable.

The IEnumerable interface only defines one method:GetEnumerator, This method will return a"Iterations"Object (or calledIteratorObject) is an implementationIEnumerator Interface.

Figure 1 ICollection hierarchy

 

Ii. iterations

  An iterator is also called an iterator in the C ++ design mode.Iteration ModeThe meaning of its existence is that each set has different storage forms. For example, vectors are stored using arrays, and linked lists are stored using references between objects, A table uses multi-dimensional arrays or cross references to store data. The tree establishes relationships between objects through multiple references between objects. graphs use trees or tables to store their relationships ...... It can be seen that different collection classes store data in a variety of ways. The diversity of storage will inevitably lead to access diversity. The most common access to a set is "traversal", that is, access from the set header to the end of the set. We need to find a way to useUnified ApproachComeTraverse different forms of access to a set. This is the basic reason for the generation of iteration methods.

The iteration mode regards the collection class asUnlimited containersAnd the container storage rules areLinearThat is, every element in the container must beHas a direct forward direction (except the first element) and a direct successor (except the last element).The container has a flag that can point to any storage unit in the container..

There are two types of iterations:Unidirectional only Forward Iteration and bidirectional Iteration.

 

2.1 unidirectional only Forward Iteration Mode

  For unidirectional only Forward Iteration mode, the unit of the SetThe flag can only start from an element of the set.One by oneMove backwardTherefore, the most basic iteration mode defines three operations:

  • Item of the access flag;
  • Mark to the next item and determine whether it has reached the end of the Set;
  • Return to the initial item.

Figure 2 unidirectional only Forward Iteration

 

2.2 bidirectional iteration Mode

  In bidirectional iteration mode, the Unit signs of a set move one by one from one element of the set, or one by one from one element of the set, therefore, this type of iterator defines four operations:

  • The current item of the access flag;
  • The flag advances to the next item and determines whether the flag has reached the end of the set;
  • The flag returns to the previous item and determines whether the flag has reached the start of the set;
  • Return to the initial item.

Figure 3 bidirectional Iteration

. Net Framework definesOne-way only Forward IterationSpecification.

The ICollection interface inherits the IEnumerable interface. The GetEnumerator method defined by this interface can obtain an iterative sub-object of the current collection class object, that is, an object that implements the IEnumerator interface.

All classes that implement the IEnumerable interface can be iterated using foreach.

  The IEnumerator interface defines an iterator that can be referenced by objects of the object type. The IEnumerable interface defines the method to obtain such iterative sub-objects.

 

Iii. Generic set

 

3.1 ICollection <T> Interface

ICollection <T> InterfaceDefinedGeneric setInterface, the so-called generic set, that is, using the generic features provided by. net Framework, so that the set is limitedOnly one type of object reference or value type is stored. Generic collections enhance the efficiency and security of set access.

Because the storage type has been determined, ICollection <T> provides more methods than the ICollection interface, including adding to the collection, deleting from the collection, and determining an object reference (or object value) whether it exists in the set.

 

3.2 IEnumerable <T> Interface

ICollection <T> interface implements the IEnumerable <T> interface, mainly used to return an implementationImplemented the IEnumerator <T> interface to determine the type of iterative sub-objects.

See the inheritance relationship diagram of ICollection <T>.

Figure 4 ICollection <T> interface inheritance Diagram

 

Familiar with the above series of interfaces, which is the basis for learning various set class objects.


Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

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.