测试超出范围

时间:2013-10-10 01:23:49

标签: c++

我正在尝试测试访问字符串中的元素是否超出范围。我知道如何捕获(std :: out_of_range)但是我想要做的是if(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中编程

2 个答案:

答案 0 :(得分:1)

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

if(test.size()< 9)

答案 1 :(得分:1)

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