Get Array size to update after using a pop back function?

I have marked the area of interest in long asterisk marks.
The program executes properly, but it fails to adjust/update the size of the array, requiring me to do it manually.


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
113
114
115
116
// This program demonstrates the SimpleVector template . 
#include <iostream> 
#include "SimpleVector.h" 

#include <vector>
using namespace std; 

int main() 
{
	const int SIZE = 10;		// Number of elements 
	int count;					// Loop counter 

	//for push_back
	int numInt;
	double numDbl;

	// Create a SimpleVector of ints. 
	SimpleVector<int> intTable(SIZE);		//Xalls simple vector class..

	// Create a SimpleVector of doubles . 
	SimpleVector<double> doubleTable(SIZE); 
	
	// Store values in the two SimpleVectors. 
	for (count = 0; count < SIZE; count++) 
	{ 
		intTable[count] = (count * 2); 
		doubleTable[count] = (count * 2.14); 
	} 

	// Display the values in the SimpleVectors . 
	cout << "These values are in int Table:\n"; 
	for (count = 0; count < SIZE; count++) 
		{cout << intTable[count] << " "; }
	cout << endl; 
	
	cout << "These values are in doubleTable:\n"; 
	for (count = 0; count < SIZE; count++) 
		cout << doubleTable[count] << " "; 
	cout << endl << endl; 
	
	
	// Use the standard + operator on the elements. 
	cout << "\nAdding 5 to each element of intTable"  
		<< " and doubleTable.\n"; 
	for (count = 0; count < SIZE; count++) 				//increases existing elements by 5 units
	{ 
		intTable[count] = intTable[count] + 5;
		doubleTable[count] = doubleTable[count] + 5.0;
	}

	// Display the values in the SimpleVectors. 
	cout << "These values are in intTable:\n"; 
	for (count = 0; count < SIZE; count++) 
		cout << intTable[count] << " "; 
	cout << endl; 

	cout << "These values are in doubleTable: \n"; 
	for (count = 0; count < SIZE; count++) 
		cout << doubleTable[count] << " "; 
	cout << endl << endl; 

	
	// Use the standard ++ operator on the elements. 
	cout << "\nIncrementing each element of intTable and"	//increases each elements's value by 1
		<< " doubleTable.\n"; 

	for (count = 0; count < SIZE; count++) 
	{ 
		intTable[count] ++; 
		doubleTable[count]++; 
	}

	// Display the values in the SimpleVectors. 
	cout << "These values are in intTable:\n" ; 
	for (count = 0; count < SIZE; count++) 
		cout << intTable[count] << " "; 
	cout << endl; 

	cout << "These values are in doubleTable:\n"; 
	for (count = 0; count < SIZE; count++) 
		cout << doubleTable[count] << " "; 
	cout << endl << endl; 

	cout << "Enter a number to insert to the int SimpleVector: ";
	cin >> numInt;
	intTable.push_back(numInt);
	// Display the values in the SimpleVectors. 
	cout << "These values are in intTable:\n" ; 
	for (count = 0; count < SIZE + 1; count++)	//had to change SIZE to, SIZE + 1
		cout << intTable[count] << " "; 
	cout << endl; 

	//*****************************************************************************
	//is there an alternative to doing 'SIZE + 1' in the for loop?
	cout << "Enter a number to insert to the double SimpleVector: ";
	cin >> numDbl;
	doubleTable.push_back(numDbl);
	for (count = 0; count < SIZE + 1; count++)	//had to change SIZE to, SIZE + 1
		cout << doubleTable[count] << " "; 
	cout << endl; 

	//*****************************************************************************
	//After using the following pop_back function call(s), the subError, member function, is called

	cout << "Pop an element from the int array..." << endl;
	intTable.pop_back();
	//intTable.pop_back();
	//intTable.pop_back();
	cout << "These values are in intTable:\n" ; 
	for (count = 0; count < SIZE; count++)	//had to change SIZE to, SIZE + 1
		cout << intTable[count] << " "; 
	cout << endl; 

	system("pause");
	return 0;
}


Header File
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#ifndef SIMPLEVECTOR_H 
#define SIMPLEVECTOR_H 
#include <iostream>		// Needed for bad_alloe exception 
#include <new>			// Needed for the exit function 
#include <cstdlib> 

#include <vector>
using namespace std; 

template <class T> 
class SimpleVector 
{ 
private: 
	T *aptr;				// To point to the allocated array
	
	//*****************************************************************
	// arraySize member variable is declared as an int. This is because
	// it holds the size of the array, which will be an integer value, 
	// regardless of the data type of the array. This is also why 
	// the size member function returns an int.
	//*****************************************************************
	int	 arraySize;			// Number of elements in the array

