#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
class sample
{
public:
sample()
{
cout << "consructor called" << endl;
}
void test()
{
cout << "Test function" << endl;
}
};
int main()
{
vector<sample> v;
sample s;
v.push_back(s);
try
{
v.at(1).test(); // throws out of range exception.
v[1000].test(); // prints test function
}
catch (const out_of_range& oor)
{
std::cerr << "Out of Range error: " << oor.what() << '\n';
}
return 0;
}
Why v[1000].test();
printing test function on the screen. I have added only one object in the vector. I agree I can access v[1000]
becuase its sequential. But why it is giving the exactly correct result? Thanks in advance.