gpt4 book ai didi

c++ - 测试超出范围

转载 作者:行者123 更新时间:2023-11-28 03:07:45 25 4
gpt4 key购买 nike

我正在尝试测试访问字符串中的元素是否超出范围。我知道如何捕捉 (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 编程

最佳答案

字符串的“范围”是它的大小。如果里面有 1 个字符,那么它的 size() 就是 1。所以你可以这样做

如果(测试大小()<9)

关于c++ - 测试超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19285567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com