以下代码有效。我的问题是,应该 2) 不会导致非常接近 1) 的结果吗?为什么 2) 投到这么小的数量?因此,也许值得注意的是 2) 正好是 1) 的一半:
std::cout << "1) " << std::pow(2, 8 * sizeof(size_t)) << std::endl;
std::cout << "2) " << static_cast<size_t>(std::pow(2, 8 * sizeof(size_t))) << std::endl;
Run Code Online (Sandbox Code Playgroud)
输出是:
// The following code works fine, throwing a std::out_of_range exception:
std::vector<double> vd{ 1.5 };
try {
int i{ -1 };
double d = vd.at(i); // exception is thrown
}
catch (std::out_of_range& re) {
std::cout << "Exception is " << re.what() << std::endl; // invalid vector subscript
}
Run Code Online (Sandbox Code Playgroud)
如果我使用无效索引访问 for 循环中的向量元素,std::exception尽管我使用.at(). 为什么std::out_of_range不抛出异常?
// in a for loop, this does not throw the exception!
std::vector<double> vd{ 1.5 };
try {
for (int i = -1; i …Run Code Online (Sandbox Code Playgroud)