c/c++ vector 的简单实现

说其简单,是因为没有实现allocate 类, 而采用简单的malloc 分配内存。

iterator 也采用直接的value_type 指针, 但保留了vector 的基本接口和方法。对理解vector 工作原理有帮助。

代码及测试代码如下:

//#include "stdafx.h"
#include <stdio.h>
template < class _T >
class vector{
	enum{ SPACE_CAPACITY = 4 };//初始容量
public:
	// 构造器与析构器
	//用explicit避免隐式类型转换
	explicit vector( int size = 0 ):
		m_size( size ),m_capacity( size + SPACE_CAPACITY ){
			_p = new _T[ m_capacity ];
		}
	//copy constructor
	vector( const vector & rhs ):_p( NULL ){ operator=( rhs ); }
	~vector(){ delete [] _p; }
 
	
	//运算符
	const vector & operator= ( const vector & rhs ){
		if( this != &rhs ){//如果是本身的话就不用再操作了
			delete [] _p;
			m_size = rhs.size();
			m_capacity = rhs.capacity();
 
			_p = new _T[ capacity() ];
			for( int k = 0; k < m_size; k++ )//拷贝元素
				_p[k] = rhs._p[k];
		}
		return *this;
	}
	_T & operator[] ( int index ) { return _p[ index ]; }
	const _T & operator[] ( int index ) const { return _p[ index ]; }
	
	// 指针, iterator 操作
	typedef _T * iterator;//用原生指针替代迭代器
	typedef const _T * const_iterator;
	iterator begin() { return &_p[0]; }
	const_iterator begin() const { return &_p[0]; }
	iterator end()	 { return &_p[ m_size ]; }
	const_iterator end()   const { return &_p[ m_size ]; }
 
	
	//插入元素, 弹出元素
	//每次空间不够的时候,就重新获得2倍于当前容量的空间
	void push_back( const _T & x ){
		if( m_size == m_capacity )
			ReAllocate( 2*m_capacity + 1 );
		_p[ m_size++ ] = x;
	}
	void pop_back() { m_size--; }
	
	// 属性接口
	int size() const { return m_size; }
	int capacity() const { return m_capacity; }
	bool empty() const { return size() == 0; }
	const _T & back() const { return _p[ m_size-1 ]; }
private:
	int m_size; //元素的个数
	int m_capacity; //容量
	_T *_p; //指向new分配的资源
 
	// 内存分配(分配器简化处理)
	void resize( int newSize ){
		if( newSize > m_capacity )
			ReAllocate( newSize ); 
		m_size = newSize;
	}
	//获取新的空间,拷贝,释放旧的数据
	void ReAllocate( int newCapacity ){
		if( newCapacity < m_size ) return;
 
		_T *oldArray = _p;
		_p = new _T[ newCapacity ];
		for( int k = 0; k < m_size; k++ )
			_p[k] = oldArray[k];
		m_capacity = newCapacity; //m_size 没有改变
		delete [] oldArray;
	}
};
 
 
 
int main()
{
	vector<int> v;
	v.push_back(4);
	v.push_back(5);
	vector<int>::iterator it;
	for(it=v.begin(); it!=v.end(); it++)
	{
		printf("data is %d\n",*it);
	}
//	_getch();
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RANSAC(RANdom SAmple Consensus)是一种经典的模型参数估计算法,常用于计算机视觉、机器学习等领域。下面是使用C/C++实现RANSAC算法的代码示例。 ```C++ #include <iostream> #include <vector> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; // 随机取样函数 vector<int> random_sample(int n, int k) { vector<int> result; for (int i = 0; i < k; ++i) { int j = rand() % n; while (find(result.begin(), result.end(), j) != result.end()) { j = rand() % n; } result.push_back(j); } return result; } // 计算模型参数函数 vector<double> compute_model(vector<pair<double,double>> data) { vector<double> result; double x_mean = 0, y_mean = 0; for (auto p : data) { x_mean += p.first; y_mean += p.second; } x_mean /= data.size(); y_mean /= data.size(); double s_xy = 0, s_xx = 0; for (auto p : data) { s_xy += (p.first - x_mean) * (p.second - y_mean); s_xx += (p.first - x_mean) * (p.first - x_mean); } double slope = s_xy / s_xx; double intercept = y_mean - slope * x_mean; result.push_back(slope); result.push_back(intercept); return result; } // 计算误差函数 double compute_error(vector<pair<double,double>> data, vector<double> model) { double error = 0; for (auto p : data) { double y = model[0] * p.first + model[1]; error += pow(y - p.second, 2); } return sqrt(error / data.size()); } // RANSAC算法主要过程 vector<double> ransac(vector<pair<double,double>> data, int k, double threshold, int max_iterations) { vector<double> best_model; int best_score = 0; for (int i = 0; i < max_iterations; ++i) { vector<int> indices = random_sample(data.size(), k); vector<pair<double,double>> sample_data; for (auto index : indices) { sample_data.push_back(data[index]); } vector<double> model = compute_model(sample_data); double error = compute_error(data, model); int score = 0; for (auto p : data) { double y = model[0] * p.first + model[1]; if (abs(y - p.second) < threshold) { score++; } } if (score > best_score) { best_score = score; best_model = model; } } return best_model; } int main() { srand(time(0)); // 生成测试数据 vector<pair<double,double>> data; for (int i = 0; i < 20; ++i) { double x = i + rand() / double(RAND_MAX); double y = 2 * x + rand() / double(RAND_MAX) - 0.5; data.push_back(make_pair(x, y)); } // 调用RANSAC算法 vector<double> model = ransac(data, 2, 0.1, 100); // 输出结果 cout << "Best Model: y = " << model[0] << "x + " << model[1] << endl; return 0; } ``` 以上代码实现了一个简单的RANSAC算法,用于拟合一组带有噪声的点,得到最佳的直线模型。其中,函数`random_sample()`用于随机取样,函数`compute_model()`用于计算模型参数,函数`compute_error()`用于计算误差,函数`ransac()`为RANSAC算法主要过程。在主函数中,我们生成了一组带有噪声的点作为测试数据,并调用RANSAC算法进行拟合,输出结果为最佳的直线模型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值