Hi,
I have a structure as follow
struct sIntStructure
{
int m_nNumber;
//
// A few more variables
//
}
And a sort class
class less_number : public std::binary_function< sIntStructure,
sIntStructure, bool>
{
public:
less_number(){};
bool operator()( sIntStructure const &lhs, sIntStructure const &Rhys ) const
{
return lhs.m_nNumber< rhs.m_nNumber;
}
};
And the vector itself.
typedef std::vector< sIntStructure , std::allocator<sIntStructure > >
sIntStructure_VECTOR;
sIntStructure_VECTOR m_stv;
//
// ...
//
Now if i am looking for a number i do something like
int FindNumber( int nNumber )
{
// Sort it first
std::sort( m_stv.begin(), m_stv.end(), less_number() );
// and then find it
sIntStructure st;
st.m_nNumber = nNumber;
std::vector< sIntStructure >::iterator i = std::lower_bound(
m_stv.begin(), m_stv.end(), st, less_number() );
if( i == m_stv.end() )
return-1;
if( nNumber != i->m_nNumber ) //
<<<<<<<<<<<<<<<< Needed?
return-1;
int ret = std::distance( m_stv.begin(), i );
return ret;
}
Am i doing something wrong?
Sometimes the line 'ret' value is incorrect, (and not equal to -1), but why?
I suspect that the Find function actually returns the nearest match, is that
true?
Is my code optimised enouth?
Regards,
Sims
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.576 / Virus Database: 365 - Release Date: 30/01/2004