In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use C++ template to customize the array, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Foreword:
Make generic templates and create custom arrays
An array that contains several attributes, array capacity, the number of array elements, and the memory address of the array itself, all of which define private types and provide parametric constructs that allow users to construct the array object. Here is the code with parametric constructors and copy constructors and destructors and operator= overloads
There is a problem with the idea of creating member functions in the previous class template. The best way is to write the class template in a hpp file instead of breaking it into multiple files.
1. Custom array .hpp-file # pragma once#includeusing namespace std;#includetemplateclass Myarry {public: Myarry () {}; / / if you create a parametric construct yourself, the compiler will not provide a parameterless construct, so you must write a parameterless construct yourself, even if it is an empty implementation! Myarry (int capacity) / / Parametric constructor {this- > capacity = capacity; this- > size = 0; this- > marry = new T [this-> capacity]; / / create the array in the heap area} ~ Myarry () / / destructor {if (this- > marry! = NULL) {delete [] this- > marry;// destructor array must be added [], otherwise the breakpoint marry = NULL; this- > capacity = 0 will be raised This- > size = 0;}} Myarry (const Myarry& arr) / / copy construction {this- > capacity = arr.capacity; this- > size = arr.size; this- > marry = new T [arr.capacity]; for (int I = 0; I
< arr.size; i++)//把数据拷贝过来 { this->Marry [I] = arr- > marry [I];}} / / Equal sign assignment Myarry& operator= (const Myarry& arr) {if (this- > marry! = NULL) / / if the data is cleared first, then assign {delete [] this- > marry; this- > marry = NULL; this- > size = 0; this- > capacity = 0;} this- > capacity = arr.capacity; this- > size = arr.size This- > marry = new T [this-> capacity]; for (int I = 0; I
< this->Copy data {this- > marry [I] = arr.marry [I];} return * this;} void pushback (const T&ptr) / / tail addition {if (this- > capacity = = this- > size) {cout size] = ptr; this- > size++;} void deleteback () / / tail deletion {if (this- > size = = 0) {cout size-1] This- > size--;} T & operator [] (int index) / / access the array through the subscript and add it as a left value & {if (index > this- > capacity) {cout capacity;} int getsize () / / to get the number of array elements {return this- > size;} private: t * marry;// array int capacity;// array capacity int size;// array elements}; 2. Test file # include "Custom Array HPP" class person {public: person () {this- > age = 0;} int age; string name;}; void text01 () {person p [4]; p [0] .age = 20; p [0] .name = "Zhang San"; p [1] .age = 0; p [1] .name = "Li Si"; p [2] .age = 40; p [2] .name = "Wang Wu" P [3] .age = 80; p [3] .name = "Zhao Liu"; Myarrypp (10); for (int I = 0; I < 4; iTunes +) {pp.pushback (p [I]);} for (int I = 0; I < pp.getsize (); iTunes +) {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.