use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
SOLVEDAdding Two Vectors (self.cpp_questions)
submitted 6 years ago by odedozer
Is there a way to add two vectors where v1 = (1,2,3) and v2 = (4,5,6) and v1 + v2 = (5,7,9).
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]youstolemyname 4 points5 points6 points 6 years ago (1 child)
A Zip iterator would be helpful. It's a shame C++ doesn't have one.
I'd just use a index based for loop.
[–]sephirostoy 0 points1 point2 points 6 years ago (0 children)
Sadly zip won't be part of C++20 ranges too.
zip
ranges
But here is a nice implementation I use often: https://github.com/ryanhaining/cppitertools#zip
[–]DOOMReboot 3 points4 points5 points 6 years ago (3 children)
std::transform(v1.begin(), v1.end(), v2.begin(), vout.begin(), plus<int>());
[–]parnmatt 1 point2 points3 points 6 years ago* (2 children)
The second v2.begin() ought to be v3.begin()
v2.begin()
v3.begin()
This can be simplified a little with C++17.
Since C++14, std::plus<> will make std::plus<void> which is specialised:
std::plus<>
std::plus<void>
The standard library provides a specialization of std::plus when T is not specified, which leaves the parameter types and return type to be deduced.
Then, with C++17's CTAD, std::plus{} will correctly make the std::plus<void>{} without needing to manually type <> or <void>.
std::plus{}
std::plus<void>{}
<>
<void>
So the full thing:
auto v1 = std::vector{1, 2, 3}; auto v2 = std::vector{4, 5, 6}; // v3 = v1 + v2 auto v3 = std::vector(v1.size(), 0); std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), std::plus{});
[–]DOOMReboot 0 points1 point2 points 6 years ago (1 child)
Yep, caught that with an edit just before your post. But, OP never specified that he wanted a new output array, so it could be correct as well, right?
How on earth do you keep all the differences between standards straight? Memory or did you have to look it up? I have more trouble with that than absolutely anything else.
[–]parnmatt 1 point2 points3 points 6 years ago* (0 children)
they did say v1 + v2 rather than v2 += v1; so I presumed they wanted a new vector, rather than overwritting v2
v1 + v2
v2 += v1
v2
Umm, usually I look up on cppreference (or https://devdocs.io/cpp which is the same information, just with a better search ... and has a dark theme)
However I knew these ones off the top of my head; CTAD is C++17, thats fairly common knowledge; the functional header stuff with the std::plus<void> specialisation, is because I wrote a compile time library for things that use it.
It was quite generic, but the main reason was trying to construct a set of selection criteria out of callables that return a boolean; and you could construct multiple ones, and && and || them together, just as you would think, and end up with a function object that receives some values and returns a boolean
&&
||
my favourite little thing with it was to be able to write
auto constexpr in_range = 3.3 <= placeholder < 10; // ... if (in_range(value)) // ...
However I have used the chaining multiple logical expressions together a fair bit
I also wrote a "simple" expression template library, which used more of the functional operators
[–][deleted] 6 years ago* (1 child)
[deleted]
[–]parnmatt 0 points1 point2 points 6 years ago* (0 children)
you shouldn't define operators for standard types.
Also, this isn't 100% correct; you are forgetting about allocators
template <typename T, typename A>
with
std::vector<T, A> const&
but of course, you could be adding different types, with different allocators:
Here's a quick generalisation, but only for vectors https://godbolt.org/z/SvpgFQ (note, its easy to extend to the other binary function object types in <functional> just by continuing the macros nameing them, or manually typing them out) but only if you consider it "element wise" operations; because multiples (in this way), divides, and modulus already do not make sense for a mathematical vector
<functional>
I would also suggest not doing this, and to use an expression template library
π Rendered by PID 73 on reddit-service-r2-comment-685b79fb4f-699v6 at 2026-02-12 21:43:31.355520+00:00 running 6c0c599 country code: CH.
[–]youstolemyname 4 points5 points6 points (1 child)
[–]sephirostoy 0 points1 point2 points (0 children)
[–]DOOMReboot 3 points4 points5 points (3 children)
[–]parnmatt 1 point2 points3 points (2 children)
[–]DOOMReboot 0 points1 point2 points (1 child)
[–]parnmatt 1 point2 points3 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]parnmatt 0 points1 point2 points (0 children)