Skinkie Posted August 31, 2005 Posted August 31, 2005 Ok I know there's a few programmers in the bunch here. This is how its going, I need to create a class to act as an array based unordered link-list. I've got no problem with the logic stuff that's no problem. My problem is they had us doing Java for a semester, then there was summer break, so my C++ syntax skills have become a bit rusty. Especially in the area of pointers and arrays. And especially since this program is suppose to use dynamic arrays which I've never used before. So I have the general string *pname = NULL; int *pnum = NULL; in the private data of the class and pname = new string[num]; pnum = new int[num]; in the constructor. so that should create the array correctly(if I'm wrong correct me). Now my question is how can I traverse this array. can I say things like pnum++; //Move to next item down list pnum = pnum[firstinlist]; //move pointer to 'first' item in list *pnum = 7; //set current spot to 7 pnum = pnum[7]; //put pointer on location 7 are all of those allowed? Any suggestions for this are appriciated. I've got a general idea of what I can/can't do, but I'd prefer to make sure.
DrMcCoy Posted August 31, 2005 Posted August 31, 2005 Skinkie said: pnum++; //Move to next item down list *pnum = 7; //set current spot to 7 ...These two are correct... However, pnum = pnum[7]; would set the current spot to the value of the 7th next element... Likewise, pnum = pnum[firstinlist]; would set the current one to the value of the firstinlistth (provided firstinlist is of a numerical type) next element... You'd need an additional pointer just iterating: int *pnum=NULL, *pnumi=NULL; pnum = new int[num]; pnumi = pnum; // set pnumi to the first element *pnumi = 5; // set the current spot to 5 pnumi[0] = 5; // ditto pnumi[2] = 42; // the the 2nd next element to 42 *(pnumi+2) = 42; // ditto *(2+pnumi) = 42; // ditto 2[pnumi] = 42; // ditto pnumi++; // move to the next element; pnumi[0] = 5; // set the currect spot (now the 2nd from the beginning) to 5 pnumi += 5; // move 5 places "down" pnumi -= 5; // move 5 places "up" int pos = pnumi-pnum; // get the current position pnumi = pnum; // "ground" the iterator, it points to the begining again Also, while doing this, you should watch that'll won't ever run into "invalid" memory, traveling beyond the boundaries of pnum, you may get a segfault there or overwriting other variables (could also lead to exploitation chances)...
Skinkie Posted September 1, 2005 Author Posted September 1, 2005 Thanks, this should be helpful, after like 8 months off it was sorta like "Declare a variable, what's a variable?" Probably not good since its my major.
Ray Jones Posted September 13, 2005 Posted September 13, 2005 Man, sometimes I am almost sure the inventor of this "syntax" also invented dyslexia.
DrMcCoy Posted September 13, 2005 Posted September 13, 2005 RayJones said: the inventor of this "syntax" Do you mean these two guy? (Brian W. Kernighan and Dennis M. Ritchie)
Ray Jones Posted September 14, 2005 Posted September 14, 2005 This is only one guy. One shot is taken from the right and one from the left. From the rear would be your avatar.
DrMcCoy Posted September 14, 2005 Posted September 14, 2005 RayJones said: This is only one guy. One shot is taken from the right and one from the left. What did I told you about losing your glasses? RayJones said: From the rear would be your avatar. At least I have an avatar!
Ray Jones Posted September 14, 2005 Posted September 14, 2005 DrMcCoy said: What did I told you about losing your glasses?Err.. You use it as an excuse for 'accidently' touching your gym teacher's boobs? ^^ Quote At least I have an avatar!Nah, I'm currently not in need of pictures.
DrMcCoy Posted September 14, 2005 Posted September 14, 2005 RayJones said: Err.. You use it as an excuse for 'accidently' touching your gym teacher's boobs? ^^ Exactly! And now go and do like I told you... RayJones said: Nah, I'm currently not in need of pictures. Ah, you're in the denial phase right now...
Ray Jones Posted September 14, 2005 Posted September 14, 2005 DrMcCoy said: Exactly! And now go and do like I told you...Err.. even if I'd wear glasses, when I compare my gym teacher's boobs to those four I can touch tonight .. no. Quote Ah, you're in the denial phase right now...Paperlapapp! More the 'no need for pictures' phase.
DrMcCoy Posted September 14, 2005 Posted September 14, 2005 RayJones said: Err.. even if I'd wear glasses, when I compare my gym teacher's boobs to those four I can touch tonight .. no. ...So you've got double-man-boobs? O_o RayJones said: Paperlapapp! More the 'no need for pictures' phase. *nods* Denial.
Ray Jones Posted September 16, 2005 Posted September 16, 2005 DrMcCoy said: ...So you've got double-man-boobs? O_oErr.. McCoy,.. boobs are mostly equal in size.. Reveal hidden contents this: O o - is a nightmare. Reveal hidden contents The boobs I am talking about usually look like this: (o)(o) .. Reveal hidden contents and like (°)(°), .. Reveal hidden contents and again (o)(o) *cough* Now you do the math what four of those look like. XD Quote *nods* Denial.Nah, I totally accept that I have none! Not even one single pixel. I am happy with it. Why waste tons of serverspace? I would even stop posting if it would spam the server.
DrMcCoy Posted September 16, 2005 Posted September 16, 2005 RayJones said: *cough* Now you do the math what four of those look like. XD ...These ones that look like my man-boobs? RayJones said: I am happy with it. ...Suuuure... RayJones said: I would even stop posting if it would spam the server. ...You mean like now?
Ray Jones Posted September 16, 2005 Posted September 16, 2005 DrMcCoy said: ...These ones that look like my man-boobs?How much beer have you been drinking already? Quote ...Suuuure...Yes, suuure, like in The Last Crusade. Quote ...You mean like now? Right now?? Nah. I'm not spamming. I'm currently flushing Ubuntu and installing Debian 3.1 stable, and am surprised, they still use the 2.4 Kernel (so I doubt it'll run out of the box on my Vaio, where I need a 2.6.9+ or somethin'..) OK, I could have known all this before but I wanted to give it a try anyways and.. HEY!! *points* LOOK!! *runs*
DrMcCoy Posted September 16, 2005 Posted September 16, 2005 RayJones said: How much beer have you been drinking already? Right now? No beer at all... RayJones said: installing Debian 3.1 stable, and am surprised, they still use the 2.4 Kernel (so I doubt it'll run out of the box on my Vaio, where I need a 2.6.9+ or somethin'..) ...You do know that you could have selected a 2.6 kernel with "bf26" at the boot menu, right? Or that you could simply compile your own one?
Ray Jones Posted September 16, 2005 Posted September 16, 2005 "bf26"????????????????????????????????Yesyes, I know. I checked the ultra default default installation on purpose. Err.. It appears to be "linux26" btw.. [EDIT] (took the edit and made a new post.. ^^)
DrMcCoy Posted September 16, 2005 Posted September 16, 2005 RayJones said: Err.. It appears to be "linux26" btw.. Oh... I thought it was "bf26"... *shrug* Anyway, I know Sarge has a 2.4.x and a 2.6.x kernel, just as Woody had 2.2.x and 2.4.x...
Ray Jones Posted September 16, 2005 Posted September 16, 2005 WHOA!, I joined the fun and did expert-mode install, there are like billions of kernels available.. ^^; I will however, compile my own anyways.. especially because I need one above 2.6.8. But later. But this way I do not need to bug around with the modutils..
Skinkie Posted October 16, 2005 Author Posted October 16, 2005 Alright, here we go round 2 of the syntax battle. This week it's accessing a Vector of Lists. I can feel your excitement building already. So the basics of this project is we're given a huge text file and have to parse the words out into lists. Each list contains all words of the same length (first list is all the one letter words, second list is two letter, and so on.) But these lists have to be stored in either a vector or another list. Right now I'm trying to get it to work with a vector and it works like element 1 holds the list of one letter words, and so on. My problem is getting the iterator to work. So I'm gonna throw up some generalized code of mine in hopes that someone will point me in the proper direction of how to iterate this monster. int main() { ifstream getword; //instantiates input variable getword.open("p3.txt"); //opens input file string tester; //string variable char ch; //char for browsing file vector<list <string> > wordcount(25); //Here's the vector list<string>::iterator it; // Here's the iterator int i; /*Code Here that reads the text from the file (this works fine, I've tested it and it's properly reading each word and can correctly write them to a file, so no help is needed here.) */ wordcount[tester.length()].push_back(tester);; /*This line I *think* is pushing the word back onto the appropriate list. If it does what I'm thinking it finds the length of string variable tester and uses that as the vecotr index and then push_back's the word onto that list. *If* I'm right*/ } /*Here's where it gets confusing, I'm trying to cout the elements of each list, on list at a time, in order. (So again, all 1 letter words will be printed, then all 2 letter words, and so on) But the compiler throws a fit over the second for statement.(see bottom for error message) If I remove the for statement and just set it to wordcount.begin() then cout it compiles, but gives me a segmentation fault on runtime. So I figure there is a mistake in the iterator syntax somewhere. I've tried googling for it, and I've tried finding anything even remotely similar in our book (at some point it has to have an answer to something... right?) but have found nothing, so I figured this would be a good time to come back here looking for help. (*Noncheating Disclaimer* And again like last time, I'm looking for help finding errors in my code, and ways to fix my syntax problems, not any kind of full solution or anything like that)*/ for(i = 0; i < 25; i++) cout << "Words with " << i << " letters"; for(it = wordcount.begin(); it < wordcount.end(); it++) cout << *it << "\n"; } return 0; } /*Compiler Error g++ test.cpp test.cpp: In function `int main()': test.cpp:49: no match for `_List_iterator<basic_string<char,string_char_traits<c har>,__default_alloc_template<false,0> >,basic_string<char,string_char_traits<ch ar>,__default_alloc_template<false,0> > &,basic_string<char,string_char_traits<c har>,__default_alloc_template<false,0> > *> & < _List_iterator<basic_string<char ,string_char_traits<char>,__default_alloc_template<false,0> >,basic_string<char, string_char_traits<char>,__default_alloc_template<false,0> > &,basic_string<char ,string_char_traits<char>,__default_alloc_template<false,0> > *>' */
DrMcCoy Posted October 16, 2005 Posted October 16, 2005 Skinkie said: wordcount[tester.length()].push_back(tester);; /*This line I *think* is pushing the word back onto the appropriate list. If it does what I'm thinking it finds the length of string variable tester and uses that as the vecotr index and then push_back's the word onto that list. *If* I'm right*/ That line will only work if the vector has at least tester.length()+1 elements, if not, that'll either throw an exception or segfaults... You should do a wordcount.resize(maximumwordsize) before doing any operation on the wordcount... Or do if(tester.length() >= wordcount.size()) wordcount.resize(test.length()+1); before each push_back()... (The latter is more time costly, since it often copies the whole memory each time... If there is a maximum word length, use the former)... ... As for counting the lengths, you can't do it that way for lists... Since a list is a double-linked-list, its iterators don't have a < operator, so you can't use the for-loop... Do something like this instead: for(unsigned int i=0;i<wordcount.size();i++) { cout << "Words with " << i << " letters:" << endl; it = wordcount[i].begin(); while(it != wordcount[i].end()) { cout << " " << *it << endl; it++; } } You could also use a vector of a vector of strings instead of a vector of a list of strings... A list is normally used when you have to insert and delete stuff from the middle of it... Since they are double-linked list, the only thing that has to be done is updating a few pointers, instead of coping lots and lots of memory if you'd do that with vectors... On the downside, you can't access random elements of a list like you can with vectors, you'd always of to traverse them from one end (which is, of course, slower)... Furthermore, in lists, each element takes up slightly more memory, thanks to the pointers... And depending on what you do, you could also use deques instead...
Skinkie Posted October 16, 2005 Author Posted October 16, 2005 Thanks McCoy, you're always good in a pinch, and yeah I realize that list's are not neccessarily the best way to go about this, but it's required for the assignment we use them. It's supposed to be practice implementing them and stuff. You know teachers.
DrMcCoy Posted October 16, 2005 Posted October 16, 2005 Skinkie said: Thanks McCoy ...np... Skinkie said: you're always good in a pinch ...Err, I'm good in a what/where? Skinkie said: and yeah I realize that list's are not neccessarily the best way to go about this, but it's required for the assignment we use them. It's supposed to be practice implementing them and stuff. You know teachers. ...Yes, unfortunatly I do...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.