Write an array by yourself. You can also write a container

Source: Internet
Author: User
#include<iostream>#include<string>#include<cstddef>#include<functional>#include<algorithm>using namespace std;template<typename T,std::size_t thesize>class carray{private:T v[thesize];public:typedef T value_type;typedef T* iterator;typedef const T* const_iterator;typedef T& reference;typedef const T& const_reference;typedef std::size_t size_type;typedef std::ptrdiff_t difference_type; iterator begin(){return &v[0];} const_iterator begin() const{return &v[0]; }iterator end(){return v+thesize;}const_iterator end() const{return v+thesize;}reference operator[](std::size_t i){return v[i];}const_reference operator[](std::size_t i) const{return v[i];}size_type size() const{return thesize;}size_type max_size(){return thesize;}T* as_array(){return v;}};int main(){carray<int,10>a;for(unsigned i=0;i<a.size();++i){a[i]=i+1;}for(unsigned i=0;i<a.size();++i){cout<<a[i]<<" ";}cout<<endl;reverse(a.begin(),a.end());for(unsigned j=0;j<a.size();++j){cout<<a[j]<<" ";}cout<<endl;transform(a.begin(),a.end(),a.begin(),negate<int>());for(int i=0;i<a.size();++i){cout<<a[i]<<" ";}cout<<endl;system("pause");return 0;}

 

For more details, refer to boost website materials: View textures

Imitate, write a container by yourself, add, delete, and modify functions,

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.