|
TheSleeper posted:No offense, but isn't it a whole lot better to at most provide a pseudocode solution? I wasn't trying to provide the completely correct solution, just something that actually does the core part of the problem. Hopefully he/she will go over that code and fix and understand it before turning it in.
|
# ? Jul 23, 2008 10:26 |
|
|
# ? Jun 8, 2024 01:34 |
|
TheSleeper posted:No offense, but isn't it a whole lot better to at most provide a pseudocode solution?
|
# ? Jul 23, 2008 12:29 |
|
floWenoL posted:Unless someone overrode operator& for vectors somewhere before. I think you don't understand how std::swap works for vectors.
|
# ? Jul 23, 2008 13:04 |
|
I'm working on big number functions for some Project Euler stuff, but I'm running into some problems. I'm using a Dlist class that I made for my programming course last semester, and I need to change part of it, I'm just not sure how to do it. Here's my insertFront function: code:
code:
|
# ? Jul 23, 2008 15:45 |
|
Use an STL vector, with the most significant digit last. Adding to the end of a vector (push_back) is fast. If you really need to insert stuff at both ends of the decimal representation, use an STL list ("list" is doubly linked; "slist" is singly linked). If you want to write your own data structure, don't use pointers, use values. You're using templates, so your code is already abstract. If you want to use pointers, just allocate the int when you need to. int tmp=0; ... result.insertFront(new int<tmp>);
|
# ? Jul 23, 2008 18:14 |
|
Learning C++ out of a book called C++ Primer Plus, on the final programming exercise of the chapter I am on I've got a problem. The exercise is "Do Programming Exercise 6, but, instead of declaring an array of three CandyBar structures, use new to allocate the array dynamically". Exercise 6 is basically there is this CandyBar structure and it has 3 member, one to hold the brand name one to hold the weight which has to be able to hold fractional parts, and one to hold the weight which is an integer. And you have 3 of these structures in an array, you have to initialize it within the program, and then the program will display all these values on screen when run. code:
code:
rdgb fucked around with this message at 22:38 on Jul 23, 2008 |
# ? Jul 23, 2008 22:31 |
|
Just use std::string instead of an array of char. 99% of the time, char arrays aren't what you want (and it would take a lot more to explain how to do string manipulation the old-school C way).
|
# ? Jul 23, 2008 22:41 |
|
abc[1].name is an array; you can't assign to it directly like that. You'll need to use strcpy() to copy "thing" into the array. Or alternatively you could change "CandyBar::name" from an array into a pointer: code:
|
# ? Jul 23, 2008 22:46 |
|
Avenging Dentist posted:Just use std::string instead of an array of char. 99% of the time, char arrays aren't what you want (and it would take a lot more to explain how to do string manipulation the old-school C way). My compiler doesn't support the use of string class in structures, or at least, it doesn't appear to. When I change it to a string then run it, the program doesn't display anything as if it gets stuck on it or something.
|
# ? Jul 23, 2008 22:50 |
|
BloodE posted:My compiler doesn't support the use of string class in structures, or at least, it doesn't appear to. When I change it to a string then run it, the program doesn't display anything as if it gets stuck on it or something. It has to support that. That's one of the most basic C++ language features. You're doing something like the following, right? code:
|
# ? Jul 23, 2008 22:53 |
|
Standish posted:abc[1].name is an array; you can't assign to it directly like that. You'll need to use strcpy() to copy "thing" into the array. strcpy worked fine thanks. Avenging Dentist posted:It has to support that. That's one of the most basic C++ language features. You're doing something like the following, right? I am doing it like that and the compiler(Dev-C++)just doesn't display anything other than the black screen and the flashing _ cursor. My book says this of a structure using a string class member : quote:Can you use a string class object instead of a character array for the name member? That is, can you declare a structure like this: Of course the compiler I'm using works with string objects fine otherwise, but it doesn't seem to like strings in structures.
|
# ? Jul 23, 2008 22:58 |
|
BloodE posted:Of course the compiler I'm using works with string objects fine otherwise, but it doesn't seem to like strings in structures. I just tried it in Dev-C++ 4 point whatever and it works fine. Still, Dev-C++ is the shittiest IDE in the world. Just use Visual C++ Express. Also, this investigation has led me to believe that the author of that book is a complete tool. Avenging Dentist fucked around with this message at 23:12 on Jul 23, 2008 |
# ? Jul 23, 2008 23:09 |
|
You are obviously doing it wrong because every C++ compiler, especially g++ used in Dev-C++, supports strings as struct members. You will want to get a better book.
|
# ? Jul 23, 2008 23:12 |
|
I'm not sure why the compiler would discriminate against strings over any other class. And if it compiles at all, it's not the compiler's fault. If it really didn't support strings in structs for some reason, it would likely toss an error.
|
# ? Jul 23, 2008 23:14 |
|
Mustach posted:close() doesn't need to be explicitly called, anyhow. The destructors will take care of it. Yes, but it's important to get young programmers in the habit of allocating and freeing resources at appropriate times.
|
# ? Jul 23, 2008 23:16 |
|
TheSleeper posted:Yes, but it's important to get young programmers in the habit of allocating and freeing resources at appropriate times. And it's more important to get people in the habit of considering the concept of RAII when writing C++. Avenging Dentist fucked around with this message at 23:21 on Jul 23, 2008 |
# ? Jul 23, 2008 23:18 |
|
quote:Can you use a string class object instead of a character array for the name member? That is, can you declare a structure like this: I think this is saying that some old compilers don't allow initialization of structs with string members, like: code:
Also, I noticed this: quote:
The valid indices of a 3-element array are 0, 1, or 2, not 1, 2, or 3.
|
# ? Jul 23, 2008 23:58 |
|
Avenging Dentist posted:And it's more important to get people in the habit of considering the concept of RAII when writing C++.
|
# ? Jul 24, 2008 01:37 |
|
Avenging Dentist posted:And it's more important to get people in the habit of considering the concept of RAII when writing C++. Do you write return 0;?
|
# ? Jul 24, 2008 02:32 |
|
more falafel please posted:Do you write return 0;? What?
|
# ? Jul 24, 2008 02:38 |
|
Maybe he's trying to make some veiled commentary on you that's somehow related to how main() doesn't require a return statement, instead of just directly stating his point.
Mustach fucked around with this message at 03:26 on Jul 24, 2008 |
# ? Jul 24, 2008 03:22 |
|
Avenging Dentist posted:What? Yeah, this was not a well-thought out post. What I meant was that calling close() is kind of like return 0 in main() -- you don't need to do it, but it makes things clearer and is in general good practice, in my opinion. But then when I think about it, that means if I had a scoped RAII mutex class, I should call unlock() on it before it goes out of scope, which I wouldn't do. So never mind, for some reason I support calling close() on fstreams, even though there's no point.
|
# ? Jul 24, 2008 03:50 |
|
Pooball posted:If you really need to insert stuff at both ends of the decimal representation, use an STL list ("list" is doubly linked; "slist" is singly linked).
|
# ? Jul 24, 2008 09:23 |
|
When is it safe to use this idiom:code:
code:
|
# ? Jul 24, 2008 16:13 |
|
Lexical Unit posted:When is it safe to use this idiom: Vectors are always guaranteed to take up contiguous space, but after you resize the sequence, any pointer you had into the sequence before becomes invalid. Edit: So in other words, yes, your second example is safe (though the first one isn't because the vector is empty).
|
# ? Jul 24, 2008 16:15 |
|
That Turkey Story posted:Edit: So in other words, yes, your second example is safe (though the first one isn't because the vector is empty). Also, I'm just curious if there's a better way to get a char** out of a std::vector<std::string> than just allocating a char** big enough to store the memory and filling it manually in a loop: code:
Lexical Unit fucked around with this message at 18:19 on Jul 25, 2008 |
# ? Jul 25, 2008 18:16 |
|
Lexical Unit posted:Awesome. I thought I read somewhere that using that idiom to fill a vector was bad, but it was just on some crappy blog and I never really trusted it. And being that I almost never need to use that idiom, especially for filling up a vector, I never looked into it further. Thanks for the clarification Off the top of my head, I'd say it's bad because if you miscalculate the size of the vector so you don't actually have enough space, bad things will happen. It's not terribly defensive programming. But if you do everything right it should work fine.
|
# ? Jul 25, 2008 19:28 |
|
Lexical Unit posted:Awesome. I thought I read somewhere that using that idiom to fill a vector was bad, but it was just on some crappy blog and I never really trusted it. And being that I almost never need to use that idiom, especially for filling up a vector, I never looked into it further. Thanks for the clarification First, watch out because that code isn't exception safe! If any of those new operations in the for loop throw bad_alloc, you are leaking memory. Make sure you handle and rethrow or use RAII. A simple start is to make fruitloops a std::vector instead of a char** (or perhaps better, a smart pointer like boost::scoped_array if you have boost). Second, do you mean the API takes a char** or char const* const*? If it's just an API not using const correctly, as in it's taking const-unqualified data even though it's guaranteed to not modify the data, you can just make a std::vector or scoped_array of char const* and then const_cast when passing the data through the API.
|
# ? Jul 25, 2008 19:57 |
|
That Turkey Story posted:First, watch out because that code isn't exception safe! If any of those new operations in the for loop throw bad_alloc, you are leaking memory. That Turkey Story posted:Make sure you handle and rethrow or use RAII. A simple start is to make fruitloops a std::vector instead of a char** (or perhaps better, a smart pointer like boost::scoped_array if you have boost). That Turkey Story posted:Second, do you mean the API takes a char** or char const* const*? That Turkey Story posted:If it's just an API not using const correctly, as in it's taking const-unqualified data even though it's guaranteed to not modify the data, you can just make a std::vector or scoped_array of char const* and then const_cast when passing the data through the API.
|
# ? Jul 25, 2008 20:13 |
|
Lexical Unit posted:The API is this: http://tronche.com/gui/x/xlib/window-information/XInternAtoms.html (abandon all hope ye who clicks here). Have you considered using XCB instead of xlib? It's much nicer.
|
# ? Jul 25, 2008 20:21 |
|
ShoulderDaemon posted:Have you considered using XCB instead of xlib? It's much nicer.
|
# ? Jul 25, 2008 20:25 |
|
Lexical Unit posted:Oops! In other words (untested): code:
|
# ? Jul 25, 2008 20:42 |
|
Heh, I just wrote something like that (I didn't use the fancy call to std::transform()). I realized this was essentially the same thing with my question about the &vector[0] idiom. The pointer returned by c_str() is valid to use so long as the string remains unchanged, correct? I think I'm getting the hang of this... I hardly ever need to think about this because I'm usually not interfacing with C libraries. It seems to work just fine, thanks for all the pointers
|
# ? Jul 25, 2008 20:47 |
|
Lexical Unit posted:The pointer returned by c_str() is valid to use so long as the string remains unchanged, correct? I think I'm getting the hang of this... I hardly ever need to think about this because I'm usually not interfacing with C libraries. Yeah.
|
# ? Jul 25, 2008 20:54 |
|
Does anyone here know if anything has happened lately with the extensible literals proposal for C++0x? (I'm looking at you, TTS.) I feel like I'm the only one looking forward to it, just because it would allow the following:code:
|
# ? Jul 25, 2008 21:09 |
|
Avenging Dentist posted:Does anyone here know if anything has happened lately with the extensible literals proposal for C++0x? (I'm looking at you, TTS.) I feel like I'm the only one looking forward to it, just because it would allow the following: Would that not be about making operator[] a constexpr thing?
|
# ? Jul 25, 2008 21:53 |
|
Vanadium posted:Would that not be about making operator[] a constexpr thing? I guess you could do it with a combination of constexpr and the new function declaration syntax, but I was thinking that the "c" suffix would return an object of type boost::mpl::int_<...>. Why can't someone just write a compiler that implements all of 0x so I can play with it?
|
# ? Jul 25, 2008 22:34 |
|
I keep having with this code that's pretty much at the core of my program. It's been more or less like this for months, and it works fine until I try to do something special. (in C++)code:
|
# ? Jul 27, 2008 02:48 |
|
code:
EDIT x2: A super-simple solution would be to have act/maintain return a bool to specify whether actor should suicide. Avenging Dentist fucked around with this message at 03:11 on Jul 27, 2008 |
# ? Jul 27, 2008 02:55 |
|
|
# ? Jun 8, 2024 01:34 |
|
Avenging Dentist posted:
Yeah, that code won't work, because that crashes the program if I only have one element. I was working on another solution, but you brought up a point I wasn't expecting: how different is it to remove elements of a list between them being before or after? If not for that, there's no problem. If I define a static replacement, and use it instead, I can put in this (not yet working) code, that should give me a valid pointer after removing an actor code:
quote:EDIT x2: A super-simple solution would be to have act/maintain return a bool to specify whether actor should suicide. That's plausible, but I'm not sure if I understand. If I know in the loop whether or not I need to get rid of someone, how does that help me actually keep that iterator from going bad?
|
# ? Jul 27, 2008 03:19 |