Play Siv3D!

ゲームとメディアアートのための C++ ライブラリ「Siv3D」

動的配列

動的配列

Siv3D June 2015 v2

動的配列を作成する

# include <Siv3D.hpp>

void Main()
{
	Array<int> v1 = { 10, 20, 30, 40 };

	Array<int> v2(10, 3);

	Array<String> v3 = { L"apple", L"banana" };

	Println(v1);

	Println(v2);

	Println(v3);

	WaitKey();
}


要素の個数を調べる

# include <Siv3D.hpp>

void Main()
{
	Array<int> v1 = { 10, 20, 30, 40 };

	Array<int> v2(10, 3);

	Array<String> v3 = { L"apple", L"banana" };

	Println(v1.size());

	Println(v2.size());

	Println(v3.size());

	WaitKey();
}


要素にアクセスする

# include <Siv3D.hpp>

void Main()
{
	Array<int> v1 = { 10, 20, 30, 40 };

	Array<int> v2(10, 3);

	Array<String> v3 = { L"apple", L"banana" };

	Println(v1[0]);

	Println(v2[5]);

	Println(v3[1]);

	Println(v1.back());

	Println(v3.front());

	WaitKey();
}


配列が空かどうか調べる

# include <Siv3D.hpp>

void Main()
{
	Array<int> v1 = { 10, 20, 30, 40 };

	Array<int> v2(10, 3);

	Array<String> v3 = { L"apple", L"banana" };

	Array<double> v4;

	Println(v1.empty());

	Println(v2.empty());

	Println(v3.empty());

	Println(v4.empty());

	WaitKey();
}


要素を追加する

# include <Siv3D.hpp>

void Main()
{
	Array<int> v1 = { 10, 20, 30, 40 };

	Println(v1);

	v1.push_back(50);

	v1.push_back(60);

	Println(v1);


	Array<String> v2;

	Println(v2);

	v2.push_back(L"apple");

	v2.push_back(L"banana");

	Println(v2);


	Array<Point> v3;

	Println(v3);

	v3.push_back(Point(3, 5));

	v3.push_back(Point(5, 7));

	Println(v3);

	WaitKey();
}


要素を追加する (Emplacement)

# include <Siv3D.hpp>

void Main()
{
	Array<String> v2;

	Println(v2);

	v2.emplace_back(L"apple");

	v2.emplace_back(L"banana");

	Println(v2);


	Array<Point> v3;

	Println(v3);

	v3.emplace_back(3, 5);

	v3.emplace_back(5, 7);

	Println(v3);

	WaitKey();
}


末尾の要素を削除する

# include <Siv3D.hpp>

void Main()
{
	Array<int> v1 = { 10, 20, 30, 40 };

	Println(v1);

	v1.pop_back();

	Println(v1);

	WaitKey();
}


要素をすべて削除する

# include <Siv3D.hpp>

void Main()
{
	Array<int> v1 = { 10, 20, 30, 40 };

	Println(v1);

	v1.clear();

	Println(v1);

	WaitKey();
}


指定した位置の要素を削除する

# include <Siv3D.hpp>

void Main()
{
	Array<int> v1 = { 10, 20, 30, 40 };

	Println(v1);

	v1.erase(v1.begin() + 1);

	Println(v1);

	WaitKey();
}


条件を満たす要素を削除する

# include <Siv3D.hpp>

void Main()
{
	Array<int> v1 = { 10, 20, 30, 40 };

	Println(v1);

	Erase_if(v1, [](int n){ return n < 30; });

	Println(v1);

	WaitKey();
}