|1. Always post the code that you actually use and that shows the described (problematic) behaviour
2. Cleaning up your code can occasionally help, but if you do so, then (a) make sure the clean code still compiles (and runs) to the extent the original code did, and (b) make sure the code still shows the same issue(s)! There really is no point discussing code that doesn't show the behaviour you are describing.
3. Learn to use a debugger, step through your code, and examine the current value of your local variables. Since you already know where you're crashing, it's a good starting point to set a break point before the call to
push_back(), and investigate the state of your variables then.
Bonus tip: remove that line
using namespace std; It is a crutch and will make it more difficult to spot issues in your code. You'll need to add the prefix
std:: in many places, so that requires additional typing, but it really isn't that hard, and it adds to the readability of your code as well as reducing the likelyhood that you're mixing up local and library symbols! The extra effort of always typing
std:: pays off very quickly!
If you don't believe me, the warnings are all over the place right in the C++ reference. See for example http://en.cppreference.com/w/cpp/language/namespace#Using-directives[^]:
cppreference.com wrote:Using-directive does not add any names to the declarative region in which it appears (unlike the using-declaration), and thus does not prevent identical names from being declared.
cppreference.com wrote:The using-directive
using namespace std; at any namespace scope introduces every name from the namespace std into the global namespace (since the global namespace is the nearest namespace that contains both std and any user-declared namespace), which may lead to undesirable name collisions. This, and other using directives are generally considered bad practice at file scope of a header file.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)