473,402 Members | 2,072 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,402 software developers and data experts.

replacing structs in a vector

I have a vector of structs. Say I get a new struct that's supposed to
replace one of the structs in the vector. I believe I could use
replace_if() to do this, but I'm not sure what the syntax would be.
Something to do with bind2nd or something, I bet.

If there's another struct in the vector who's "id" data member is the
same as the new struct, that's the one that should be replaced.
Otherwise, I just want to add the struct to the end of the vector.

(not all these structs have a valid 'id' field at all times, otherwise
I'd use a map)

Thanks,
Joe
May 5 '06 #1
8 2413
Joe Van Dyk wrote:
I have a vector of structs. Say I get a new struct that's supposed to
replace one of the structs in the vector. I believe I could use
replace_if() to do this, but I'm not sure what the syntax would be.
Something to do with bind2nd or something, I bet.
That depends on how you want to find the element to be replaced.
If there's another struct in the vector who's "id" data member is the
same as the new struct, that's the one that should be replaced.
Ah, ok. Then replace_if might work. I think you would need to write your own
predicate to compare the id fields of two objects.

Something like:
(untested)

struct id_cmp
{
bool operator()(const my_struct& a, const my_struct& b) const
{
return a.id == b.id;
}
};

my_struct new_obj;
replace_if(my_vec.begin(), my_vec.end(), bind2nd(id_cmp, new_obj), new_obj);
Otherwise, I just want to add the struct to the end of the vector.

(not all these structs have a valid 'id' field at all times, otherwise
I'd use a map)


I just wanted to suggest that ;-)

May 5 '06 #2
Rolf Magnus wrote:
Joe Van Dyk wrote:

I have a vector of structs. Say I get a new struct that's supposed to
replace one of the structs in the vector. I believe I could use
replace_if() to do this, but I'm not sure what the syntax would be.
Something to do with bind2nd or something, I bet.

That depends on how you want to find the element to be replaced.

If there's another struct in the vector who's "id" data member is the
same as the new struct, that's the one that should be replaced.

Ah, ok. Then replace_if might work. I think you would need to write your own
predicate to compare the id fields of two objects.

Something like:
(untested)

struct id_cmp
{
bool operator()(const my_struct& a, const my_struct& b) const
{
return a.id == b.id;
}
};

my_struct new_obj;
replace_if(my_vec.begin(), my_vec.end(), bind2nd(id_cmp, new_obj), new_obj);


I'm slighly confused, probably mostly because I don't understand how
bind2nd works. You're creating a new struct that contains a bool
operator that takes two my_structs and returns their equality status.

Why is the id_cmp struct needed? Can't you just have a function called
compare_my_structs or something and use that function in replace_if?

Joe
Otherwise, I just want to add the struct to the end of the vector.

(not all these structs have a valid 'id' field at all times, otherwise
I'd use a map)

I just wanted to suggest that ;-)

May 5 '06 #3
In article <Iy********@news.boeing.com>,
Joe Van Dyk <jo********@boeing.com> wrote:
I have a vector of structs. Say I get a new struct that's supposed to
replace one of the structs in the vector. I believe I could use
replace_if() to do this, but I'm not sure what the syntax would be.
Something to do with bind2nd or something, I bet.

If there's another struct in the vector who's "id" data member is the
same as the new struct, that's the one that should be replaced.
Otherwise, I just want to add the struct to the end of the vector.


How many objects are to get replaced? If only one, I would use find_if
to find it and then replace what is found rather than replace_if (which
goes through the entire container even if it replaces the first element.)

struct pred : unary_function< my_struct, bool >
{
int id;
pred( int id ): id( id ) { }
bool operator()( my_struct st ) {
return st.id == id;
}
};
Then in the code where you have:

my_struct st;
vector< my_struct > vec;
int id;

Do this:

vector< my_struct >::iterator it =
find_if( vec.begin(), vec.end(), pred( id ) );
if ( it != vec.end() )
*it = st;
May 5 '06 #4
In article <Iy********@news.boeing.com>,
Joe Van Dyk <jo********@boeing.com> wrote:
Rolf Magnus wrote:
Joe Van Dyk wrote:

I have a vector of structs. Say I get a new struct that's supposed to
replace one of the structs in the vector. I believe I could use
replace_if() to do this, but I'm not sure what the syntax would be.
Something to do with bind2nd or something, I bet.

That depends on how you want to find the element to be replaced.

If there's another struct in the vector who's "id" data member is the
same as the new struct, that's the one that should be replaced.

Ah, ok. Then replace_if might work. I think you would need to write your own
predicate to compare the id fields of two objects.

Something like:
(untested)

struct id_cmp
{
bool operator()(const my_struct& a, const my_struct& b) const
{
return a.id == b.id;
}
};

my_struct new_obj;
replace_if(my_vec.begin(), my_vec.end(), bind2nd(id_cmp, new_obj), new_obj);


I'm slighly confused, probably mostly because I don't understand how
bind2nd works. You're creating a new struct that contains a bool
operator that takes two my_structs and returns their equality status.

Why is the id_cmp struct needed? Can't you just have a function called
compare_my_structs or something and use that function in replace_if?


You could, but then the two my_struct objects would have to be exactly
identical... Which makes one wonder why it's being replaced. :-)
May 5 '06 #5

Daniel T. wrote:
In article <Iy********@news.boeing.com>,
Joe Van Dyk <jo********@boeing.com> wrote:
I have a vector of structs. Say I get a new struct that's supposed to
replace one of the structs in the vector. I believe I could use
replace_if() to do this, but I'm not sure what the syntax would be.
Something to do with bind2nd or something, I bet.

If there's another struct in the vector who's "id" data member is the
same as the new struct, that's the one that should be replaced.
Otherwise, I just want to add the struct to the end of the vector.
How many objects are to get replaced? If only one, I would use find_if
to find it and then replace what is found rather than replace_if (which
goes through the entire container even if it replaces the first element.)

struct pred : unary_function< my_struct, bool >
{
int id;
pred( int id ): id( id ) { }
bool operator()( my_struct st ) {


This could be improved by accepting "const my_struct&" instead of
passing by value.
return st.id == id;
}
};


The op might also consider if map<> would serve them better.

May 5 '06 #6
Daniel T. wrote:
In article <Iy********@news.boeing.com>,
Joe Van Dyk <jo********@boeing.com> wrote:

I have a vector of structs. Say I get a new struct that's supposed to
replace one of the structs in the vector. I believe I could use
replace_if() to do this, but I'm not sure what the syntax would be.
Something to do with bind2nd or something, I bet.

If there's another struct in the vector who's "id" data member is the
same as the new struct, that's the one that should be replaced.
Otherwise, I just want to add the struct to the end of the vector.

How many objects are to get replaced? If only one, I would use find_if
to find it and then replace what is found rather than replace_if (which
goes through the entire container even if it replaces the first element.)


Just one of them.

struct pred : unary_function< my_struct, bool >
{
int id;
pred( int id ): id( id ) { }
bool operator()( my_struct st ) {
return st.id == id;
}
};
I'm not familar with unary_function. I'll go look it up.
Then in the code where you have:

my_struct st;
vector< my_struct > vec;
int id;

Do this:

vector< my_struct >::iterator it =
find_if( vec.begin(), vec.end(), pred( id ) );
if ( it != vec.end() )
*it = st;

Hm.

Here's the code I currently have:
std::vector<my_struct>::iterator i;
for (i = vec.begin(); i != vec.end(); ++i)
{
if (i->id == st.id)
{
*i = st;
break;
}
}
if (i == vec.end())
vec.push_back(st);

Would people rather see that, or something like what Daniel wrote?

Perhaps I need to redo my design, and use a map for when my structs have
a valid id, and another container for the structs that are waiting for
their id.
May 5 '06 #7
In article <Iy********@news.boeing.com>,
Joe Van Dyk <jo********@boeing.com> wrote:
Hm.

Here's the code I currently have:
std::vector<my_struct>::iterator i;
for (i = vec.begin(); i != vec.end(); ++i)
{
if (i->id == st.id)
{
*i = st;
break;
}
}
if (i == vec.end())
vec.push_back(st);

Would people rather see that, or something like what Daniel wrote?
Now that I know what's supposed to happen if none of the objects have
that id, let me update:

struct has_id : unary_function< my_struct, bool >
{
int id;
has_id( int id_ ): id( id_ ) { }
bool operator()( my_struct st ) const {
return st.id == id;
}
};

The above is just boilerplate code... Now let's look at the actual
location where the job is done...

vector<my_struct>::iterator i =
find_if( vec.begin(), vec.end(), has_id( st.id ) );
if ( i == vec.end() )
vec.push_back( st )
else
*i = st;
Let's compare the two examples. My call explicitly says: "find_if...
has_id" so it's easy to tell what the block of code is supposed to do.
Now I admit that you have to get used to interpreting "i == vec.end()"
as "not found" but otherwise, what I wrote is rather straight forward
and requires little further interpretation from the reader.

Now notice that no where in your loop does the subject of finding
anything ever come up. In fact, we have to read two blocks in before we
even learn that the loop will terminate before reaching the end of the
container!

Also, my example has a Cyclomatic Complexity of 2 (i.e. there are only
two paths of execution.) Yours has a complexity of 4 and 'i' must be
compared to vec.end() at least twice.

Perhaps I need to redo my design, and use a map for when my structs have
a valid id, and another container for the structs that are waiting for
their id.


That would probably be a better choice, and remove the 'id' from the
struct. Instead use it as a key to the map.
May 6 '06 #8
In article <11**********************@e56g2000cwe.googlegroups .com>,
"Noah Roberts" <ro**********@gmail.com> wrote:
Daniel T. wrote:
In article <Iy********@news.boeing.com>,
Joe Van Dyk <jo********@boeing.com> wrote:
I have a vector of structs. Say I get a new struct that's supposed to
replace one of the structs in the vector. I believe I could use
replace_if() to do this, but I'm not sure what the syntax would be.
Something to do with bind2nd or something, I bet.

If there's another struct in the vector who's "id" data member is the
same as the new struct, that's the one that should be replaced.
Otherwise, I just want to add the struct to the end of the vector.


How many objects are to get replaced? If only one, I would use find_if
to find it and then replace what is found rather than replace_if (which
goes through the entire container even if it replaces the first element.)

struct pred : unary_function< my_struct, bool >
{
int id;
pred( int id ): id( id ) { }
bool operator()( my_struct st ) {


This could be improved by accepting "const my_struct&" instead of
passing by value.


Maybe, it all depends on the size of the struct. It's a little more
flexible with pass by value because one doesn't need to worry about the
reference to reference problem if the pred is wrapped in another functor
like "not1".
return st.id == id;
}
};

May 6 '06 #9

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

Similar topics

6
by: Alatarie | last post by:
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;...
2
by: William Payne | last post by:
Hello, I have two structs: struct FileEntry { FileEntry(const char* name, const char* type, std::size_t file_size, HICON icon) : m_file_size(file_size),
6
by: Simon Elliott | last post by:
I have ome new code which has to work with some legacy code which does a lot of memset's and memcmp's on structs of PODs. This leads me to want to do stuff like this: struct foo { unsigned...
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; };
5
by: ma740988 | last post by:
There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where...
1
by: sam961 | last post by:
Hi, I am a beginner in C++ so I hope you can be patient on me since my purpose is to learn. I am now involved in a relatively complicated Data Analysis exercise. I have a series of frames...
4
by: engggirl3000 | last post by:
I was told to write a function that will read data, in a user defined text file, into a vector of structs. I am not sure where to start with this. Could some one tell me what is a vector of...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.