tags:

views:

208

answers:

3

I have a class here that contain linked list of other class cars example

class transportation
{
    private;
     int id;
     list <car> *cars;
     int keyval;
    public :
      set()
      {
      }

      void setTransportation(int ids, list <car> *carss, int keyvals)
      {
          id=ids;
          cars[keyvals]=carss[keyvals];
          keyval=keyvals;
      }

      list <car> *getCars()
      {
          return cars;  // I have to accessed cars list to be modified in my main
                        // is this how I do it ?????
       }

above is only a excerpt from my code. What I have to do is to set list of cars in the main, thus I will call setTransportation in main and pass in list of carss to build up the linked list of car. Am i doing the right thing on setTransportation? As i have to dynamically allocated memory to array of list cars, how do I do that in my main function?

int main()
{
    car bmw;
    transportation *ptr=new transportation;
    list <car> carList[100];
    list <transportation> transportationList;

   //other code
      if (i == 0) {
     bmw.setCar(iseq,type);
     carList[k].push_back(bmw);
     list <car> *inputList = (*ptr).getCars(); // is this how I accessed list from 
     inputList[k]=new list <car> [k];            // list class???
     (*ptr).setTransportation(iid,carList,k);
     transportationList.push_back((*ptr));
     k++;
     }

Any help will be appreciated

+1  A: 

Take a look at iterators.

graham.reeds
do you mean that I need to use iterator?but initially, my list would not contain anything, why should I use iterator then?
But like Skurmedel says, if you are copying from one list to another an iterator is an ideal choice.
graham.reeds
+1  A: 

In setTransportation, you could pass in a reference to a std::list and copy all the cars from the former list to the latter.

This is done with iterators (see grahams answer) and the insert-method.

Example:

std::list<car> cars();
// Fill cars with values.
std::list<car> otherCars(); // We could use copy-constructor here but not relevant.
otherCars.insert(otherCars.begin(), cars.begin(), cars.end()); // First argument is the position where you want to insert the values.

setTransportation could thus look like this:

setTransportation(int id, const std::list<car> &cars, int keyvals)
{
    id = id;
    this->cars.insert(this->cars.begin(), cars.begin(), cars.end());
    keyvals = keyvals;
}

Note that you would have to change the type of "cars" in transportation to "std::list< car >" instead of "std::list< car > *" which is a pointer to a list of cars.

If you have a "using namespace std;" at the top of your file you don't need the std::-prefix in front of list< T >.

Skurmedel
+1  A: 

You have a fair amount of questionable code in your example. I'd like to point a few of them out to make sure the meaning of your code actually reflects your intentions.

 list <car> *cars;

This is a pointer to a list of cars. a pointer is probably not necessary

 list <car> carList[100];

This is a "100-element array of lists of car objects". You may have thought you were making a "list of car objects 100-elements long" but this is not the case.

 inputList[k]=new list <car> [k];

I think this creates a new array of list objects 'k' elements long and assigns it to the k-th element of 'inputList'.

All-in-all, very dubious use of pointers. Forgive me if I've misjudged and these were, in fact, intentional. Follow the other answers' advice for eliminating the use of pointers.

veefu