#include "stdafx.h"
#include <iostream>
#include <vector>
usingnamespace std;
// Cubed integers
class cubes
{
int cube; // The vector position cubed
public:
// Constructors
cubes () {cube = -1;}
cubes (int c) {cube = c * c * c;}
//
int get_cube () {return cube;}
};
// Overloaded operators
booloperator< (cubes a, cubes b) {return a.get_cube() < b.get_cube();}
booloperator==(cubes a, cubes b) {return a.get_cube() == b.get_cube();}
void show_vector (vector<cubes> &v);
// Show the vector specifications and contents
int main()
{
vector<cubes> v; // A vector
vector<cubes>::iterator p_v; // An iterator
vector<cubes>::reverse_iterator rp_v; // A reverse iterator
unsignedint i; // Counter variable
// Print the heading
print_heading();
// Show the specs and contents of the starting vector
cout << "\n\nHere is the starting empty cubes vector:";
show_vector(v);
// Add 6 cubes and show the vector
cout << "\n\nAdding the first 6 cubes to the vector:";
for(i = 0; i < 6; i++)
v.push_back(cubes(i));
show_vector(v);
// Delete even cubes and show the vector
cout << "\n\nDeleting the even cubes from the vector:";
p_v = v.begin();
for(i = 0; i < (int) v.size(); i++)
{
v.erase(p_v + i);
p_v++;
}
show_vector(v);
// Reinsert the even cubes and show the vector
cout << "\n\nInserting the even cubes back into the vector:";
p_v = v.begin();
for(i = 0; i < (int) v.size(); i += 2)
v.insert(p_v + i, v[i].get_cube());
show_vector(v);
// Add 4 cubes and show the vector
cout << "\n\nAdding the next 4 cubes to the end of the vector:";
p_v = v.end();
for(i = 5; i <= 8; i++)
{
v.insert(p_v, v[i].get_cube());
p_v++;
}
show_vector(v);
// Add 4 empty cubes and show the vector
cout << "\n\nAdding 4 empty cubes to the end of the vector:";
for(i = 9; i <= 12; i++)
v.push_back(cubes());
show_vector(v);
// Remove the empty cubes and show the vector
cout << "\n\nRemoving the last 4 empty cubes from the vector:";
for(i = 1; i <= 4; i++)
v.pop_back();
show_vector(v);
// Show the vector contents in forward and reverse order
cout << "\n\nPrinting the cubes with an iterator, then with a "
<< "reverse iterator:";
for(p_v = v.begin(); p_v <= v.end(); p_v++)
cout << "\n " << p_v->get_cube() << " ";
for(rp_v = v.rbegin(); rp_v <= v.rend(); rp_v++)
cout << "\n " << rp_v->get_cube() << " ";
// Clear the vector
cout << "\n\nRemoving all cubes from the vector:";
v.clear();
show_vector(v);
// Print a goodbye message and terminate the program
cout << "\n\nThanks for demonstrating a vector container today!";
cout << "\n\n\n\n\n\n";
return 0;
}
void show_vector(vector<cubes> &v)
{
unsignedint i, // Counter variable
sum = 0; // The sum of the vector contents
for(i = 0; i <= (int) v.size(); i++)
sum += v[i].get_cube();
cout << " Size: " << (int) v.size()
<< " - Sum: " << sum
<< " - Capacity: " << v.capacity()
<< " - Max Size: " << v.max_size();
cout << " Cubes Vector contents: ";
for(i = 0; i <= (int) v.size(); i++)
cout << v[i].get_cube();
return;
}
I switched out all <= with < in my for loops, and now the program runs until it reaches "Deleting the even cubes from the vector". Then I get an assertion failure: Expression: ("this->_Has_container()",0)
Now that you switched out the <= for < in your loops, that bit should work properly. In fact, the whole code will run without errors.
Now, just work on your formatting.
Here is the starting empty cubes vector: Size: 0 - Sum: 0 - Capacity: 0 - Max Size: 4611686018427387903 Cubes Vector contents:
Adding the first 6 cubes to the vector: Size: 6 - Sum: 225 - Capacity: 8 - Max Size: 4611686018427387903 Cubes Vector contents: 0182764125
Deleting the even cubes from the vector: Size: 3 - Sum: 73 - Capacity: 8 - Max Size: 4611686018427387903 Cubes Vector contents: 1864
Inserting the even cubes back into the vector: Size: 6 - Sum: 262730 - Capacity: 8 - Max Size: 4611686018427387903 Cubes Vector contents: 11512826214464
Adding the next 4 cubes to the end of the vector: Size: 9 - Sum: 524874 - Capacity: 16 - Max Size: 4611686018427387903 Cubes Vector contents: 1151282621446426214400
Adding 4 empty cubes to the end of the vector: Size: 12 - Sum: 524871 - Capacity: 16 - Max Size: 4611686018427387903 Cubes Vector contents: 1151282621446426214400-1-1-1
Removing the last 4 empty cubes from the vector: Size: 9 - Sum: 524874 - Capacity: 16 - Max Size: 4611686018427387903 Cubes Vector contents: 1151282621446426214400
Printing the cubes with an iterator, then with a reverse iterator:
1
1
512
8
262144
64
262144
0
0
0
0
262144
64
262144
8
512
1
1
Removing all cubes from the vector: Size: 0 - Sum: 0 - Capacity: 16 - Max Size: 4611686018427387903 Cubes Vector contents:
Thanks for demonstrating a vector container today!
Ok, did you mean the <= in only certain for loops? Because I changed them all, and I'm getting the assertion failure mentioned in my reply to firedraco.
I think you're right. I tried it in Codeblocks and removed "stdafx.h", and it worked fine. I have to figure out why Visual won't work with it, though, because I'm required to program with Visual for this assignment. Anyway, thank you very much for your help.