- How to do this (small task - std::transform?)? - 7 Updates
- Use of the break; and continue; statments - 3 Updates
- No judgment, just forgiveness and guidance - 2 Updates
- How to do this (small task - std::transform?)? - 1 Update
- peformance of wait notify concept of condition variable - 1 Update
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: May 21 03:01AM +0200 On 20-May-17 7:57 PM, Bonita Montero wrote: > transform( vp.begin(), vp.end(), back_inserter( vp2 ), > []( Person &p ) { Person2 p2; p2.age = p.age; return p2; } ); > } Hm! As I understood the task: > // add 10 items into persons2 .... > Now, which std-function I can use to copy *only* the age-values from > persons vector into persons2? … it involves • Two vectors of equal size, one hold Person items and the other holding Person2 items. I.e. the latter is not empty. • The `age` values in the latter should be /changed/. It looks to me as if you're assuming instead that the `vp2` vector is initially empty. Also, an assumption that at the end it should hold only age values, with other items default-initialized. But this means that the `height` items will have indeterminate values. For example, when I added the following little driver code, #include <iostream> auto main() -> int { vector<Person> vp = { {1, "One"}, {2, "Two"}, {3, "Three" } }; vector<Person2> vp2; f( vp, vp2 ); for( auto const& p : vp2 ) { cout << p.age << " " << p.height << " `" << p.name << "`" << endl; } } which hopefully conforms to the assumptions in your code, it produced 1 4208845 `` 2 4208845 `` 3 4208845 `` Changing the default initialization to one that carries over the data is trivial, like this, #include <assert.h> void f_fixed( vector<Person> &vp, vector<Person2> &vp2 ) { assert( vp2.size() == 0 ); vp2.reserve( vp.size() ); transform( vp.begin(), vp.end(), back_inserter( vp2 ), []( Person &p ) { Person2 p2{ p.age, 0, p.name }; return p2; } ); } which gives the output 1 0 `One` 2 0 `Two` 3 0 `Three` Then it's very clear that this is really a copying of transformed values. I think that's very likely what the OP was really after. I.e., that the request to modify values was an X/Y-question. • • • As a matter of style I would add `const` for the vector of original items, and instead of using an out-argument for the result I would simply return it, like this: auto f2( vector<Person> const& vp ) -> vector<Person2> { vector<Person2> result; result.reserve( vp.size() ); transform( vp.begin(), vp.end(), back_inserter( result ), []( Person const& p ) { return Person2{ p.age, 0, p.name }; } ); return result; } E.g. this allows the driver program to use `const`, and there's no need for the `assert` any more – one degree of freedom for bugs is removed. Cheers!, - Alf |
JiiPee <no@notvalid.com>: May 21 04:55AM +0100 On 21/05/2017 02:01, Alf P. Steinbach wrote: >> vector<Person2> persons2; >> // add 10 items into persons .... >> // add 10 items into persons2 .... And then possibly set all the member variables into some value. > • Two vectors of equal size, one hold Person items and the other > holding Person2 items. I.e. the latter is not empty. > • The `age` values in the latter should be /changed/. all the age values from the first should be copied to second (in the same order). And no other values touched/modified -they should remain the same as they were. > 1 0 `One` > 2 0 `Two` > 3 0 `Three` so it copied ALL the values from the first? this is not what I wanted. |
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: May 21 07:02AM +0200 On 21-May-17 5:55 AM, JiiPee wrote: >> 2 0 `Two` >> 3 0 `Three` > so it copied ALL the values from the first? this is not what I wanted. Okay, then you CAN do this, in order to just modify the `age` values, leaving the other fields' values unchanged: #include <assert.h> #include <string> #include <vector> #include <algorithm> using namespace std; struct Person { int age; string name; }; struct Person2 { int age; int height; string name; string mobilenumber; }; void f( vector<Person> const& v, vector<Person2> &v2 ) { assert( v.size() == v2.size() ); transform( v.begin(), v.end(), // Source 1, for arg `p` below. v2.begin(), // Source 2, for arg `p2` below. v2.begin(), // Destination []( Person const& p, Person2 p2 ) { p2.age = p.age; return p2; } ); } #include <iostream> auto main() -> int { vector<Person> const vp = { {1, "One"}, {2, "Two"}, {3, "Three" } }; vector<Person2> vp2 = { {0, 10, "En"}, {0, 20, "To"}, {0, 30, "Tre"} }; f( vp, vp2 ); for( auto const& p : vp2 ) { cout << p.age << " " << p.height << " `" << p.name << "`" << endl; } } But as I wrote earlier, it's contrived. And as I forgot to mention, sorry, it's also needlessly inefficient and brittle. Better just use a straightforward `for` loop. ;-) Cheers & hth., - Alf |
Bonita Montero <Bonita.Montero@gmail.com>: May 21 11:26AM +0200 > • Two vectors of equal size, one hold Person items and the other holding > Person2 items. I.e. the latter is not empty. > • The `age` values in the latter should be /changed/. It is just an example but not real-world code. |
JiiPee <no@notvalid.com>: May 21 12:30PM +0100 Ok thanks, I ll save this. Yes I see its too "heavy"in this situation. Maybe better on other situations. But anyway good to know how to do it. |
JiiPee <no@notvalid.com>: May 21 01:17PM +0100 On 21/05/2017 06:02, Alf P. Steinbach wrote: > []( Person const& p, Person2 p2 ) { p2.age = p.age; return p2; } But does this make first a copy of the old p2 and then modify that new object and then assign it to the old object? |
JiiPee <no@notvalid.com>: May 21 09:34PM +0100 Sure, but the point was here to find a std function for this. On 21/05/2017 14:07, Stefan Ram wrote: |
Paavo Helde <myfirstname@osa.pri.ee>: May 21 05:14PM +0300 On 20.05.2017 13:38, Stefan Ram wrote: > was carefully crafted by a master to handle all errors and > release all resources correctly might look to a beginner as > if the author did not care about theses topics at all. It just does not look like so, but RAII and exceptions allow one to write most of the code genuinely not caring about how errors are handled. |
woodbrian77@gmail.com: May 21 12:12PM -0700 On 20.05.2017 13:38, Stefan Ram wrote: > was carefully crafted by a master to handle all errors and > release all resources correctly might look to a beginner as > if the author did not care about theses topics at all. First Leigh so eloquently reminded us to "Read the Bible" and now Stefan seems to be referring to 1st Corinthians 3:10: "According to G-d's grace that was given to me, I have laid a foundation as a skilled master builder, and another builds on it. But each one must be careful how he builds on it." I guess we can expect more boppers -- biblically oriented programmers. Brian Ebenezer Enterprises - In G-d we trust. http://webEbenezer.net |
Christian Gollwitzer <auriocus@gmx.de>: May 21 09:27PM +0200 > I guess we can expect more boppers -- biblically oriented programmers. backward oriented programmers? Christian |
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: May 21 11:39AM -0700 A personal invitation. RSVP and secure your future in Heaven: https://www.youtube.com/watch?v=ETc-nG7X5 Go to evening church services tonight. If it's already too late, go visit an online church. Temple Baptist in Knoxville, TN will be broadcasting their live service in a few hours. And you can find past services on the YouTube Chris A channel. Search for "Pastor Charles Lawson". Jesus loves you. He wants you to be with Him in Heaven. Ask Him to forgive you today and be forgiven today. Secure your future and begin your new life. Thank you, Rick C. Hodgin |
Christian Gollwitzer <auriocus@gmx.de>: May 21 09:25PM +0200 Am 20.05.17 um 01:16 schrieb Rick C. Hodgin: > If I were posting my artwork, or how to lose fat and gain muscle tone, > then yoy may be correct. But I am posting about Jesus, and teaching > you about forgiveness of sin and eternal life. If I were posting about Jesus, or how to lose fat and gain muscle tone, then you may be correct. But I am posting about art, and teaching you about the beauty of the world. > These things are of such paramount importance to all people that it > would be a sin to not post such teachings. These things are of such paramount importance to all people that it would be a sin to not post such teachings. See the difference? Christian |
ram@zedat.fu-berlin.de (Stefan Ram): May 21 01:07PM >> []( Person const& p, Person2 p2 ) { p2.age = p.age; return p2; } >But does this make first a copy of the old p2 and then modify that new >object and then assign it to the old object? You can define you own algorithm to get a less contrived call. The following algorithm supplies an additional iterator called »reference« for a target which is to be modified: template < typename InputIterator, typename Function, typename Iterator > Function for_each ( InputIterator first, InputIterator last, Iterator reference, Function f ) { for (; first != last; ++first)f( *first, reference++ ); return ::std::move( f ); } . I am not used to write algorithms and so I'd gladly accept any criticism of the definition. (For example, the names could be improved.) This algorithm then can be called as in: for_each ( ::std::cbegin( vector0 ), ::std::cend( vector0 ), ::std::begin( vector1 ), /* target */ []( person0 const & source, auto target_iterator ) { target_iterator->age = source.age; } ); . |
"Chris M. Thomasson" <invalid@invalid.invalid>: May 20 06:31PM -0700 On 5/1/2017 8:29 AM, Bonita Montero wrote: >> You also forget to destroy that event. > I dindn't forget to destroy anything because this is only > a simple test-app. Dangerous line of thinking. Anyway, I am done. |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment