473,395 Members | 1,412 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.

vectors of structs

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
Jul 19 '05 #1
6 8303
Alatarie wrote:
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?

--
Attila aka WW
Jul 19 '05 #2
On Tue, 30 Sep 2003 20:21:07 +1000, "Alatarie"
<Al******@Narmolanya.com> wrote:
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
Jul 19 '05 #3

"Alatarie" <Al******@Narmolanya.com> wrote in message
news:3f***********************@lon-reader.news.telstra.net...
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.
Jul 19 '05 #4
In article <3f***********************@lon-reader.news.telstra.net>,
Al******@Narmolanya.com says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #5
> 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 .

--
Andrew Koenig, ar*@acm.org
Jul 19 '05 #6
On 2003-09-30, tom_usenet <to********@hotmail.com> wrote:
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.


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.

--
Sergei Matusevich,
Brainbench MVP for C++
http://www.brainbench.com

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
5
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
10
by: mahurshi | last post by:
I've got a gate structure that looks like this /* Defining sGATE structure */ struct sGATE { string name; vector<int> input; int output; };
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.