简体   繁体   English

将for循环迭代器添加到变量

[英]Adding for-loop iterator to variable

I have an extremely basic question (python user trying to fix some old c++ code that was given to me).我有一个非常基本的问题(python 用户试图修复一些旧的 c++ 代码给我)。

I'm trying to add one to the value of a loop iterator.我正在尝试将一个添加到循环迭代器的值中。 I need to pass (iterator+1) to a function, but it doesn't work if I pass i+1 as an argument, so I've tried printing some things out.我需要将 (iterator+1) 传递给 function,但如果我将 i+1 作为参数传递它就不起作用,所以我尝试打印一些东西。

Ideally, I would like a solution that works when i is of type size_t (this is how it was originally coded).理想情况下,我想要一个适用于 i 类型为 size_t 的解决方案(这就是它最初的编码方式)。

 for (int i=0; i<npar; i++) {
    int i_plusplus = 1+i;
    cout << i_plusplus << endl;
    MNPOUT(i_plusplus,name,valg,errorg,minvg,maxvg,isVariableg);
    params(i) = val;
 }

I expect it to print 1, 2, 3... etc It actually prints 1i, 2i, 3i...我希望它打印 1, 2, 3... 等等 它实际上打印 1i, 2i, 3i...

If I just print out i, I get the value of the iterator (0, 1, 2...)如果我只是打印出 i,我会得到迭代器的值 (0, 1, 2...)

int i_plusplus += 1 if you want to increment the value of i_plusplus by 1 int i_plusplus += 1 如果要将 i_plusplus 的值增加 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM