How to combine two arrays into array of pairs?

I have two TArray objects containing FStrings. They’re both the same length. I want to use some method to combine the two arrays into an array of pairs/tuples. This is similar to how the zip function works in Python or boost.

I tried doing it with std::transform, but I’m struggling to understand the correct usage of this function. I know there’s also a Algo::Transform in UE C++ API that’s suppose to do similar thing.

Here’s my failed attempt:

	TArray<FString> LeftImages;
	TArray<FString> RightImages;

	// Combine the left and right image paths into pairs
	TArray<TTuple<FString, FString>> ImagePathPairs;
	std::transform(LeftImages.begin(), LeftImages.end(), RightImages.begin(),
		std::back_inserter(ImagePathPairs),
		[](const FString& aa, const FString& bb)
		{
			return TTuple<FString, FString>(aa, bb);
		});

Hi mwojt_platige,

You can’t mix standard template iterators with UE iterators - you’ll need to use a for loop and build the tuple’d array yourself:

easiest to just do a loop probably. Transform looks like it applies the given function that is the third argument, to the given input that is the first argument, and outputs it as the out parameter second argument.

here is an example from the tests in engine

			TArray<FString> Strings = {
				TEXT("Hello"),
				TEXT("this"),
				TEXT("is"),
				TEXT("a"),
				TEXT("projection"),
				TEXT("test")
			};

			TArray<int32> Lengths;
			Algo::Transform(Strings, Lengths, &FString::Len);

so that doesn’t look like it would do what you want

Yes, incompatibility of iterators is a big inconvenience. Especially in the context of the growing popularity of std::ranges

Since std::begin also works for raw c-arrays, you can take advantage of that.
(but that will only work for contiguous-memory containers like TArray)

This should look like: (I haven’t tested it)

TArray<TPair<FString,FString>> ImagePathPairs;
ImagePathPairs.SetNum(LeftImages.Num());
auto ResultsIt = std::begin(&ImagePathPairs[0]);
auto LeftBegin = std::cbegin(&LeftImages[0]);
auto LeftEnd = LeftBegin + LeftImages.Num();
auto RightBegin = std::cbegin(&RightImages[0]);
std::transform(LeftBegin, LeftEnd, RightBegin, ResultsIt, [](FString const& aa, FString const& bb) {
  return TTuple<FString, FString>(aa, bb);
});