Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 检查>=0终止条件_C++_Loops_For Loop_Size T - Fatal编程技术网

C++ 检查>=0终止条件

C++ 检查>=0终止条件,c++,loops,for-loop,size-t,C++,Loops,For Loop,Size T,我需要向后循环一个字符串 // std::string str assumed to be defined at this point for (std::size_t i = str.length() - 1; i >= 0; i--) { // perform some check on str[i] } 问题描述 现在,如果我使用一个inti循环索引,这会起作用,因为我最终会变成-1,循环终止。当对运行索引使用std::size\u t i(无符号)时,它将在“低于”零时变得非

我需要向后循环一个字符串

// std::string str assumed to be defined at this point
for (std::size_t i = str.length() - 1; i >= 0; i--) {
  // perform some check on str[i]
}
问题描述
现在,如果我使用一个
inti
循环索引,这会起作用,因为我最终会变成-1,循环终止。当对运行索引使用
std::size\u t i
(无符号)时,它将在“低于”零时变得非常大,因此循环不会终止,最终将导致分段错误。如果我想使用std::size\u t作为循环索引类型,那么解决这个问题的首选方法是什么,因为std::string::length返回的是std::size\u t,而不是int

可能的解决方案

for (std::size_t i = str.length(); i > 0; i--) {
  // perform some check on str[i - 1]
}

我认为这真的很难看,因为我们使用I作为非直观的“偏移”idx。什么是干净的解决方案?

如果循环中不需要
i
,可以使用反向迭代器:

int main()
{
    std::string s = "Hello, World!";
    for (std::string::reverse_iterator i = s.rbegin(); i != s.rend(); ++i)
        std::cout << *i;
}
intmain()
{
std::string s=“你好,世界!”;
for(std::string::reverse_迭代器i=s.rbegin();i!=s.rend();++i)

std::cout带有索引的更好的循环如下

for ( std::size_t i = str.length(); i != 0; i--) {
  // perform some check on str[i-1]
  //                       ^^^^^^^^
}

也不是宣言,

std::size_t i = str.length();
你可以直接写信

auto i = str.length();

你需要知道索引吗?否则你可以使用反向迭代器。这能回答你的问题吗?
std::size_t
是一个无符号数。无符号数总是
=0
@AlgirdasPreidžius OP知道它们在问题中逐字说明。@Borgleader OP说“当“低于”零时,它将变得非常大,因此循环不会终止,最终将导致分段错误。”这并不能清楚地表明,他理解这种情况(
=0
)由于
无符号的性质,将永远是正确的。他只是写了你这样做时发生的情况的观察结果。我争辩说,首先不需要做任何测试。@马特忽略向下的投票。我显示了正确的循环。我删除了我的向下的投票,但在我的辩护中,你最初的答案只有存在的第一个循环按op计算的nt(!=和>在本例中是功能等效的afaict)并且被认为是丑陋的,所以这个答案的原始版本不是答案,因为OP在寻找其他的东西。@Borgleader你错了。看起来你不理解这个问题。我显示的第一个循环与问题中的循环不同。第一个循环与(std::size\u t i=str.length()的
有什么不同;i>0;i--){
(>vs!=aside ofc)@Borgleader首先,您展示了与问题中不同的循环。其次,我展示了如何使用循环中的索引访问字符串的元素。如果我不是从and(s.rbegin())而是从某个位置开始向后迭代,此解决方案是否也适用(由索引给出)在字符串中?否则非常好,谢谢。是的,您可以执行例如
std::string::reverse_iterator i=s.rbegin()+5
,使其不从末尾开始,而是在那之前执行
5
字符。
auto i = str.length();