⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 data.cpp

📁 一个简单灵活的数据挖掘实验平台
💻 CPP
字号:
/************************************************************************/
/*		AUTHOR:   Leiming Hong                                          */
/*		INST.:    South China Univ. of Tech.							*/
/*      Date:     2005-10-26                                            */
/*      FUNC:     Vector represents an instance                         */      
/************************************************************************/
#include "Data.h"
#include "Exception.h"

#include <memory>
#include <string>
using namespace std;

//: head file for testing
#include <iostream>
#include <ctime>

//========================================================
// construct a data vector with specified size
//========================================================

Data::Data(int size)
   :_size(size),
	_values(new double[size]),
	_weight(1.0)
{
}
Data::Data(const double* values,int size)
   :_size(size),
	_values(new double[size]),
	_weight(1.0)
{
	memcpy(_values,values,sizeof(double)*size);	
}
Data::Data(vector<double> values)
	:_size(values.size()),
	 _values(new double[values.size()]),
	 _weight(1.0)
{
	copy(values.begin(),values.end(),_values);
}
Data::Data(const Data& data)
{
	_size = data._size;
	_weight = data._weight;
	_values = new double[data._size];
	memcpy(_values,data._values,sizeof(double)*data._size);
}
Data::~Data()
{
	if(_values != 0)
	delete []_values;
}
int Data::size() const
{
	return _size;
}
double Data::get_weight() const
{
	return _weight;
}
void Data::set_weight(double weight)
{
	_weight = weight;
}
//: to accelerate without bounds checking
double& Data::operator [](int index)
{
	if(index<0 || index>=_size)
		throw new Exception("Data::operator[], index out of bound");
	return _values[index];
}
string Data::to_string()
{
	string str;
	char buf[100];
	for(int i=0;i<_size;i++)
	{
		_gcvt(_values[i],10,buf);
		str.append(buf);
		str.append(" ");
	}
	return str;
}
/************************************************************************
// code for testing
void main()
{
	time_t start,end;
	start = clock();
	try
	{
		Data data(10);

		for(int i=0;i<data.size();i++)
			data[i] = i;
		cout<<"size: "<<data.size()<<endl;
		cout<<"weight: "<<data.get_weight()<<endl;
		data.to_string();
		
		cout<<"\nCopy construct test:"<<endl;
		Data data2 = data;
		cout<<"size: "<<data2.size()<<endl;
		cout<<"weight: "<<data2.get_weight()<<endl;
		data2.to_string();
		
		cout<<"\nvector constructor test\n";
		vector<double> values;
		values.push_back(10);
		values.push_back(34.3);
		Data data3(values);
		cout<<"size: "<<data3.size()<<endl;
		cout<<"weight: "<<data3.get_weight()<<endl;
		data3.to_string();

		cout<<"\ndouble array constructor\n";
		double array[2] = {10,34.3};
		Data data4(array,2);
		cout<<"size: "<<data4.size()<<endl;
		cout<<"weight: "<<data4.get_weight()<<endl;
		data4.to_string();

	}
	catch(Exception* e)
	{
		cout<<e->message()<<endl;
		delete e;
 	}
	end = clock();
	cout<<"\nTime eclipsed[s]: "<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
}
/************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -