Confused and lost.....

Hi everyone,

I am working on an a school asignment, basically create a build in list.(why we cant use the STL do not know..)

I am not familiar with templates and not so good at pointers. My function that I do not get is the insert(). I am not sure how to aproach the parameter passed
I am thinking it should be an int x but since it deals with different data types it has to be like that. But I have no idea on how to even get started.

My class looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template<typename TYPE>
class OrderedCollection
{
private:
  int size, space;
  TYPE* elements;
public:
  // the big three
  // and many other function
  void insert(const TYPE& x);  // this is the function that I am confused
};
// this function is supposed to insert an element and sort the elements
// but the parameter past is TYPE and I have no clue what that means and how to aproach it
void OrderedCollection<TYPE>::inseret(const TYPE& x)
{
  // not sure what goes in here
}


Everyone thanks in advance,for any help at all
const TYPE& x is a const reference.
Using it simply means you don't copy the parameter as a local variable in the insert() function, but use the original variable itself, and you promise not to change its value by const.

You might use TYPE x for simplicity, but it'll waste time and space if TYPE is something large instead of int, double, etc.
Last edited on
So if I do this it will work???
But I still od not know how to sort if this is properly writen.....???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <typename TYPE>
void OrderedCollection<TYPE>::insert(const TYPE& x)
{
  size = size + 1; // increase the size by one

  TYPE* temp = newTYPE[size]; // I have to increase array
  for(int i=0; i<size; i++)
  {
    temp[i] = elements[i]; // here we copy the elements in new temp array
  }

  temp[size-1] = x; // here we add the x at the end of the array
  delete [] elements;// delete the old elements
  elements = temp; // add the temp elements in current elements
}
Typo at line 6, leave a space between new and TYPE.
size = size + 1; can be written as ++size; or size+=1; for simplicity.
Curly braces not needed when only one instruction is in for(), if(), while() or do while();.

One bug in your code is that at the end of the for(), you will copy an element that doesn't exist.
Your code should run fine, but only because you don't modify that element's value.
Secondly you don't sort anything, you just add the new element to the end of the elements.

Finally, be sure to have a dummy elements = new TYPE[1]; (in Constructor) before running insert() for the first time. And obviously, the delete[] elements; in the Destructor.

Here you go, my version. It's your business if you learn from it, or just steal it. Good luck.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <typename TYPE>
void insert(const TYPE &x)
{
    int where=0; // where to insert the new element

    while (elements[where] < x && where <= size) // find where
        ++where;

    size += 1; // update size
    TYPE *temp = new TYPE[size];
    temp[where] = x; // add x

    for (int i=0; i<where; ++i) // copy first part from elements
        temp[i] = elements[i];

    for (int i=where+1; i<size; ++i) // copy second part from elements
        temp[i] = elements[i];

    delete[] elements;
    elements = temp;
}


Edit: typos.
Edit2: contains some bugs too. Happy hunting.
Last edited on
Topic archived. No new replies allowed.