【C++】Iterator (イテレータ)

サンプル

SampleCpp.cpp

#include "stdafx.h"
#include <string>
#include <list>
#include <iostream>
#include <sstream>
using namespace std;
using namespace System;

// 構造体
typedef struct {
	double hight;
	double weight;
} Size;

typedef struct {
    long id;
    string name;
    char sex;
    int age;
    Size size;
} Person;

int main(array<System::String ^> ^args)
{
	// 文字列
	list<string> result;
	for (int i = 0; i < 10; i++)
	{
		// C++ で int 型の値を string 型にするときは stringstream を使う
		// http://d.hatena.ne.jp/itog/20090310/1236672671
		stringstream ss;
		ss << "Sample" << i;
		string value = ss.str();
		result.push_back(value);
	}
	for (list<string>::iterator it = result.begin();  it != result.end(); it++)
	{
		string output = "Output is " + *it;
		cout << output << endl;
	}

	// 構造体
	list<Person> people;
	for (int i = 0; i < 10; i++)
	{
		Person person = {i, "Tom", 'M', i * 2, 172.2, 63.2};
		people.push_back(person);
	}
	for (list<Person>::iterator it = people.begin();  it != people.end(); it++)
	{
		stringstream ss;
		ss << "Output is " << it->id << " " + it->name + " " + it->sex + " "
			<< it->age << " " << it->size.hight << " " << it->size.weight;
		string value = ss.str();
		cout << value << endl;
	}
	
	char input[100];
	scanf("%s", input);

    return 0;
}