vector operations in c++ [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Open source C++ library for vector mathematics
I've to do a very simple question: how can I do basic operations like sum, difference or product on two diffent int vector, like in matlab, using c++? does exist any function that can do it?
Thanks in advance.

Not in the standard library, you will have to use a third party library, I don't know what your requirements are, but you could take a look at something like boost::ublas.

Take a look at the standard algorithms: http://en.cppreference.com/w/cpp/algorithm

Depending on your requirements (which you didn't really elaborate on), you might be looking for anything from std::for_each to Boost::uBLAS...

You can write you own class, like class Vector, class Matrix, where you overload the operators like +, -, *. Or you can use libraries like LAPACK, boost ublas, ...

Use std::accumulate to accumulate a single value e.g. the total sum or total product.
Use std::inner_product to generate a value which is the result of a binary operator between values in 2 vectors, and a binary operation between successive results. This is a very useful function, if you can formulate your problem correctly. This is related to MapReduce.
Although, probably what you really want is std::transform which can operate on two inputs and write into a third output.

Related

In C++ how do you store 2 different data types in 1 data structure?

I most often work with python and therefore I'm used to being able to put a bool and integer in a single list. I realize that C++ has a different paradigm, however, I imagine that there is a workaround to this issue. Ideally I want a vector that could contain data that looks like {1, 7, true, 8, false, true, 9}. So this vector would have to be defined with syntax like (vector int bool intBoolsVec), however, I realize that isn't proper syntax.
I see that some people suggest using variant that was introduced in C++17, is this the best solution? Seems like this would be a common problem if C++ doesn't easily allow you to work with heterogenous containers, even if those containers are constrained to a couple defined types like a vector that only takes only ints and bools.
What is the easiest way to create a vector that contains both integers and booleans in C++? If someone could also provide me more insight on why C++ doesn't have an easy/obvious way to do this, that might help me better understand C++ as well.
My approach would probably be to create my own class, which does exactly what I want it to do. This might be your easiest solution, besides using std::any. Also, you could combine those by creating a custom array of std::any which only allows integers and booleans at certain entries for your example. This would be similar, but not equal to an array of std::variant. Also, In C++ you can store 2 types in a std::pair, if that fits your use-case.

Multidimensional Array Implementation [duplicate]

This question already has an answer here:
how to traverse a boost::multi_array
(1 answer)
Closed 8 years ago.
After searching for a while I did not find an answer to my question, so apologies in advance if this was already answered somewhere else.
I'm looking for a multi-dimensional data-structure in C++ that allows access not only as N-dimensional array, but also as 1-dimensional.
For an example assume a simple 2-dimensional matrix (it could go to higher dimensions, but in that case let's stick to this example). In most cases the member will be accessed in row-colum-form, e.g. matrix[x][y]. In other cases it might be desireable to access all members as a single list, e.g. for matrix addition using std-algorithms.
Standard-Approach would probably be something like std::array<std::array<double, 4>, 4> and write additionally an iterator with linear access to all members and maybe an extra accessor function.
A second approach is the other way around std::array<double, 16> with accessors in row-colum-form, but in this case it gets tricky to return whole columns .
Or maybe it is doable with boost MultiArray, but I think reducing the dimensions of a MultiArray always results in only getting slices of the MultiArray.
My question boils down to: Is there already an implementation in the standard-library or some well-known library, like boost, for this? If not, am I missing a point and there is a simpler method than the ones I wrote about?
EDIT: I was not looking for only iteration over all values, like in the mentioned question. But however from the pointed documentation I could find that MultiArray can be accessed as C-style array which is enough for my needs. This can then be closed and thanks for all answers
See boost::multi_array::data() and boost::multi_array::num_elements().
As with std::vector, it would appear you can just access it as a solid block of memory by index, if that's all you want. I've never done this, but looks like you can. Just because you can doesn't necessarily mean you should, but, well...
See this answer:
how to traverse a boost::multi_array
There is something like what you are looking for: std::valarray<T>. Well, the intend of the std::valarray<T> class template is to provide different views on the same array and to support potentially vectorized evaluation. That said, it doesn't really work and probably few people are using it.
However, from what you described you probably want to have something providing an array view on an existing array. I'd be pretty sure that this was implemented before, if nothing else as a replacement for std::valarray<T> but I can't point to an implementation.

Find an element in a vector array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find an item in a std::vector?
I am using C++ Builder to create a VCL Forms application. I also have a vector array of appointment objects that each have a name, type, reminder date/time, a date/time, location and comments.
I am wanting to implement a find feature that will let a user find an appointment given certain criteria.
The user can choose to find an appointment in the vector array by either choosing the name, type etc or a combination of each.
What would be the best programming concept to use for this situation? The vector is not large. No more than 10 or 20 elements.
Thanks
Use std::find_if() and define the required predicate (if C++11 you can use lambda function).
See online demo http://ideone.com/Md7sp.
std::find_if(A.begin(),A.end(),isthatit(conditions));
where isthatit is a predicate object constructed from conditions.
If you have many criterias you should think about
Boost.MultiIndex container which targets different search indexes.
http://www.boost.org/doc/libs/1_51_0/libs/multi_index/doc/tutorial/index.html

Initializing a AS 3.0 vector with type array - Vector.<Array>? (plus C++ equivelant)

I have a quick question for you all. I'm trying to convert over some ActionScript code to C++ and am having a difficult time with this one line:
private var edges:Vector.<Array>
What is this exactly? Is this essentially a multidimensional vector then? Or is this simply declaring the vector as a container? I understand from researching that vectors, like C++ vectors, have to be declared with a type. However, in C++ I can't just put down Array, I have to use another vector (probably) so it looks like:
vector<vector<T> example;
or possibly even
vector<int[]> example;
I don't expect you guys to know the C++ equivalent because I'm primarily posting this with AS tags, but if you could confirm my understand of the AS half, that would be great. I did some googling but didn't find any cases where someone used Array as it's type.
From Mike Chambers (adobe evangelist) :
"Essentially, the Vector class is a typed Array, and in addition to ensuring your collection is type safe, can also provide (sometimes significant) performance improvements over using an Array."
http://www.mikechambers.com/blog/2008/08/19/using-vectors-in-actionscript-3-and-flash-player-10/
Essentially the vector in C++ is based on the same principles. As far as porting a vector of Arrays in AS3 to C++, well that's not a conversion that is clear cut in principle, as you could have a collection (array) of various types in C++, such as a char array. However, it appears you've got the idea, as you've pretty much posted examples of both avenues in your question.
I would post some code but I think you've got it exactly. Weather you use a vector within a vector or you declare a specifically typed collection I think comes down to a matter of what works best for you specific project.
Also you might be interested in:
http://www.mikechambers.com/blog/2008/09/24/actioscript-3-vector-array-performance-comparison/

What is the usefulness of project1st<Arg1, Arg2> in the STL?

I was browsing the SGI STL documentation and ran into project1st<Arg1, Arg2>. I understand its definition, but I am having a hard time imagining a practical usage.
Have you ever used project1st or can you imagine a scenario?
A variant of project1st (taking a std::pair, and returning .first) is quite useful. You can use it in combination with std::transform to copy the keys from a std::map<K,V> to a std::vector<K>. Similarly, a variant of project2nd can be used to copy the values from a map to a vector<V>.
As it happens, none of the standard algorithms really benefits from project1st. The closest is partial_sum(project1st), which would set all output elements to the first input element. It mainly exists because the STL is heavily founded in mathematical set theory, and there operations like project1st are basic building blocks.
My guess is that if you were using the strategy pattern and had a situation where you needed to pass an identity object, this would be a good choice. For example, there might be a case where an algorithm takes several such objects, and perhaps it is possible that you want one of them to do nothing under some situation.
Parallel programming. Imagine a situation where two processes come up with two valid but different results for a given computation, and you need to force them to be the same. project1st/2nd provides a very convenient way to perform this operation on a whole container, using an appropriate parallel call that takes a functor as an argument.
I assume that someone had a practical use for it, or it wouldn't have been written, but I'm drawing a blank on what it might have been. Presumably its use-case is similar to the identity function that the description mentions, where there's no real need for processing but the syntax requires a functor anyway.
The example on that same page suggests using it with the two-container form of std::transform, but if I'm not mistaken, the way they're using it is functionally identical to std::copy, so I don't see the point.
It looks like a solution in search of a problem to me.