Ogre::list lowest closest value
-
araccun
- Kobold
- Posts: 33
- Joined: Mon Mar 10, 2014 6:37 pm
Ogre::list lowest closest value
I need to find lowest closest double value of input from my Ogre::list, i couldnt find documentation of ogre::list i found implementation of things i need on std::list but cannot convert it for ogre list. Any ideas? I wanted to use relevance of std::lower_bound.
- Klaim
- Old One
- Posts: 2565
- Joined: Sun Sep 11, 2005 1:04 am
- Location: Paris, France
- x 56
- Contact:
Re: Ogre::list lowest closest value
Ogre::list<T>::type is a std::list<T> by default. See https://bitbucket.org/sinbad/ogre/src/d ... quisites.h
-
araccun
- Kobold
- Posts: 33
- Joined: Mon Mar 10, 2014 6:37 pm
Re: Ogre::list lowest closest value
So i got method
but when i try to pass pointer to Ogre::List i got error
thats my list declaration:
Code: Select all
double closest(std::list<double>* vec, double value) {
auto const it = std::lower_bound(vec->begin(), vec->end(), value);
if (it == vec->end()) { return -1; }
return *it;
}static cast dosent help eithererror C2664: 'FrameStartedListener::closest' : cannot convert parameter 1 from 'std::list<_Ty,_Alloc> *' to 'std::list<_Ty> *'
thats my list declaration:
Code: Select all
list<double>::type* preBeat;- Klaim
- Old One
- Posts: 2565
- Joined: Sun Sep 11, 2005 1:04 am
- Location: Paris, France
- x 56
- Contact:
Re: Ogre::list lowest closest value
Look at the declaration in the header I pointed: by default the list provided by Ogre uses a custom allocator, which basically mean that it's not exactly a std::list<T> but a std::list<T,A> (note that the C++ committee consider this very annoying and is working at solutions to allow any std::list<T> or std::list<T,A> to be considered the same type in generic code).
That's what the compiler is telling you in the error (in a non-obvious way, unfortunately).
So either you change your function signature to take the right type (Ogre::list<double>::type instead of std::list<double>),
or you make it template:
With C++11 and without pointers I personally would have written:
(code not tested)
Then if I use this function on set or map, I would specialize it so that it calls std::set<T>::lower_bound member instead of the global algorithm.
That's what the compiler is telling you in the error (in a non-obvious way, unfortunately).
So either you change your function signature to take the right type (Ogre::list<double>::type instead of std::list<double>),
or you make it template:
Code: Select all
template<RealType, RealTypeContainer>
RealType closest(RealTypeContainer* vec, RealType value) { // why a pointer? vec cannot be null!
auto const it = std::lower_bound(vec->begin(), vec->end(), value);
if (it == vec->end()) { return -1; }
return *it;
}
Code: Select all
template<RealType, RealTypeContainer>
RealType closest(const RealTypeContainer& container, RealType value) { // const ref: read-only!
using namespace std; // automatic usage of std:: by default if container is a standard container
const auto container_end = end(container);
auto const it = std::lower_bound(begin(container), vec_end, value); // begin() and end() works with arrays and custom types too
if (it == container_end)
{
return -1;
}
return *it;
}
Then if I use this function on set or map, I would specialize it so that it calls std::set<T>::lower_bound member instead of the global algorithm.