0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OpenSiv3Dのremove_if でメンバ変数の条件で削除する

Last updated at Posted at 2024-06-17

下記クラスで動的配列を作ったとき、a < 5 の要素のものだけ削除する

TEST.h
class Test
{
public:
	int a, b;
	Test() :a(0), b(0)
	{}
	Test(int a, int b = 0)
	{
		this->a = a, this->b = b;
	}
};

Array<Test> tests; // ここにランダムで値を適当にいれる

tests.remove_if([&](auto a) { return a.a < 5; }); // a<5 の要素のみ削除

以下、デバッグ表示込みの実装コード

remove_if_TEST.cpp

# include <Siv3D.hpp> // Siv3D v0.6.14

class Test
{
public:
	int a, b;
	Test() :a(0), b(0)
	{}
	Test(int a, int b = 0)
	{
		this->a = a, this->b = b;
	}
};

void Main()
{
	// 背景の色を設定する | Set the background color
	Scene::SetBackground(ColorF{ 0.6, 0.8, 0.7 });

	Array<Test> tests, temp;
	for (size_t i = 0; i < 10; i++)
	{
		tests << Test{ Random(10),7 };
	}
	temp = tests;
	tests.remove_if([&](auto a) {return a.a < 5; }); //aが5未満の要素だけ削除 bは関係なし

	String tempStr = U"";
	for (auto& a : temp)
	{
		tempStr = tempStr + U"[ {},{} ]"_fmt(a.a, a.b);
	}
	String deletedStr = U"";
	for (auto& a : tests)
	{
		deletedStr = deletedStr + U"[ {},{} ]"_fmt(a.a, a.b);
	}
	s3d::Print << U"[a,b]" << tempStr;
	s3d::Print << U"  a < 5  のみ削除";
	s3d::Print << U"[a,b]" << deletedStr;


	while (System::Update())
	{
	}
}

実行結果

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?