The Big Programming Thread - Page 780
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
Manit0u
Poland17071 Posts
| ||
JWD[9]
364 Posts
On October 15 2016 05:23 Manit0u wrote: Not sure about C++ but can't you just free the entire vector at once and not bother with freeing every single element in it beforehand? That sounds like the question I wanted to ask. What makes sure, that when an object of this containertype goes out of scope, that all the destructors for the elements it manages get called? And do I have to call them explicitly, especially for nontrivial types? And if not, why was alloc.destroy then used in the reserve function? edit: http://stackoverflow.com/questions/18077898/destroy-vs-deallocate I am even unsure if this answers my question. Because most of the code in the book is wrong in a funny way, so you have to think about it. But allocaters don't really get introduced: “Messing with allocators” is pretty advanced stuff, and tricky. Leave it alone until you are ready to become an expert. And then the books ask questions about them, and to then have the outlined solutions be wrong, seems cruel for a book aimed at beginners. ¯\_(ツ)_/¯ | ||
Nesserev
Belgium2760 Posts
| ||
JWD[9]
364 Posts
It seems like this decision for what to do with vector object only is in the hands of the user if you give pointers as elements, the standard vector should actually call the destructors? I really just wanna take your word for it Nesserev, and it seems like a plausible way of defining how a vector should behave. Does it come to down to that? Choice of the implementer? | ||
Nesserev
Belgium2760 Posts
| ||
JWD[9]
364 Posts
On October 15 2016 06:05 Nesserev wrote: Actually, no, you're right. The same for loop should be present in the destructor, but you say that it's not? It is not. Stroustrup is evil ... or I should acquire the most recent edition of the book. Nesserev, Manit0u, thanks for being there. | ||
Nesserev
Belgium2760 Posts
| ||
Manit0u
Poland17071 Posts
Brilliant! | ||
Blisse
Canada3710 Posts
Also follow-up, what happens if you introduce same value denominations? Like your set of coins is {1a, 1b, 5, 10}. Also does the solution vary for permutations of coins? Ugh my head. | ||
Acrofales
Spain17261 Posts
On October 15 2016 15:22 Manit0u wrote: Brilliant! Yes, but it wouldn't work on me. Heh, guess I'm not a moron. Or perhaps a really obstinate moron | ||
Acrofales
Spain17261 Posts
On October 15 2016 16:38 Blisse wrote: Guys I feel really stupid but how does the coin change problem work? I've been thinking about it the last 3 hours and I think my brain is fried... I don't understand how you get to breaking it into solutions not containing coin X and solutions containing coin X. Also follow-up, what happens if you introduce same value denominations? Like your set of coins is {1a, 1b, 5, 10}. Also does the solution vary for permutations of coins? Ugh my head. Too lazy to google. What is the coin change problem? Is it simply the question of what change to give to a customer to minimize number of coins? If so, I don't get the problem you're having. Or are you working on some variation where you want all permutations? | ||
Khalum
Austria831 Posts
On October 15 2016 05:10 JWD[9] wrote: [..]
[..] Are the brackets for the initialization list actually written like this in the book? | ||
Manit0u
Poland17071 Posts
I really need to utilize bitwise operations more. | ||
Mr. Wiggles
Canada5894 Posts
On October 16 2016 01:50 Manit0u wrote: Heh, every day you learn something new. Today I've learned a cool way of checking if a number is a power of 2:
I really need to utilize bitwise operations more. If you're interested in bit-level manipulations and micro-optimisations, check out Hacker's Delight. http://www.hackersdelight.org | ||
Acrofales
Spain17261 Posts
On October 16 2016 01:50 Manit0u wrote: Heh, every day you learn something new. Today I've learned a cool way of checking if a number is a power of 2:
I really need to utilize bitwise operations more. Heh. Hadn't thought of that, but it's pretty nifty! | ||
Manit0u
Poland17071 Posts
On October 16 2016 02:27 Mr. Wiggles wrote: If you're interested in bit-level manipulations and micro-optimisations, check out Hacker's Delight. http://www.hackersdelight.org Thanks. I'll be sure to check it out seeing how most of my career for the past 2 years has been code optimization and refactoring. I also need to re-learn some maths since I suck at it now (that's what you get when you decide to pursue a programming career after nothing but humanities in education). | ||
Blisse
Canada3710 Posts
On October 15 2016 17:32 Acrofales wrote: Too lazy to google. What is the coin change problem? Is it simply the question of what change to give to a customer to minimize number of coins? If so, I don't get the problem you're having. Or are you working on some variation where you want all permutations? It's the one where given a set of coins and a target value, given an infinite amount of each coin how many ways can you use the coins to reach the target value. It's the dynamic programming problem. I don't understand the logic of it. | ||
JWD[9]
364 Posts
On October 15 2016 18:08 Khalum wrote: Are the brackets for the initialization list actually written like this in the book? Yes On October 15 2016 07:44 Nesserev wrote: Kk, so I got my paws on Stroustrup's book, and read the section. I guess I didn't look at your code well enough. There are two classes: - vector_base: used as a base class for vector<T,A>; manages the allocated memory of a vector - myvector<T,A>: used to store objects The class vector_base is only aware of the big block of memory allocated for the vector, and is not aware of the objects stored in said block of memory. The destructor in your code, was the destructor for vector_base, which only handles cleaning up the vector's reserved memory. The destructor of myvector<T,A> is not shown, but it would contain the for loop to clean up the elements. The reserve() method was also for myvector<T,A>, and has the for loop for the reason I posted earlier. Also, isn't the second edition of 'Programming: Principles and Practice Using C++' the latest one? I checked, seems it is the latest version. Awww, I constructed and destructed the elements in the vector_base. I can see why I should not do that. Now it also makes sense why he used uninitialized_copy in the reserve function and could not rely on the vector_base destructor. At least I was right for feeling stupid I thank you Nesserev! | ||
RoomOfMush
1296 Posts
What I need is something like a small wiki. Platform independent, with hyperlinks to important topics, a table of content, search function would be nice; all the standard quality-of-life features we have nowadays. I would also like to generate this manual automatically; ideally programmatically. I would also like to open this user manual from inside the java application. I was thinking an HTML solution would be great for that. I could have the application start a browser and display the manual. Does anybody know of some tool / framework / toolkit / software / etc that can help me? | ||
Manit0u
Poland17071 Posts
| ||
| ||