Skip to content

Instantly share code, notes, and snippets.

@MasazI
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save MasazI/5d67ef213121fb04fd06 to your computer and use it in GitHub Desktop.

Select an option

Save MasazI/5d67ef213121fb04fd06 to your computer and use it in GitHub Desktop.
iterator_vector.cpp
//
// iterator.cpp
// CplusplusPractice
//
// Created by masai on 2015/05/22.
// Copyright (c) 2015年 masai. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
template<typename Iter>
void func(Iter begin, Iter end){
// ++itでポインタを次へ進める
for (Iter it = begin; it != end; ++it){
// イテレータの要素へのアクセスはポインタの実体を指定
cout << *it << endl;
}
}
int main(){
// 配列の定義
vector<int> int_array;
// サンプルデータを入れる
int_array.push_back(2);
int_array.push_back(4);
int_array.push_back(6);
int_array.push_back(8);
// 要素の書き出し
func(int_array.begin(), int_array.end());
// 別の型の配列を定義
vector<double> double_array;
double_array.push_back(.3);
double_array.push_back(.4);
double_array.push_back(.5);
double_array.push_back(.6);
double_array.push_back(.7);
func(double_array.begin(), double_array.end());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment