By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,426 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,426 IT Pros & Developers. It's quick & easy.

<Vector> Find function why is the return value wrong?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Sims" <si*********@hotmail.com> wrote in message
news:bv************@ID-162430.news.uni-berlin.de...
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;
Try to avoid using global variables if there is a reasonable alternative.

//
// ...
//

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


What you are trying to do is easy enough:

struct sIntStructure
{
int m_nNumber;
sIntStructure(int i_nNumber = 0) : m_nNumber(i_nNumber) {}
};

bool operator == (const sIntStructure &lhs, const sIntStructure &rhs)
{
return lhs.m_nNumber == rhs.m_nNumber;
}

// note: pass the vector as an argument, not as a global variable
int FindNumber(const std::vector<sIntStructure>&v, int nNumber)
{
typedef std::vector<sIntStructure> t_vec;
t_vec::const_iterator i = std::find(v.begin(), v.end(),
sIntStructure(nNumber));
if (i == v.end())
return -1; // error return: didn't find it
return int(i - v.begin()); // normal return
}

void try_it()
{
typedef std::vector<sIntStructure> t_vec;
t_vec m_stv;
m_stv.push_back(sIntStructure(45));
m_stv.push_back(sIntStructure(17));
m_stv.push_back(sIntStructure(67));
std::cout << FindNumber(m_stv, 17) << '\n'; // output: 1
}

Of course std::find works in linear time which isn't very fast. If you are
planning to do a lot of searches you should probably use std::map instead:

typedef std::map<int, sIntStructure> t_map;
t_map a_map;
a_map[45] = sIntStructure(45);
a_map[17] = sIntStructure(17);
a_map[67] = sIntStructure(67);
sIntStructure &si = a_map[17];

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #2

P: n/a

Am i doing something wrong?
Sometimes the line 'ret' value is incorrect, (and not equal to -1), but

why?

lower_bound returns the first element that is >= to what you are looking
for.

john
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.