Jump to content

Home

C++ Syntax


Skinkie

Recommended Posts

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.

Link to comment
Share on other sites

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 :D

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)...

Link to comment
Share on other sites

  • 2 weeks later...
...So you've got double-man-boobs? O_o
Err.. McCoy,.. boobs are mostly equal in size..

 

this: O o - is a nightmare.

 

 

 

The boobs I am talking about usually look like this: (o)(o) ..

 

 

 

 

and like (°)(°), ..

 

 

 

 

and again (o)(o)

 

 

*cough* Now you do the math what four of those look like. XD

 

*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.
Link to comment
Share on other sites

...These ones that look like my man-boobs?
How much beer have you been drinking already?

 

...Suuuure...
Yes, suuure, like in The Last Crusade.

 

...You mean like now? ;P
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*

Link to comment
Share on other sites

How much beer have you been drinking already?

 

Right now? No beer at all...

 

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?

Link to comment
Share on other sites

  • 1 month later...

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> > *>'

*/

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Thanks McCoy

 

...np... :)

 

you're always good in a pinch

 

...Err, I'm good in a what/where? :confused:

 

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... :lol:

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...