测试超出范围

Test out of range

本文关键字:范围 测试      更新时间:2023-10-16

>我正在尝试测试访问字符串中的元素是否超出范围。我知道如何捕捉(std::out_of_range(,但我试图做的是如果(out_of_range(例:

string test;
int a;
test="123456789";
if(test.at(9)==out_of_range) //this isnt proper code but this is what im trying to accomplish
{
a=0;}
else
a=1;

我正在C++VS10编程

字符串的"范围"是它的大小。 如果其中有 1 个字符,则其 size(( 为 1。所以你可以做到

if(test.size(( <9(

你为什么不检查字符串的长度?