I'm trying to write a function or set of functions that will find the minimum value of a multi dimensional std::vector with any number of dimensions.
I'd like to have the function templated so that it works with any type or class that can use the < operator.
You have functions U min(std::vector<T>&) and T min(std::vector<T>&). How do you expect the compiler to tell which one to use?
To be honest, I don't know how to do this. I can't think of a good way to tell whether T is a number or a vector (bad ways include restricting numeric (comparable) types to the built in ones or even using typeid.name() and searching for "std::vector" in it). Though even then C++ should have problems understanding what U was.
@helios
What is there not to get? If vector<int>, he wants to find the smallest int in it. If vector<vector<...<int>...> he wants to map min on each element and build a vector<int> form the results (and search it, of course).
That's kind of an interesting problem. I have a kind of solution but I am not sure if its exactly what you are looking for. Also it's not heavily tested but it seems to work:
#include <iostream>
#include <vector>
int min (int i){ return i; }
template <typename T>
int min (std::vector<T>& v) {
int res = min(v[0]);
for (int i = 1; i < v.size(); i++)
if (min(v[i]) < res) res = min(v[i]);
return res;
}
int main(){
std::vector< std::vector< int > > v;
int a[] = {10, 20, 30};
v.push_back( std::vector<int>( a, a+3 ) );
int b[] = {6, 5, 7, 2};
v.push_back( std::vector<int>( b, b+4 ) );
int c[] = {5, 50, 10};
v.push_back( std::vector<int>( c, c+3 ) );
std::cout << min( v );
std::cin.get();
}
If something so simple for int can't be done for T, you could say that there is a problem.
edit:
@Galik, I did not expect that to work. Why would C++ match the second function before the first? Does it have a rule to start with the least general ones?
Also, your use of max and push_backs is slightly silly.
Okay I think I've cracked it, or at least come close.
I was resigned to using the method that I already knew and that hamsterman suggested and to simply write a specialization for every built in type, but then had a closer look at Galik's code and had a read around. I think my initial try was a partial specialisation (or maybe not a specialisation at all), because both were functions of a std::vector<T>. A bit of reading (there's not a lot of resources about this on the web) indicated partial specialisations can only be performed on class templates not function templates. Galik's code is a full specialisation because the type used in the second function (vector<T>) is more specialised that the first (T).
#include<vector>
template<class T>
inline T vmin(const T &v)
{
return v;
}
template<class U, class T>
U vmin(const std::vector<T>& v)
{
U result=vmin<U>(v[0]);
for(size_t i=1; i<v.size(); ++i)
{
U newmin=vmin<U>(v[i]);
if(newmin < result) result=newmin;
}
return result;
}
int main()
{
std::vector< std::vector <int> > myvector;
//some code to allocat vector
//get the smallest int in the vector
int smallint=vmin<int>(myvector);
//this method also allows me to get arrays
//get a vector containing the smallest ints in each subvector
std::vector<int> somesmallints=min< std::vector<int> >vmin(myvector);
}
Another method which I found worked was to pass a dummy parameter to the vmin functions above use this to define the output type
//just a code snippet
template<class T, class U>
U vmin(const T &v, const U &dummy)
{
return v;
}
template<class T, class U>
U vmin(const std::vector<T>& v, const U &dummy)
{
//definition the same as above
}
int main()
{
std::vector< std::vector <int> > myvector;
//some code to allocat vector
//get the smallest int in the vector
int dummy;
int smallint=vmin(myvector,dummy);
}
I don't think I can see any better ways than this, but am always open to suggestions. Thanks Hamsterman and Galik for your extremely useful input.
And a special mention to this comment
I think it's totally retarded, though; starting from using nested vectors.
I always think it's nice that they let children on these forums but sometimes I think their parents should teach them some manners first.
I think you just need to choose your words a bit more carefully and be less confrontational with these things Helios. If someone used those words with me in person then I'd think them rude and I apply the same standards online too. Perhaps next time you could ask the questioner why they want to do what they are doing and/or suggest how they can do it better. If you are constructive like that then you will soon build a reputation.
As for the specifics here, as far as I'm concerned std::vector is just another word for array in C++ so why not nest them. As far as why try to create this function, I'd have thought that it was clear why I'd like one function call that does the job of a significant number of lines of code (especially when it gets to 4d+). If however you have a preferred method of dealling with multi-d arrays I'd be glad to hear it. I certainly don't know everything so your way might be better than mine.
If someone used those words with me in person then I'd think them rude
Well, that's a problem with your sensibilities. I was merely expressing my opinion about the direction the code was headed. I'm not responsible if you take it as a personal attack.
std::vector is just another word for array in C++ so why not nest them
Because it's inefficient in both time and space, and because even simple things like getting the minimum element in an n-dimensional matrix grow in complexity with n.
I wrote a simplistic 10-cubic matrix class as a benchmark of memory access patterns (http://www.cplusplus.com/forum/general/50371/ [arithmetic_10cubic_matrix]), which shows how this kind of things should be done and which can be easily extended to n dimensions and non-cubic matrices.
In a class structured like this, getting the minimum is trivial. You simply iterate over a linear array.
There is nothing wrong with my sensibilities, I expect people to be polite - there is nothing wrong with that
Thanks for the link to the post - an interesting quick read. The obvious problem with generating my own type of vector is that I have to rewrite all the usefull code that already exists in the std::vector. The intention is not to make my vector supremely fast but to make it easier to use. I think often it's the case that people spend more time optimising their code for speed than they will ever save waiting for it to run - but again this is like I said sometimes it's an interesting problem so people like yourself and others want to spend time on it. However just because my aims are different to yours does not make them retarded - nor does it make your aims any less deserving of your time.
You are of course correct that many of these things become easier when you generate a multi-d array in contiguous memory and things can probably be performed faster. There are of course disadvantages - it becomes much harder to resize subdimensions when using contiguos blocks because even if you are appending to the end of a dimension you have to move almost every item in your array. In this sense a std::vector would be a lot faster. Of course there are other multi-d array implimentations that already use contiguous memoty and do a lot of this stuff. Boost probably has one and so does ALGLIB and GSL. ALGLIB includes BLAS (and the other too might also) for added computational speed when copying arrays and performing calculations but because so many people including myself use std::vector for their arrays I wanted to generate some code to make the maths I do with these vectors easier and is compatible with stuff that's already written. I don't have the time or the energy to reinvent the wheel and I don't want users of my code to have to convert all their data into my special array type.
Anyway thanks for the discussion. I've learnt some stuff and I hope you have too
There are of course disadvantages - it becomes much harder to resize subdimensions when using contiguos blocks because even if you are appending to the end of a dimension you have to move almost every item in your array.
And you think this is not the case with a vector? A vector will only work better if the dimension you're extending has space left in its capacity. Otherwise it will end up making lots and lots of copy constructor calls. Even vectors of pointers will be nicer in is this case.