どうも、ちょげ(@chogetarou)です。
for文を使ってリスト(List)の全要素に特定の値を掛け算する方法を紹介します。
方法

for文を使ってリスト(List)の全要素に特定の値を掛け算するには、「*」を使います。
まず、for文でリストをループします。
そして、ループ処理で、リストの要素に「*」で値を掛けます。
//myList=対象のリスト, value=値
for (auto it = myList.begin(); it != myList.end(); ++it) {
*it *= value;
}
上記のfor文は、リストの全要素に特定の値を掛け算します。
使用例
#include <iostream>
#include <list>
using namespace std;
int main(void){
list<int> nums = { 11, 2, 8, 20, 7, 4 };
int value = 10;
for (auto it = nums.begin(); it != nums.end(); ++it) {
*it *= value;
}
for(int item: nums) {
cout << item << endl;
}
return 0;
}
出力:
110
20
80
200
70
40
コメント