473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
2 3596
"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sean Dettrick | last post by:
Hi, I have several <vector>'s of the same length, with entries as follows: I= A= B= I want to use STL to make (I == 0) a mask to operate on the elements of A and B, i.e. I want to do this:
1
by: Florian Liefers | last post by:
"Hello World\n", i have the following problem: One of my headerfiles for a lib is including <vector>. When i compile the lib, everything is done well. In my application another file is...
5
by: Ahmad | last post by:
Hi all, I have written a simple c++ app, as I'm still learning c++. The thing works flawlessly on VC++6, but just doesn't work on g++. The transliterate function which causes the problem is...
6
by: Some Clown | last post by:
Greetings, I'm trying to figure out how to loop through a vector of strings, searching each item as I go for either a boolean condition or a "contains" test. So if my vector is called 'v' I...
2
by: Russ Ford | last post by:
I'm trying to overload the << operator to output vectors, as follows: template <class T> ostream& operator << (ostream& o, const vector<T>& v) { o << "< "; copy (v.begin(), v.end(),...
3
by: kuiyuli | last post by:
I'm using VC++ .Net to do a simlple program. I tried to use <vector> <list> in the program, and I simply put the folowing lines " #include <list> #include <vector> #include <string> using...
0
by: cagenix | last post by:
I was running through a data structures book and I was curious if anyone could inform me of how to inherit the vector class to do a simple search and erase function. The example states: ...
1
by: Birthe Gebhardt | last post by:
Dear all, I could not find the way to handle 'not normal' list objects, for example using remove_if, find etc. Example : class Todo { public : .. int getNumber(){ return num_;}
5
by: Stefan.Wagenbrenner | last post by:
Good morning, I'm trying to write a dll using Dev Cpp 4. I'm able to compile the following code, but the linker throws an error and no dll is produced: #include <stdio.h> #include <windows.h>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.