C + + templates and STL Small Example series < a > self-built array array

Source: Internet
Author: User

C + + templates and STL Small Example series < a > self-built array array, provides the following external interface
Method function Description
Array () No-parameter construction method, the number of constructed elements is the array of template parameters
Array (int length) An array of parameter construction methods, with the number of elements being a parameter of length
~array () Destructors
int size () Returns the number of elements in an array
t& get (int num) Returns a reference to the element in the array that specifies the subscript
void set (T data, int num) Sets the value of the specified subscript element
t& operator [] (int num) overloading the [] function of type T
The following code uses the private element size1, originally wanted to name the size, but because in the public method declared the int size () method, the compilation is not past, so named Size1, it feels very strange.

My_array.h

Ifndef __my_array__#define __my_array__template<typename T, int n>class array {public:array ();  Array (int length);  ~array ();  t& get (int idx);  t& operator[] (int idx);  void set (T data, int idx);  int size ();p rivate:t* pt; int size1;};/  /constructor template<typename T, int n>array<t, N>::array () {pt = new T[n]; size1 = n;}  Constructor template<typename T, int n>array<t, n>::array (int length) {pt = new T[length]; size1 = length;} destructor template<typename T, int n>array<t, N>::~array () {delete [] pt;} Gets the number of array elements template<typename T, int n>int array<t,n>::size () {return size1;}    Gets the specified subscript element template<typename T, int n>t& array<t, n>::get (int num) {if (num >= size1 | | num < 0) {  Exception} else{return Pt[num]; }}//set the value of the specified subscript element template<typename T, int n>void array<t, n>::set (t data, int num) {if (num >= size1 | | num &L T               0) {//exception                                         } else{Pt[num] = data; }}//the [] function of the overloaded element type template<typename T, int n>t& array<t, n>::operator[] (int num) {if (num >= size1 | | num  < 0) {//exception} else{return * (PT + num); }} #endif

Test procedure:

#include <iostream>#include <string>#include "my_array.h"using namespace std;int main(){  Array<int, 5> ary;  for(int i = 0; i < ary.size(); ++i){    ary.set(i * 10, i);    cout << ary.get(i) << " ";    cout << ary[i] << ", ";  }  cout << endl;  Array<string, 3> asr(4);  for(int i = 0; i < asr.size(); ++i){    asr.set("AAA", i);    cout << asr.get(i) << " ";    cout << asr[i] << ", ";  }  cout << endl;  return 0;}

C + + templates and STL Small Example series < a > self-built array array

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.