TL;DR
#include <vector>
std::vector <unsigned long long int> triangleNumbers(0);
int main()
{
triangleNumbers[0]=10000000000;
}
You have an empty vector, and the very first line in main leads to undefined behavior since you’re trying to access item 0 (there is no such item).
Live Example using operator [ ]
Live Example showing what vector::at() does
Note the second link demonstrates that you are accessing an element out of bounds by using at() instead of [ ] to access the first item.
To add items to a std::vector, use one of the methods designed to do this, namely vector::push_back(), vector::insert(), vector::emplace_back(), vector::resize(), or construct the std::vector with the requisite number of entries.
The easiest out of all of these options would be to construct the vector using the initializer list:
std::vector<unsigned long long int> triangleNumbers =
{10000000000, 10000000002, 10000000005, 10000000009, 10000000014};
Once you properly set up the vector, then you need to look at the rest of your code, to see where you may be accessing an out-of-bounds index. Especially take a look at j in your loop, and how you’re using it as an index. The vector::at() function will tell you immediately if j goes out of bounds.
EDIT: If you really wanted a syntax that would simulate an “auto-expanded” array, the closest you can come to this would be to use a std::unordered_map<int, unsigned long long> as seen by this example.
Possibly the map solution would be a drop-in replacement — you would have to test it.
3
solved Why does this program which finds the smallest triangle number with >500 factors crash? [closed]