vectors of structs

A

Alatarie

Hi all,

This is probably a stupid question but I've been out of the c++ thing for a
while...
I'm trying to make a vector of structs. Each time I create a struct:

struct RECORDS
{
int id;
other vars
....
};

I use an iterator to go through the vector to see if a struct with a
matching id exists, as this affects the other struct variables. However,
comparing the current id with *iterator.id does not work. This is what I
have:

for(it = records.begin(); it != records.end(); it++)
{
partID = *it.partID;

if(record.partID == *it.partID)
{
found = true;
}
}

Does anyone have any suggestions?
Thanks
 
A

Attila Feher

Alatarie said:
Hi all,

This is probably a stupid question but I've been out of the c++ thing
for a while...
I'm trying to make a vector of structs. Each time I create a struct:

struct RECORDS
{
int id;
other vars
....
};

I use an iterator to go through the vector to see if a struct with a
matching id exists, as this affects the other struct variables.
However, comparing the current id with *iterator.id does not work.

Would you care to share what "does not work" means over there?
 
T

tom_usenet

Hi all,

This is probably a stupid question but I've been out of the c++ thing for a
while...
I'm trying to make a vector of structs. Each time I create a struct:

struct RECORDS
{
int id;
other vars
....
};

I use an iterator to go through the vector to see if a struct with a
matching id exists, as this affects the other struct variables. However,
comparing the current id with *iterator.id does not work. This is what I
have:

for(it = records.begin(); it != records.end(); it++)
{
partID = *it.partID;

*it.partID is equivalent to *(it.partID) - don't forget the
precendence rules! Use either:
(*it).partID
or even better:
it->partID.

Tom
 
J

jeffc

Alatarie said:
Hi all,

This is probably a stupid question but I've been out of the c++ thing for a
while...
I'm trying to make a vector of structs. Each time I create a struct:

struct RECORDS
{
int id;
other vars
....
};

I use an iterator to go through the vector to see if a struct with a
matching id exists, as this affects the other struct variables. However,
comparing the current id with *iterator.id does not work.

Even if you don't like pointer notation, dereferencing notation can be even
worse. I'd recommend you use iterator->id instead, assuming that is what
you meant in the first place.
 
J

Jerry Coffin

[ ... ]
I use an iterator to go through the vector to see if a struct with a
matching id exists, as this affects the other struct variables. However,
comparing the current id with *iterator.id does not work. This is what I
have:

[ ... ]
Does anyone have any suggestions?

Your immediate problem (binding of '.' vs. '*') has already been
addressed, so I'll pass it for now.

Looking at things on a larger scale, I'd suggest that you use std::find
or std::find_if instead of code anything like you have right now. This
can be a little bit of a pain at first (e.g. you'll probably end up with
a nested call to bind1st or bind2nd) but IMO, it's well worth the
trouble in the long run.

If you're doing a search like this on a regular basis, you might want to
consider storing (an index to?) your records in a set or multiset.
Another alternative to consider would be one of the (several) containers
around that support binary searching on a sorted vector to improve
performance.
 
A

Andrew Koenig

I use an iterator to go through the vector to see if a struct with a
matching id exists, as this affects the other struct variables. However,
comparing the current id with *iterator.id does not work.

That's because . binds more tightly than *, so you have to write
(*iterator).id, or (equivalently) iterator->id .
 
S

Sergei Matusevich

*it.partID is equivalent to *(it.partID) - don't forget the
precendence rules! Use either:
(*it).partID
or even better:
it->partID.

Just my $0.02 - you may also want to use some STL algorithm to process
the records, e.g.

struct EqRecord_partID : public unary_function<RECORDS, bool>
{
const int id_;
explicit EqRecord_partID( const int id ) : id_( id ) {}
bool operator()( const RECORD& entry ) const
{
return id_ == entry.partID;
}
};

// .......

vector<RECORD>::iterator it =
find_if( records.begin(), records.end(), EqRecord_partID( partID ) );

You can also consider storing records in a map keyed by partID. In this
case you may not need to store partID in the RECORD struct at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top