简体   繁体   English

如何连接两个 TStringDynArray?

[英]How to concatenate two TStringDynArray?

Is there a way to concatenate the content of one TStringDynArray to another TStringDynArray ?有没有办法将一个TStringDynArray的内容连接到另一个TStringDynArray

//First TStringDynArray
Item1
Item2
Item3

//Second TStringDynArray
ItemA
ItemB
ItemC

//Result after concatenate
Item1
Item2
Item3
ItemA
ItemB
ItemC

Not directly, asSystem::DynamicArray does not provide concatenation features in C++.不是直接的,因为System::DynamicArray在 C++ 中不提供串联功能。 So you would need to create a 3rd TStringDynArray , size it to the sum of the sizes of the 2 arrays, and individually copy each String from the 2 arrays into the 3rd array ( String is reference-counted, so you can't just copy bytes with memcpy() or equivalent, like you can with arrays of trivial types), eg:因此,您需要创建第 3 个TStringDynArray ,将其调整为 2 个 arrays 的大小之和,然后将 2 个 arrays 中的每个String单独复制到第 3 个数组中( String是引用计数的,因此您不能只复制使用memcpy()或等效的字节,就像你可以使用普通类型的 arrays 一样),例如:

TStringDynArray Arr1;
// fill Arr1 as needed...

TStringDynArray Arr2;
// fill Arr2 as needed...

TStringDynArray MergedArr;
MergedArr.Length = Arr1.Length + Arr2.Length;
int idx = 0;

for(int i = 0; i < Arr1.Length; ++i) {
    MergedArr[idx++] = Arr1[i];
}

for(int i = 0; i < Arr2.Length; ++i) {
    MergedArr[idx++] = Arr2[i];
}

// use MergedArr as needed...

Alternatively (C++Builder 10.1 Berlin and later):或者(C++Builder 10.1 Berlin 及更高版本):

#include <algorithm>

TStringDynArray Arr1;
// fill Arr1 as needed...

TStringDynArray Arr2;
// fill Arr2 as needed...

TStringDynArray MergedArr;
MergedArr.Length = Arr1.Length + Arr2.Length;
std::copy(Arr1.begin(), Arr1.end(), MergedArr.begin());
std::copy(Arr2.begin(), Arr2.end(), MergedArr.begin() + Arr1.Length);

// use MergedArr as needed...

In the Clang-based compilers , you can take this a step further by using some variadic template functions to help you concatenate as many input TStringDynArray arrays as you want, eg:基于 Clang 的编译器中,您可以更进一步,使用一些可变参数模板函数来帮助您根据需要连接尽可能多的输入TStringDynArray arrays,例如:

#include <algorithm>

int ArrLength()
{
    return 0;
}

template<typename... Ts>
int ArrLength(TStringDynArray &arr, Ts&... others)
{
    return arr.Length + ArrLength(others...);
}

void ArrCopy(TStringDynArray::iterator dst)
{
}

template<typename... Ts>
void ArrCopy(TStringDynArray::iterator dst, TStringDynArray &arr, Ts&... others)
{
    std::copy(arr.begin(), arr.end(), dst);
    ArrCopy(dst + arr.Length, others...);
}

template<typename... Ts>
TStringDynArray ArrMerge(TStringDynArray &arr, Ts&... others)
{
    TStringDynArray res;
    res.Length = ArrLength(arr, others...);
    ArrCopy(res.begin(), arr, others...);
    return res;
}

...

TStringDynArray Arr1;
// fill Arr1 as needed...

TStringDynArray Arr2;
// fill Arr2 as needed...

TStringDynArray MergedArr = ArrMerge(Arr1, Arr2/*, Arr3, Arr4, ...*/);
// use MergedArr as needed...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM