自己写一个数组,同样也可以写一个容器

97 篇文章 0 订阅
92 篇文章 0 订阅
#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;
}

 

更详细的资料,参考boost网站的资料:看贴图

模仿,自己写一个容器,有增删改函数、

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值