Click here to Skip to main content
15,912,082 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: array => vector [modified] Pin
VeganFanatic17-Jun-10 14:27
VeganFanatic17-Jun-10 14:27 
GeneralRe: array => vector Pin
Stephen Hewitt17-Jun-10 14:57
Stephen Hewitt17-Jun-10 14:57 
GeneralRe: array => vector Pin
VeganFanatic17-Jun-10 15:10
VeganFanatic17-Jun-10 15:10 
GeneralRe: array => vector Pin
Stephen Hewitt17-Jun-10 15:45
Stephen Hewitt17-Jun-10 15:45 
GeneralRe: array => vector Pin
VeganFanatic17-Jun-10 15:46
VeganFanatic17-Jun-10 15:46 
GeneralRe: array => vector Pin
VeganFanatic17-Jun-10 15:58
VeganFanatic17-Jun-10 15:58 
AnswerRe: array => vector Pin
Aescleal17-Jun-10 22:18
Aescleal17-Jun-10 22:18 
Questioninheriting std::vector Pin
VeganFanatic17-Jun-10 8:26
VeganFanatic17-Jun-10 8:26 
a while ago I wrote a vector template to assist in a project, it works OK.

I was considering the effect inheriting the std::vector and make all of the functionality available and simply attach my new functionality on top of that.

Is this practical? Here is the vector as it stands today.


template <class base> class vector {<br />
public:<br />
	std::vector<base> data;<br />
	friend class matrix<base>;<br />
	vector operator+(const vector &that) {			// parallelogram law<br />
		vector ret;<br />
		assert(this->data.size() == that.data.size());<br />
		ret.resize(this->data.size());<br />
		#pragma omp parallel for<br />
		for (int i = 0; i < this->data.size(); i++)<br />
			ret.data[i] = this->data[i] + that.data[i];<br />
		return ret;<br />
	}<br />
	vector operator-(vector that) {				// parallelogram law<br />
		vector ret;<br />
		assert(this->data.size() == that.data.size());<br />
		ret.resize(this->data.size());<br />
		#pragma omp parallel for<br />
		for (int i = 0; i < this->data.size(); i++)<br />
			ret.data[i] = this->data[i] - that.data[i];<br />
		return ret;<br />
	}<br />
	vector operator* (base &that) {				// scalar product<br />
		vector ret;<br />
		ret.resize(this->data.size());<br />
		#pragma omp parallel for<br />
		for (int i = 0; i < this->data.size(); i++)<br />
			ret.data[i] = this->data[i] * that;<br />
		return ret;<br />
	}<br />
	double operator* (vector &that) {			// dot / inner product<br />
		double ret;<br />
		#pragma omp parallel for<br />
		for (int i = 0; i < this->data.size(); i++) <br />
			ret += this->data[i] * that.data[i];  // sum of products of complex numbers is real<br />
		return ret;<br />
	}<br />
	vector operator/ (base &that) {				// scalar division<br />
		vector ret;<br />
		ret.resize(this->data.size());<br />
		#pragma omp parallel for<br />
		for (int i = 0; i < this->data.size(); i++)<br />
			ret.data[i] = this->data[i] / that;<br />
		return ret;<br />
	}<br />
	vector operator= (vector &that) {			// assignment operator<br />
		for (int i=0; i < that.data.size(); i++)<br />
			this->data[i] = that.data[i];<br />
		return *this;<br />
	}<br />
	base& operator[] (size_t index) {			<br />
		return this->data[index];<br />
	}<br />
	bool operator== (vector &that) {			<br />
		bool temp = true;<br />
		for (int i=0; i < that.data.size(); i++)<br />
			if (this->data[i] != that.data[i]) temp = false;<br />
		return temp;<br />
	}<br />
	// the norm is the vector length<br />
	base norm(void) {						<br />
		base result = (base)0.0;<br />
		#pragma omp parallel for<br />
		for (int i = 0; i < this->data.size(); i++) {<br />
			result += this->data[i] * this->data[i];  // sum of products of complex numbers is real<br />
		}<br />
		return sqrt(result);<br />
	}<br />
	// Bring up some of the std:vector functions<br />
	size_t size(void) {<br />
		return data.size();<br />
	}<br />
	vector resize(size_t newsize) {<br />
		data.resize(newsize);<br />
		return *this;<br />
	}<br />
	vector clear(void) {<br />
		data.clear();<br />
		return *this;<br />
	}<br />
};<br />

http://www.contract-developer.tk

AnswerRe: inheriting std::vector Pin
Aescleal17-Jun-10 9:12
Aescleal17-Jun-10 9:12 
GeneralRe: inheriting std::vector Pin
VeganFanatic17-Jun-10 9:15
VeganFanatic17-Jun-10 9:15 
AnswerRe: inheriting std::vector Pin
Chris Losinger17-Jun-10 9:14
professionalChris Losinger17-Jun-10 9:14 
GeneralRe: inheriting std::vector Pin
VeganFanatic17-Jun-10 9:18
VeganFanatic17-Jun-10 9:18 
GeneralRe: inheriting std::vector Pin
Chris Losinger17-Jun-10 9:22
professionalChris Losinger17-Jun-10 9:22 
GeneralRe: inheriting std::vector Pin
VeganFanatic17-Jun-10 9:25
VeganFanatic17-Jun-10 9:25 
GeneralRe: inheriting std::vector Pin
Chris Losinger17-Jun-10 9:28
professionalChris Losinger17-Jun-10 9:28 
GeneralRe: inheriting std::vector Pin
Stephen Hewitt17-Jun-10 14:05
Stephen Hewitt17-Jun-10 14:05 
GeneralRe: inheriting std::vector Pin
Chris Losinger17-Jun-10 16:34
professionalChris Losinger17-Jun-10 16:34 
GeneralRe: inheriting std::vector Pin
Emilio Garavaglia18-Jun-10 3:12
Emilio Garavaglia18-Jun-10 3:12 
GeneralRe: inheriting std::vector Pin
Chris Losinger18-Jun-10 11:17
professionalChris Losinger18-Jun-10 11:17 
Questionscan array of byte Pin
rockago17-Jun-10 8:25
rockago17-Jun-10 8:25 
AnswerRe: scan array of byte Pin
Niklas L17-Jun-10 8:34
Niklas L17-Jun-10 8:34 
GeneralRe: scan array of byte Pin
rockago17-Jun-10 8:48
rockago17-Jun-10 8:48 
GeneralRe: scan array of byte Pin
Niklas L17-Jun-10 9:11
Niklas L17-Jun-10 9:11 
QuestionRe: scan array of byte Pin
David Crow17-Jun-10 9:11
David Crow17-Jun-10 9:11 
QuestionEnumwindows hangs (freezing) application any idea? Pin
ERLN17-Jun-10 8:07
ERLN17-Jun-10 8:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.