473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

defining + and = operator for vector<double>

Hi,
I was wondering how to define the + and = operator for a vector of
double or float and do I need to define it explicitly?
does it not get defined automatically(like copy constructor) if one
does not define it explicitly?

thanks,
--A.

Aug 15 '05 #1
4 2457
Amit wrote:
I was wondering how to define the + and = operator for a vector of
double or float and do I need to define it explicitly?
does it not get defined automatically(like copy constructor) if one
does not define it explicitly?


The copy assignment operator does get defined automatically. However,
you still probably want to define one yourself. Read about "The Rule
of Three".

The operator+ does not get defined automatically.

As to how to define operator+, it's up to you. If you want to be able
to add a vector to another vector, then most likely you need

vector<double> operator+(vector<double> const& v1,
vector<double> const& v2)
{
if (v1.size() != v2.size())
throw "bad size";
// otherwise do what you need here: create a temporary
// vector, add values from v1 and v2 in it and then return it
vector<double> temp(v1);
// add v2 elements to each of 'temp'
return temp;
}

This should probably be a stand-alone function.

V
Aug 15 '05 #2
Thanks,
Can I define it in my own different namespace though(assuming I am
using namepspace std already)?

--A.

Aug 15 '05 #3
Amit wrote:
Thanks,
Can I define it in my own different namespace though(assuming I am
using namepspace std already)?


Most likely not.

V
Aug 15 '05 #4

Victor Bazarov wrote:
Amit wrote:
I was wondering how to define the + and = operator for a vector of
double or float and do I need to define it explicitly?
does it not get defined automatically(like copy constructor) if one
does not define it explicitly?
The copy assignment operator does get defined automatically. However,
you still probably want to define one yourself. Read about "The Rule
of Three".

The operator+ does not get defined automatically.

As to how to define operator+, it's up to you. If you want to be able
to add a vector to another vector, then most likely you need

vector<double> operator+(vector<double> const& v1,
vector<double> const& v2)
{
if (v1.size() != v2.size())
throw "bad size";
// otherwise do what you need here: create a temporary
// vector, add values from v1 and v2 in it and then return it
vector<double> temp(v1);
// add v2 elements to each of 'temp'
return temp;
}

This should probably be a stand-alone function.

V


Amit wrote: Thanks,
Can I define it in my own different namespace though(assuming I am
using namepspace std already)?

--A.


yes, however, you'd need to use:

using namespace your_namespace;

or:

using your_namespace::operator+;

if you wanted to use the operator outside of your namespace. Or, you
could type, for example:

std::vector<double> result = your_namespace::operator+(v1, v2);

however, that kind of defeats the purpose of defining the operator+.
Note that you should probably define operator+= too.

Aug 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dennis | last post by:
Hi I'm trying to implement a vector of vectors where find can be used to find a vector<double> in the vectors of vectors, that is hard to understand i guess. What I mean is that I got a vector...
2
by: Pepijn Kenter | last post by:
Dear experts. I have a vector<float> and want to convert that to a vector<double>. I optimistically tried: #include <vector> #include <iostream> using namespace std; int main() {
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
10
by: bluekite2000 | last post by:
and why doesnt the standard vector have such conversion available?
14
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
7
by: utab | last post by:
Dear all, I tried sth like this, compiles but segmentation fault error. In my reasoning field_values holds a vector<double> but when I tried, I understood that it is not the case :-). ...
9
by: richard_lavoie | last post by:
Hi, I have something like this: vector<floatvec1; and I want to cast it, so I use vector vec2<double= static_cast< vector<double(vec1); I always become a error: syntax error before `>'...
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.