	void memError();		// Handles memory allocation errors 
	void subError();		// Handles subscripts out of range

	//T push_back(T num);

public: 
	// Default constructor 
	SimpleVector() 
		{	aptr = 0; arraySize = 0;	} 

	// Constructor declaration 
	SimpleVector(int);

	// Copy constructor declaration 
	SimpleVector(const SimpleVector &); 

	// Destructor declaration 
	~SimpleVector(); 


	// Accessor to return the array size 
	int size() const 
		{	return arraySize;	} 

	// Accessor to return a specific element 
	T getElementAt(int position); 

	void push_back(const T &);
	void pop_back ();

	// Overloaded [] operator declaration 
	T &operator[](const int &); 
}; 
 

//************************************************************
// Constructor for SimpleVector class. Sets the size of the  
// array and allocates memory for it. 
//************************************************************ 
template <class T> 
SimpleVector<T>::SimpleVector(int s) 
{ 

	arraySize = s; 

	if (arraySize == 0)	//added for PC 16_08
	{
		aptr = NULL;
		return;
	}

	// Allocote memory for the array. 
	try 
	{ 
		aptr = new T [s]; 
	}
	catch (bad_alloc) 
	{ 
		memError(); 
	} 
	
	// Initialize the array . 
	for (int count = 0; count < arraySize; count++) 
		*(aptr + count) = 0;
}

//*********************************************
// Copy Constructor for SimpleVector class. 
//********************************************* 
template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector &obj) 
{ 
	// Copy the array size. 
	arraySize = obj.arraySize; 

	// Allocate memory for the array. 
	aptr = new T[arraySize]; 
	if (aptr == 0) 
		memError(); 

	// Copy the elements of obj's array. 
	for(int count = 0; count < arraySize; count++) 
		*(aptr + count) = *(obj.aptr + count); 
}

//***************************************
// DestrUCtor for SimpleVector class. 
//***************************************
template <class T> 
SimpleVector<T>::~SimpleVector() 
{ 
	if (arraySize > 0) 
	delete [] aptr; 
} 
//end constructors/destructors***************************************************************


//private members*****************************************************************************

template <class T> 
void SimpleVector<T>::memError() 
{ 
	cout << "ERROR:Cannot allocate memory.\n"; 
	system("pause");
	exit(EXIT_FAILURE); 
} 

//************************************************************
// subError function. Displays an error message and 
// terminates the program when a subscript is out of range.
//************************************************************
template <class T> 
void SimpleVector<T>::subError() 
{  
	cout << "ERROR: Subscript out of range.\n";
	system("pause");
	exit(EXIT_FAILURE); 
}

//end private members*************************************************************************

//public members
template <class T> 
void SimpleVector<T>::push_back(const T &num) 
{ 
	T *temp = new T[arraySize + 1];
		
	for(int index = 0; index < arraySize; index++)	// if arraySize == 0, this will not execute.
	{
		temp[index] = aptr[index];
	}
		
	temp[arraySize++] = num;

	//delete [] aptr;
	aptr = temp;
} 

template <class T>
void SimpleVector<T>::pop_back()
{
	T *temp = new T[arraySize];

	for (int index = 0; index < arraySize; index++)
	{
		temp[index] = aptr[index];
	}

	temp[arraySize--];
	//delete [] aptr;

	aptr = temp;
}

//*****************************************************
// getElementAt function. The argument is a subscript. 
// This function returns the value stored at the sub-
// cript in the array.
//*****************************************************
template <class T> 
T SimpleVector<T>::getElementAt(int sub) 
{
	if (sub < 0 || sub >= arraySize) 
		subError(); 
	return aptr[sub]; 
} 

//********************************************************* 
// Overloaded [] operator. The argument is a subscript. 
// This function returns a reference to the element  
// in the array indexed by the subscript.  
//*********************************************************
template <class T> 
T &SimpleVector<T>::operator[](const int &sub) 
{ 
	if (sub < 0 || sub >= arraySize) 
		subError(); 
	return aptr[sub]; 
}
#endif  

Use the size() member function instead of SIZE in the loops.
SIZE++?
I think what Peter87 is saying is that when you insert an element into the vector using the pop_back function, then you change the size. However, you are still using the const int SIZE, which is 10, thus you are using the wrong size.

I did not look through all that code, but instead, try using the vector::size() member function, which returns the vector's size.

Example:
1
2
3
4
vector<int> myvector;
cout << myvector.size() << "\n";
myvector.pop_back(5)
cout << myvector.size() << "\n";
Last edited on
nm
Last edited on
Topic archived. No new replies allowed.