Assertion Failure Using Vector

It compiles and runs, but after "Here is the starting empty cubes vector" prints, I get this message:

Debug Assertion Failed!

Expression: vector subscript out of range


Can someone help me, please?


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
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace 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
bool operator< (cubes a, cubes b) {return a.get_cube() <  b.get_cube();}
bool operator==(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
   unsigned int 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)
{
   unsigned int 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;
}
Why are you casting v.size() to an int? It's already unsigned and doesn't need casting.

Anyway, your issue is you are using <= in the for loops. You need <.
I get EXC_BAD_ACCESS upon getting cube, which is at least what gdb says...

EDIT: And it's caused by the fact that your vector is empty upon startup, and even if it had something, cube wasn't initialized to anything.

-Albatross
Last edited on
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)
Albatross, I don't quite understand... The vector is supposed to start empty.
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!


-Albatross

EDIT: (2^3) * 5 posts, and counting.
Last edited on
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 changed all of them, and it works just fine. Though, I did omit your header and your printheader() function.

EDIT: Are you 100% sure you changed all of them? Did you use the find function your IDE or text editor ought to have?

-Albatross
Last edited on
Yes, I'm using Visual C++ 2008, and its Find function found no <=. Wow, this is frustrating. I

blocked out the print_heading function and call, and still have the same problem, so that's not

the issue.
Well, then you have an insane compiler, or else Microsoft was tampering with your C++ standard library. I'm using GCC 4.2.1, and it works just fine...

-Albatross
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.
Last edited on
Topic archived. No new replies allowed.