## mingw-users

 [Mingw-users] Addition of 2 Vectors (a + b) From: - 2005-02-12 17:22:38 ```Hi! Can 2 vectors of a given size be added (in c++)? How? Thanks and regards, Harp -- DSL Komplett von GMX +++ Supergünstig und stressfrei einsteigen! AKTION "Kein Einrichtungspreis" nutzen: http://www.gmx.net/de/go/dsl ```
 Re: [Mingw-users] Addition of 2 Vectors (a + b) From: - 2005-02-12 17:34:17 ```>Can 2 vectors of a given size be added (in c++)? How? > >Thanks and regards, >Harp > > > I think you need to be a little more specific on what you want to do. This is how I would add tow vectors: struct vector { float x,y,z; } vector add(vector v1,vector v2) { vector result; result.x=v1.x+v2.x; result.y=v1.y+v2.y; result.z=v1.z+v2.z; return result; } Of course, much more elegant is to overload the + operator. But I think, I did not really give you what you are looking for ... As I said, be little bit more specific what you want to do. Greetings, Nathan ```
 Re: [Mingw-users] Addition of 2 Vectors (a + b) From: Greg Chicares - 2005-02-12 18:45:13 ```On 2005-02-12 12:34 PM, Nathan Hüsken wrote: > >> Can 2 vectors of a given size be added (in c++)? How? >> > I think you need to be a little more specific on what you want to do. > This is how I would add tow vectors: > > struct vector > { > float x,y,z; > } > > vector add(vector v1,vector v2) > { > vector result; > result.x=v1.x+v2.x; > result.y=v1.y+v2.y; > result.z=v1.z+v2.z; > return result; > } > > Of course, much more elegant is to overload the + operator. Yes: if you write your own 'tuple' class, then just write the operations you need. Or, with std::vector instances that are known to have the same size, this works: std::transform (v0.begin() ,v0.end() ,v1.begin() ,v2.begin() ,std::plus() ); but probably won't be as fast as your unrolled loop. Then there's std::valarray, which may be faster and more expressive, though its design is generally considered to be somewhat deficient. Alternatives to std::valarray such as Blitz++ and PETE may be preferable. ```
 Re: [Mingw-users] Addition of 2 Vectors (a + b) From: - 2005-02-13 12:16:37 ```> On 2005-02-12 12:34 PM, Nathan HÃ¼sken wrote: > > > >> Can 2 vectors of a given size be added (in c++)? How? > >> > > I think you need to be a little more specific on what you want to do. > > This is how I would add tow vectors: > > > > struct vector > > { > > float x,y,z; > > } > > > > vector add(vector v1,vector v2) > > { > > vector result; > > result.x=v1.x+v2.x; > > result.y=v1.y+v2.y; > > result.z=v1.z+v2.z; > > return result; > > } > > > > Of course, much more elegant is to overload the + operator. > > Yes: if you write your own 'tuple' class, then just > write the operations you need. > > Or, with std::vector instances that are known to have > the same size, this works: > > std::transform > (v0.begin() > ,v0.end() > ,v1.begin() > ,v2.begin() > ,std::plus() > ); > > but probably won't be as fast as your unrolled loop. > > Then there's std::valarray, which may be faster and > more expressive, though its design is generally > considered to be somewhat deficient. Alternatives > to std::valarray such as Blitz++ and PETE may be > preferable. > Hi! Thanks to you all for your nice suggestions. I thought of something like this: // vectors: with overloading operators #include class Vector{ public: Vector(){}; Vector(int x, int y); int x, y; Vector operator +(Vector v); }; Vector::Vector (int a, int b) { x = a; y = b; } //global operators Vector Vector :: operator+(Vector v){ Vector temp; temp.x = x + v.x; temp.y = y + v.y; return (temp); } int main () { Vector a (8,9); Vector b (6,4); Vector c; c = a + b; cout << c.x << "," << c.y; return 0; } Is it okay with that of overloading operators? Thanks and regards, Harp -- DSL Komplett von GMX +++ Supergünstig und stressfrei einsteigen! AKTION "Kein Einrichtungspreis" nutzen: http://www.gmx.net/de/go/dsl ```
 Re: [Mingw-users] Addition of 2 Vectors (a + b) From: Greg Chicares - 2005-02-13 16:17:06 ```On 2005-02-13 07:16 AM, hmetu@... wrote: > > Thanks to you all for your nice suggestions. I thought of something like > this: > > class Vector{ > public: > Vector(){}; The result of using that constructor is that x and y are uninitialized. You could write Vector(): x(0), y(0) {}; to prevent that. > Vector(int x, int y); You later implement this as > Vector::Vector (int a, int b) { > x = a; > y = b; > } Consider adding the 'inline' keyword to avoid inefficiency. Or just write it inside the class definition, using an initializer-list: Vector(int a, int b): x(a), y(b) {} That doesn't matter much when the datatype is int. But someday you might want a Vector of some class type: Vector(myclass const& a, myclass const& b) {x = a; y = b;} would default-construct x and y, and then assign values to them, but Vector(myclass const& a, myclass const& b): x(a), y(b) {} would copy-construct x and y more efficiently in one step. > int x, y; > Vector operator +(Vector v); > }; > //global operators > Vector Vector :: operator+(Vector v){ > Vector temp; > temp.x = x + v.x; > temp.y = y + v.y; > return (temp); > } Prefer making operator+=() a member and implementing operator+() as a nonmember that calls operator+=(). Generally, try to make such operators behave as they do for builtin types. It's tricky, so try to learn from others' experiences. If you can get Scott Meyers's "Effective C++" books, they'll help you a lot. For code like this, g++'s '-Weffc++' flag is useful. Use all these flags -W -Wall -Weffc++ -std=c++98 -pedantic and consider all the advice they give. Searching the web with terms like operator overload arithmetic member will find really bad advice like http://www.cplusplus.com/doc/tutorial/tut4-2.html and more thoughtful advice like http://www.cs.pdx.edu/~karlaf/CS202_Fall04/OpOvGuide.html http://www.cs.duke.edu/csed/tapestry/howtoe.pdf It may be better to search in newsgroups like alt.comp.lang.learn.c-c++ comp.lang.c++.moderated where, if someone gives poor advice, someone else will correct it. Consider using this excellent library http://boost.org/libs/utility/operators.htm#arithmetic which will give you (for example) operator+() automatically if you write operator+=(). ```
 Re: [Mingw-users] Addition of 2 Vectors (a + b) From: - 2005-02-15 12:13:48 ```Hi Greg! Million thanks!!! Regards, Harp > On 2005-02-13 07:16 AM, hmetu@... wrote: > > > > Thanks to you all for your nice suggestions. I thought of something like > > this: > > > > class Vector{ > > public: > > Vector(){}; > > The result of using that constructor is that x and y > are uninitialized. You could write > > Vector(): x(0), y(0) {}; > > to prevent that. > > > Vector(int x, int y); > > You later implement this as > > > Vector::Vector (int a, int b) { > > x = a; > > y = b; > > } > > Consider adding the 'inline' keyword to avoid inefficiency. > Or just write it inside the class definition, using an > initializer-list: > > Vector(int a, int b): x(a), y(b) {} > > That doesn't matter much when the datatype is int. But someday > you might want a Vector of some class type: > > Vector(myclass const& a, myclass const& b) {x = a; y = b;} > > would default-construct x and y, and then assign values to them, but > > Vector(myclass const& a, myclass const& b): x(a), y(b) {} > > would copy-construct x and y more efficiently in one step. > > > int x, y; > > Vector operator +(Vector v); > > }; > > > //global operators > > Vector Vector :: operator+(Vector v){ > > Vector temp; > > temp.x = x + v.x; > > temp.y = y + v.y; > > return (temp); > > } > > Prefer making operator+=() a member and implementing operator+() > as a nonmember that calls operator+=(). Generally, try to make > such operators behave as they do for builtin types. It's tricky, > so try to learn from others' experiences. > > If you can get Scott Meyers's "Effective C++" books, they'll > help you a lot. For code like this, g++'s '-Weffc++' flag is > useful. Use all these flags > -W -Wall -Weffc++ -std=c++98 -pedantic > and consider all the advice they give. > > Searching the web with terms like > operator overload arithmetic member > will find really bad advice like > http://www.cplusplus.com/doc/tutorial/tut4-2.html > and more thoughtful advice like > http://www.cs.pdx.edu/~karlaf/CS202_Fall04/OpOvGuide.html > http://www.cs.duke.edu/csed/tapestry/howtoe.pdf > It may be better to search in newsgroups like > alt.comp.lang.learn.c-c++ > comp.lang.c++.moderated > where, if someone gives poor advice, someone else will correct it. > > Consider using this excellent library > http://boost.org/libs/utility/operators.htm#arithmetic > which will give you (for example) operator+() automatically if you > write operator+=(). > > > > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real users. > Discover which products truly live up to the hype. Start reading now. > http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click > _______________________________________________ > MinGW-users mailing list > MinGW-users@... > > You may change your MinGW Account Options or unsubscribe at: > https://lists.sourceforge.net/lists/listinfo/mingw-users > -- Lassen Sie Ihren Gedanken freien Lauf... z.B. per FreeSMS GMX bietet bis zu 100 FreeSMS/Monat: http://www.gmx.net/de/go/mail ```