|
std::valarray and its slices handle all your row/column business pretty neatly, and don't need tr1.
|
# ? Apr 14, 2011 02:50 |
|
|
# ? Jun 8, 2024 05:14 |
|
Hey, fairly new to C++, having trouble getting to grips with reading from external files. My problem: I want to store a .txt dictionary file in my trie data structure. I have created the trie class, and the function to add words prototype(?) is like so code:
code:
I have a txt file which is an entire dictionary with 1 word per line. I've tried various methods to add these words to the char[] but it never works, and my knowledge of c++ is limited. Anyone got any ideas?
|
# ? Apr 14, 2011 19:39 |
|
Fox1 posted:I have a txt file which is an entire dictionary with 1 word per line. I've tried various methods to add these words to the char[] but it never works, and my knowledge of c++ is limited. Anyone got any ideas? If you're using C++, you should use ifstream and getline(stream, string) to read a flat text file, generally speaking. Any reason you're using char * instead of string? Something like this: (I may have the syntax wrong as I'm going off memory, and this does not check for blank lines or lines that contain more than one word) code:
|
# ? Apr 14, 2011 19:56 |
|
Because the trie structure is a linked list of single chars, making a tree for my lexicon. It is part of a scrabble AI, eventually the trie will be traversed to look for valid words that can be made from the board and the players rack. I don't think it can be done with strings because of how words can be made in scrabble (combo of rack and board letters)
|
# ? Apr 14, 2011 20:11 |
Even if you're going to be storing the strings in a tree you can just as well work with STL strings instead, if only for the simplified memory management. If your tree insertion function will still be simpler when done with char pointers (it might be) you should still be using the string type when reading the file, IMO, and then just get the const char * pointer to the string's storage (with the string::c_str() function) for passing into the tree.
|
|
# ? Apr 14, 2011 20:27 |
|
Ok, c_str() looks promising actually, I will have a play with that.
|
# ? Apr 14, 2011 20:39 |
|
No clue if this is the right place, forgive me if it is wrong! I'm not asking for someone to do my homework, I'm asking for someone to explain why this isn't working and provide a solution I've run into a rut with my code for a project, and it's driving me insane because I did similar things in previous programs and they all worked fine. Here's my problem: I have an object O, and a pointer P that points to O. I now link P with other pointers, and those pointers with pointers. A big ol' linked list. However, when I check out O, O is not linked with anything. What am I doing wrong? Here's the dirty stuff Main.cpp code:
code:
code:
|
# ? Apr 14, 2011 20:49 |
Princess Kakorin posted:Main.cpp First you create an auto-allocated tree object, which means it'll be allocated on the stack. Then, you declare a pointer to a tree, allocate a new tree object on the heap with operator new. You now have two tree objects, one on the stack and one on the heap. Finally, you assign the tree on the stack to the tree on the heap. This copies the contents of one tree object into the other, it does not make the pTree variable point to the stack-allocated tree. What you meant to do: code:
Lastly, in your original code, you leak a single tree object, the one you heap allocated with operator new, since you never delete it. E: Wait, your tree class also looks very strange. I don't think it sounds like a good idea to have a mutable tree object that walks its own children. You should separate the representation of the tree itself from the walking of it. E2: Okay maybe it is technically possible to do as you're trying to do, and in that case what you're doing in your main() would be correct, but it still looks like a bad idea to handle trees in. Tree algorithms are usually implemented recursively or using an explicit stack or queue to walk the trees, and your way of having a mutable tree objects complicates this. Is there any specific reason you're doing like this? nielsm fucked around with this message at 21:15 on Apr 14, 2011 |
|
# ? Apr 14, 2011 21:05 |
|
nielsm posted:That's how how pointers or the dereferencing operator work. Ah, well I was going to delete it after I got everything handled. Now, I've tried to do the &myTree, but when I populate P and do the getNext and everything using pTree and go back to myTree, myTree is exactly where pTree is. I'm probably being dumb right now, with 3 weeks left in the semester my brain is dead. Any idea on how to return myTree to the top without creating another function to have me scale all the way back up? Because then having the pointer is pointless(Haha! puns.), and I was hoping to save time by just using the pointer to add the data without having me have to scale back to the top. Edit: I didn't see your edits, I should probably explain the project some more. I'm supposed to make an "intelligent" 20-questions game using a binary tree, and if it can't figure it out it asks the user to tell it what it is, and a question associated with it, which it will then insert. Because of this, I figured it'd be simpler to create the tree and a pointer to the tree, and just populate and scale with the tree, and delete the pointer when I'm done. Like I said, I'm probably being dumb(No, I know I am because I keep comparing a binary tree to linked lists, which is why I'm trying to put this pointer in.) Should I just suck it up and do it recursively? Princess Kakorin fucked around with this message at 21:20 on Apr 14, 2011 |
# ? Apr 14, 2011 21:12 |
|
There's a couple of things about the way C/C++ is normally written that I think really hurts initial understanding.code:
|
# ? Apr 14, 2011 21:21 |
wellwhoopdedooo posted:There's a couple of things about the way C/C++ is normally written that I think really hurts initial understanding. Because this line declares one int pointer and one int: code:
|
|
# ? Apr 14, 2011 21:27 |
|
nielsm posted:Because this line declares one int pointer and one int:
|
# ? Apr 14, 2011 21:54 |
|
roomforthetuna posted:Which is stupid too! Perhaps even stupider! Yeah, that was in Bjarne's rant too. It's been so long since I've declared multiple variables on one line, I completely forgot about that.
|
# ? Apr 14, 2011 21:57 |
|
wellwhoopdedooo posted:Yeah, that was in Bjarne's rant too. It's been so long since I've declared multiple variables on one line, I completely forgot about that.
|
# ? Apr 14, 2011 22:04 |
|
Is there anything like boost::array where you can specify the size on construction, and then it's fixed? Looking for a container suitable for objects which are not copyable or moveable, but with minimum allocation overhead. Also, is there anything similar to std::queue where the maximum size is specified as a template parameter (ala boost::array), with storage on the stack but a variable size? Bonus points if its push_back method accepts boost::in_place_factory for non-copy constructible types. Paniolo fucked around with this message at 03:16 on Apr 15, 2011 |
# ? Apr 15, 2011 02:01 |
|
Paniolo posted:Is there anything like boost::array where you can specify the size on construction, and then it's fixed? Looking for a container suitable for objects which are not copyable or moveable, but with minimum allocation overhead. e: oh you mean explicitly during construction and not as a template parameter. VVVVV TasteMyHouse fucked around with this message at 02:21 on Apr 15, 2011 |
# ? Apr 15, 2011 02:12 |
|
TasteMyHouse posted:erm... Boost.Array is like that already? I should have clarified: by on construction I mean at run-time, not at compile-time, i.e. the maximum size as a parameter to either the constructor or an init() method.
|
# ? Apr 15, 2011 02:14 |
|
1. Why do you want this specifically? i.e. why is the template solution no good? 2. if you're doing it with a ctor, it is going to be inherently something done at run-time, so dynamic allocation is going to occur somewhere. you could fairly easily make your own class that wraps a call to new, and I think that's pretty about as good as you're gonna get anyway. Someone correct me if I'm wrong.
|
# ? Apr 15, 2011 02:21 |
|
wellwhoopdedooo posted:
I always write my code as int* x, and I hate the fact that pretty much every IDE does int *x instead. x is of type int*, so int* x makes infinitely more sense than int *x. I almost never declare multiple variables on the same line, and when I do, I don't mix ints and int*'s anyways. I wish there was an option in visual studio to change the default so that it makes more sense, instead of me having to change it manually afterwards.
|
# ? Apr 15, 2011 08:12 |
|
Amusingly, and probably because of how I write C++ references, I eventually split the difference and started writing code:
|
# ? Apr 15, 2011 15:06 |
|
BigRedDot posted:Amusingly, and probably because of how I write C++ references, I eventually split the difference and started writing This is what I do too.
|
# ? Apr 15, 2011 16:16 |
|
GrumpyDoctor posted:This is what I do too. I used to. Then I had to change it because another project member said it made them think I was multiplying them. It's not important why different styles were adopted.
|
# ? Apr 15, 2011 20:33 |
|
code:
And if you insist on thinking that way, you will NEVER understand function pointer declarations. Fecotourist fucked around with this message at 04:21 on Apr 16, 2011 |
# ? Apr 16, 2011 04:17 |
|
Fecotourist posted:
I think the point of the complaint is not that it's a better representation of what the compiler does, it's that it's a better representation of what the programmer does, and is thus what the compiler should do. To clarify: code:
quote:And if you insist on thinking that way, you will NEVER understand function pointer declarations.
|
# ? Apr 16, 2011 05:49 |
|
Fecotourist posted:
I really wish this were a troll.
|
# ? Apr 16, 2011 06:14 |
|
wellwhoopdedooo posted:There's a couple of things about the way C/C++ is normally written that I think really hurts initial understanding. Because declaration mimics use.
|
# ? Apr 17, 2011 09:13 |
|
When I was learning, I never really "got" pointers until I had a teacher that wrote them int*. But that is kind of a bad idea, because "int* foo, *bar" just looks dumb. Honestly, the language should just be different. writing "int* foo" isn't good, but writing "int *foo" confuses learners.
|
# ? Apr 17, 2011 16:21 |
|
evensevenone posted:When I was learning, I never really "got" pointers until I had a teacher that wrote them int*. But that is kind of a bad idea, because "int* foo, *bar" just looks dumb. This is where D gets it right. code:
|
# ? Apr 17, 2011 19:01 |
|
Did D actually go anywhere?
|
# ? Apr 17, 2011 20:36 |
|
What is a good build system for C/C++ projects? I have been using cmake and love it so far, but there is probably something better out there?
|
# ? Apr 17, 2011 20:59 |
|
Vanadium posted:Did D actually go anywhere? Um, yes? D2 is in active development and there are a decent amount of projects written in D. Which are mostly listed here. github D projects: nice to see XOmB up there for being most watched. There was a D thread in COBOL a while back.
|
# ? Apr 17, 2011 21:37 |
|
What do you guys think of this? Is it just a massive troll? Reading it, I can tell where he's blatantly wrong sometimes, but I'm not savvy enough to have a rebuttal to everything he says on the tip of my tongue.
|
# ? Apr 17, 2011 22:39 |
|
TasteMyHouse posted:What do you guys think of this? Is it just a massive troll? Reading it, I can tell where he's blatantly wrong sometimes, but I'm not savvy enough to have a rebuttal to everything he says on the tip of my tongue. What is he "blatantly wrong" about that he hasn't corrected? And are you assuming that there's a rebuttal to everything he says? e: other than the way he complete ignores generic programming as an idea raminasi fucked around with this message at 23:12 on Apr 17, 2011 |
# ? Apr 17, 2011 22:50 |
|
TasteMyHouse posted:What do you guys think of this? Is it just a massive troll? Reading it, I can tell where he's blatantly wrong sometimes, but I'm not savvy enough to have a rebuttal to everything he says on the tip of my tongue. I read it as less of a troll and more of a whiny rant. No language is without its drawbacks, but most of the ones he mentions are generally irrelevant and can be easily circumvented by higher level programming practices.
|
# ? Apr 17, 2011 23:17 |
|
GrumpyDoctor posted:What is he "blatantly wrong" about that he hasn't corrected? he said you could get the address of a variable with a C++ reference, when thats really not how they work. He also trashed initializer lists, calling them "ugly" which I guess is his opinion, but I think its a wrong opinion =P.
|
# ? Apr 17, 2011 23:30 |
|
Elfforkusu posted:The appropriate response to most of his complaints is "So? And?" With most higher-level languages the flaw is "no access to system stuff" - hence Java GUIs frequently looking terribly out of place, for example, and generally relatively poor support for inherently low-level operations like talking to graphics cards. Either that or the "not even remotely portable" problem, as with C# or Objective C.
|
# ? Apr 17, 2011 23:35 |
|
YosefK is a whiny baby, but sometimes right. However, there are many sources that are also right that aren't nearly as whiny, annoying, and badly written as that website.pr0metheus posted:What is a good build system for C/C++ projects? I have been using cmake and love it so far, but there is probably something better out there?
|
# ? Apr 18, 2011 01:35 |
|
TasteMyHouse posted:What do you guys think of this? Is it just a massive troll? Reading it, I can tell where he's blatantly wrong sometimes, but I'm not savvy enough to have a rebuttal to everything he says on the tip of my tongue.
|
# ? Apr 18, 2011 02:02 |
|
edit: (I ended up looking at the call stack, and none of what I said makes sense, so disregard this. I'll leave it here for posterity. Or something) I have a brief question. Reading the documentation for Grand Central Dispatch implies that recursive calls to dispatch_sync result in a deadlock. Why then does this work? Wouldn't each subsequent dispatch_sync call wait for the next one to finish, but be in the "currently executing" position? code:
code:
Slurps Mad Rips fucked around with this message at 08:17 on Apr 18, 2011 |
# ? Apr 18, 2011 07:03 |
|
|
# ? Jun 8, 2024 05:14 |
|
pr0metheus posted:What is a good build system for C/C++ projects? I have been using cmake and love it so far, but there is probably something better out there?
|
# ? Apr 18, 2011 07:12 |