while
C では、ループが実行された回数をどのように数えますか?
while
Python では、最初に空のリストを作成し、ループが実行されるたびにループから値を追加します。while
次に、そのループが実行された回数を知るために、そのリストの長さを見つけます。Cに同様の方法はありますか?
while
C では、ループが実行された回数をどのように数えますか?
while
Python では、最初に空のリストを作成し、ループが実行されるたびにループから値を追加します。while
次に、そのループが実行された回数を知るために、そのリストの長さを見つけます。Cに同様の方法はありますか?
変数を 0 に初期化し、反復ごとにインクリメントしますか?
int num = 0;
while (something) {
num++;
...
}
printf("number of iterations: %d\n", num);
(申し訳ありませんが、これはCではなくC ++の方法です...)本当に入力リストに移動したい場合は、次のように実行できます。
#include <list>
#include <iostream>
using namespace std;
...
list<int> my_list;
int num = 0;
while( ... ) {
...
++num;
my_list.push_back(num);
}
cout << "List size: " << my_list.size() << endl;
リスト値を印刷する場合:
#include <list>
#include <iostream>
#include <algorithm>
using namespace std;
...
list<int> my_list;
int num = 0;
while( ... ) {
...
++num;
my_list.push_back(num);
}
cout << "List contens: " << endl;
// this line actually copies the list contents to the standard output
copy( my_list.begin(), my_list.end(), iostream_iterator<int>(cout, ",") );
開始i = 0
し、i++
すべてのループパスで...