vector::iterator

久々にC++さわったらだいぶわすれてたからリハビリからはじめる。

#include <iostream>
#include <vector>
#include <string>

using namespace std;

void print_vector (string name, vector<int> &v)
{
    cout << "vector:\t" << name << endl;
    int i = 0;
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++, i++) {
        cout << "\t" << i << ":\t" << *it << endl;
    }
}

int main()
{

    vector<int> hoge, fuga;
    hoge.push_back(1);
    hoge.push_back(4);
    hoge.push_back(3);
    hoge.push_back(8);

    fuga.push_back(2);
    fuga.push_back(3);
    fuga.push_back(9);

    cout << hoge.size() << endl;

    print_vector("hoge", hoge);
    print_vector("fuga", fuga);

    hoge.swap(fuga);

    print_vector("hoge", hoge);
    print_vector("fuga", fuga);
    return 0;
}
4
vector: hoge
        0:      1
        1:      4
        2:      3
        3:      8
vector: fuga
        0:      2
        1:      3
        2:      9
vector: hoge
        0:      2
        1:      3
        2:      9
vector: fuga
        0:      1
        1:      4
        2:      3
        3:      8


iterator とりあえず自分で作ってみるのがいいのかな。