Last active
August 26, 2018 08:03
-
-
Save hiramekun/32233f1b60c4dcfcef7779e43ae8ac9d to your computer and use it in GitHub Desktop.
c++のvectorの操作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
#include <map> | |
#include <unordered_map> | |
#include <cmath> | |
#include <numeric> | |
using namespace std; | |
int main() { | |
vector<int> v = { 3, 5, 2, 4, 1 }; | |
printf("begin address: %d\n", v.begin()); | |
printf("max element address: %d\n", max_element(v.begin(), v.end())); | |
printf("max element: %d\n", *max_element(v.begin(), v.end())); | |
printf("max element idx: %d\n", max_element(v.begin(), v.end()) - v.begin()); | |
// 配列の和を計算. | |
printf("sum all: %d\n", accumulate(v.begin(), v.end(), 0)); | |
// 配列の掛け算をしたものを計算. | |
printf("multiple all: %d\n", accumulate(v.begin(), v.end(), 1, multiplies<int>())); | |
// 部分和を求める. | |
vector<int> w(5); // 先に要素数分のメモリを確保. | |
partial_sum(v.begin(), v.end(), w.begin()); | |
for (int i = 0; i < w.size(); i++) { | |
printf("to index %d: %d\n", i, w[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実行結果