Vectors and Templates

Can someone help me finish writing this code? Ive made it so that my_vector.cpp compiles. However, i need to complete my_vector.cpp using my_vector.h file.

my_vector.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<iostream>
#include <string>
#include "my_vector.h"

using namespace std;

template <class T>
my_vector<T>::my_vector()
{
	my_size = 0;
	my_capacity = 1;
	buffer = new T[my_capacity];
	
} 
template <class T>
my_vector<T>::my_vector(unsigned int size)
{
	my_size = size;
	my_capacity = my_size;
	buffer = new T[my_capacity];
}
template <class T>
my_vector<T>::my_vector(unsigned int size, const T & initial)
{	
	my_size = size;
	my_capacity = my_size;
	buffer = new T [my_capacity];
	for (int i = 0; i < my_capacity; i++ ) 
		buffer[i] = & initial;
}
template <class T>
my_vector<T>::my_vector(const my_vector & v)
{
	my_size = size;
	my_capacity = my_size;
	buffer = new T [my_capacity];
	for (int i = 0; i < my_capacity; i++)
		buffer[i] = &v;
}
template <class T>
my_vector<T>::~my_vector()
{
	delete [] buffer;
}
template <class T>
int my_vector<T>::capacity() const
{
	
	
return my_capacity;
}

template <class T>
unsigned int my_vector<T>::size() const
{
return my_size;
}

template <class T>
bool my_vector<T>::empty() const
{
	return my_size();
}
template <class T>
void my_vector<T>::push_back(const T & value)
{
	if (my_size>my_capacity)
		push_back(& value);
}
template <class T>
void my_vector<T>::pop_back()
{
	while (!buffer.empty())
 	 {
  	  buffer.pop_back();
 	 }

}    
template <class T>
void my_vector<T>::reserve(unsigned int new_capacity)
{
	T* temp = new T [new_capacity];
	for (int i = 0; i < my_capacity; i++)
	temp[i] = buffer[i];
	delete buffer;
	buffer = temp;
	my_capacity = new_capacity;
		

}
template <class T>
void my_vector<T>::resize(unsigned int new_size)
{
	if (new_size > my_size)
		{
		my_size = new_size;
		reserve(new_size);
		}
	else
	{
		my_size = new_size;
	}

}
template <class T>
T & my_vector<T>:: operator [ ](unsigned int index) const
{
	return buffer [index];
}





my_vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef MY_VECTOR_H
#define MY_VECTOR_H

// my_vector.h
using namespace std;

template <class T>  
class my_vector
{
  public:
    // Constructors / Destructor
    my_vector(); 
    // Constructor with initial size
    my_vector(unsigned int size);
    // Constructor with initial size -- set all initial spaces to initial param
    my_vector(unsigned int size, const T & initial);    
    // Copy constructor
    my_vector(const my_vector & v); 
    ~my_vector(); 

    // Return capacity of vector (in elements)
    int capacity() const; 
    // Return the number of elements in the vector
    unsigned int size() const; 
    // Return true if vector has no elements
    bool empty() const; 

    // Add a new element to the end of the backing array 
    // Don't forget to resize if needed!
    void push_back(const T & value); 
    // Remove the last element (don't return it)
    void pop_back(); 
    // Adjust capacity (make more space as needed)
    void reserve(unsigned int new_capacity); 
    // Resize vector (even if that means making it smaller)
    void resize(unsigned int new_size); // adjust size

    /* Overloaded operators */
    // Return a *reference* to numbered element
    T & operator [ ](unsigned int index) const; 

  private:
     unsigned int my_size;
     unsigned int my_capacity;
     T * buffer;
};

/* put all your code in my_vector.cpp */
#include "my_vector.cpp"

#endif

Last edited on
Wait a minute... I recall there being something with template definitions in a C++ file generating errors... and considering how templates are implemented... ah, sleepy day. *shakes self*

As for your actual code, have you covered dynamic memory allocation? As in new and delete? Those would help you a lot with push_back(), pop_back(), reserve(), and resize().

-Albatross
Last edited on
Yeah we have but i dont really know how to implement it into my code. Can you show me how its done using my code?
buffer = new T[my_size];
^Any ideas from that? Again, I'm half asleep but... no pain in trying, right?

-Albatross
well i got help with that piece of work but i know that a new array was made and put into buffer. I believe now we place all the initial values into this array but i dont know how to do that. thank you for helping even though your half asleep :D
Try a for loop, starting from 0 and with a condition of i < my_size (I think you can figure out what happens per iteration).

-Albatross
I believe it is this:
1
2
3
for (int i = 0; i < my_size; i++ ) 
		buffer[i] = my_capcity[i];

Someone help me with this code. I could really use some help!
Could you be more specific as to which function is giving you the most trouble?

(Oh, and are you sure about my_capacity[i]?)

EDIT: Would reading this help? http://cplusplus.com/doc/tutorial/dynamic/

-Albatross
Last edited on
The constructor and destructor functions are giving me trouble. i went to get help but they werent much help. Oh i already read that and but i dont know how to apply it.

And im not sure about my_capacity[i]. I believe it puts the items i have in capacity to buffer but im not sure if im right.
Firstly, my_capacity is an unsigned integer, so [i] might cause problems, as might the fact that buffer is a template... (oh eff).


Constructor 1:
Set everything to NULL.

Constructor 2:
1
2
3
my_size = size;
my_capacity = my_size;
buffer = new T[my_size]; //This is a solution. I really shouldn't have given it.... 


Constructor 3:
1
2
3
my_size = size;
my_capacity = my_size;
buffer = new T[my_size];

You will then need a for loop to copy initial into each space of your reserved space. I hope you can figure that out.

Copy Constructor:
I leave that one to you. Consider: my_size will equal my_capacity, which will equal the size of the other vector, and not only can you reserve your space ahead of time based on that, but you can also use it for the bounds of your loop, which will copy the elements from the other vector to the initialized vector one by one.


Do these help?

-Albatross
Last edited on
Yes it really helped :D

I have reposted my code on top. Can you check it and make some of the necessary corrections. It compiles but does not run properly.
SOMEONE please look at my code. Can someone see anything that needs to be fixed?
Could you be more specific? What does it do that you don't what it to do? I'm not really interested in trying to figure out what the problem is in addition to figuring out how to solve it.
Well i just want someone to check my push_back and pop_back statements. Can you show me how to do those?
Topic archived. No new replies allowed.