如何连接两个 TStringDynArray?
How to concatenate two TStringDynArray?
有没有办法将一个 TStringDynArray
的内容连接到另一个 TStringDynArray
?
//First TStringDynArray
Item1
Item2
Item3
//Second TStringDynArray
ItemA
ItemB
ItemC
//Result after concatenate
Item1
Item2
Item3
ItemA
ItemB
ItemC
不直接,因为 System::DynamicArray
在 C++ 中不提供串联功能。因此,您需要创建第 3 个 TStringDynArray
,将其大小调整为 2 个数组大小的总和,然后将每个 String
从 2 个数组单独复制到第 3 个数组(String
是引用计数的,所以你不能只用 memcpy()
或等价物复制字节,就像你可以用普通类型的数组一样),例如:
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...
或者(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...
在 Clang-based compilers 中,您可以更进一步,使用一些可变参数模板函数来帮助您连接任意数量的输入 TStringDynArray
数组,例如:
#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...
有没有办法将一个 TStringDynArray
的内容连接到另一个 TStringDynArray
?
//First TStringDynArray
Item1
Item2
Item3
//Second TStringDynArray
ItemA
ItemB
ItemC
//Result after concatenate
Item1
Item2
Item3
ItemA
ItemB
ItemC
不直接,因为 System::DynamicArray
在 C++ 中不提供串联功能。因此,您需要创建第 3 个 TStringDynArray
,将其大小调整为 2 个数组大小的总和,然后将每个 String
从 2 个数组单独复制到第 3 个数组(String
是引用计数的,所以你不能只用 memcpy()
或等价物复制字节,就像你可以用普通类型的数组一样),例如:
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...
或者(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...
在 Clang-based compilers 中,您可以更进一步,使用一些可变参数模板函数来帮助您连接任意数量的输入 TStringDynArray
数组,例如:
#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...