# Homework Question

• 02-16-2008
Jozrael
Homework Question (New one)
First problem fixed, segfault problem below.

<Ignore>Ok, so for my assignment, I need a list of list of list of ints. We're working with lists this week, so more elegant data structures are not allowed :p. In any case, I'm wondering if theres a way to 'name' each of these lists. More simply:

Lets say I need a list of streets, each with a list of house numbers. This would be a list of list of ints, but I would also need a separate list of strings for the street names. I'm wondering if I could combine these somehow so that the list of list of ints somehow still contains the information on the street names.

I can give any more information to help better explain the problem.</Ignore>
• 02-16-2008
linucksrox
Well without using a more elegant data structure, no you can't combine strings with ints into one structure. I'm really not sure exactly what you're asking, but it sounds like you need a 2 dimensional array of ints and also an array of strings that correlates with the ints. Or instead of arrays, if you're actually using lists (from the STL) it's the same way. So you already said it:
Quote:

This would be a list of list of ints, but I would also need a separate list of strings for the street names.
Do you still need help with the code?
• 02-16-2008
anon
You could combine them by making a list of struct (or std::pair) that contain a string (the street name) and a list of ints (house numbers).
• 02-16-2008
Jozrael
Alright, well, I 'solved' the first problem by throwing a whole series of classes into the equation. It works fine, except for the fact that I'm getting a segfault while running my program (its still not fully written, I just like to debug it continually as I write :x). I was wondering if I could get advice as to how to fix this particular segfault. I've narrowed it down to a set of for loops with some odd behavior. Here it is:

Code:

```for(list<Route>::iterator itr = routes.begin(); itr != routes.end(); itr++) {     for(list<Street>::iterator itr2 = itr->getStreets().begin(); itr2 != itr->getStreets().end(); itr2++) {         blah blah;     } }```
Oddly enough, when I comment out the for statements and just put each of the parts of them in their own command (like, taking each of the 3 parts of the for loop and putting it on its own line of code, not looped, its fine. Its just when looped together it segfaults.

Each of my classes (Route, Street, etc) has a list of the next Class. I.e. route has a list of Street. The lists are not initialized to zero (since doing that caused funky problems of its own). Any ideas on where I might start looking? Thanks for your help, and thank you to the two people that already posted.

EDIT: The problem lies in the second for loop: the program doesn't like to increment itr2. I'm guessing it goes out of bounds, but I'm not sure how to prevent this.
• 02-16-2008
dra
are you allowed to use structs?

Code:

```struct street_n_numbers{ std::string street; std::list<int> house_nums; };```
• 02-16-2008
Jozrael
No, we're only allowed to use classes in this course. And this is how I setup my program.

As to the segfault, I found some interesting information via some debugging cout statements...my memory management must be terrible, because the street names arent being written correctly to the variables =\.
• 02-16-2008
Jozrael
Well, I found an odd workaround to the problem, but I'm still not sure why it didn't work in the first place =\. I'll post it later (once I've met the deadline) so I can find out what I was doing wrong.
• 02-17-2008
CornedBee
Structs are classes. But if your teacher is opposed to the struct keyword, you can do this:
Code:

```class street_addresses { public:   std::string street;   std::list<int> house_nums; };```