[英]two vector<string> concatenation
I have two vectors , like this: 我有两个向量,像这样:
A ={"Sam", "Jordan", "Mike"}
B ={"Smith", "Lancaster", "Horgen"}
After Concatenation, They should look like this: 串联后,它们应如下所示:
A ={"SamSmith", "JordanLancaster", "MikeHorgen"}
Basically, you combine first and last names. 基本上,您将名字和姓氏结合在一起。 How do I do that?
我怎么做?
Use std::transform
( live example ): 使用
std::transform
( 实时示例 ):
std::transform(
begin(A), end(A), begin(B), begin(A),
[](const auto& s1, const auto& s2) { return s1 + s2; }
);
for (size_t i = 0; i < A.size() && i < B.size(); ++i)
A[i] += B[i];
Now the A
vector contains the required output. 现在,
A
向量包含所需的输出。
It could done easily by iterating over two vectors and concatenating
strings in each step and assigning result back into A
, 通过迭代两个向量并在每个步骤中
concatenating
字符串并将结果分配回A
,可以轻松完成此操作。
typedef vector<string>::iterator VIter;
for(VIter it1=A.begin(), it2=B.begin(); it1 != A.end(), it2 != B.end(); ++it1, ++it2)
{
*it1 = *it1 +*it2;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.