question
完整代码:
#include <iostream>
#include <vector>
template <typename T>
std::vector<T> MergeArray(std::vector<T> a, std::vector<T> b)
{
int n = a.size(), m = b.size(), i = 0, j = 0;
std::vector<T> result;
while (i < n && j < m)
{
if (a[i] < b[j])
{
result.emplace_back(a[i]);
i++;
}
else
{
result.emplace_back(b[j]);
j++;
}
}
if (i == n)
{
for (; j < m; ++j)
{
result.emplace_back(b[j]);
}
}
if (j == m)
{
for (; i < n; ++i)
{
result.emplace_back(a[i]);
}
}
return result;
}
int main()
{
int n, m, temp;
std::vector<int> a, b;
std::cin >> n;
for (auto i = 0; i < n; ++i)
{
std::cin >> temp;
a.emplace_back(temp);
}
std::cin >> m;
for (auto i = 0; i < m; ++i)
{
std::cin >> temp;
b.emplace_back(temp);
}
std::cout << "--------------------------------------" << std::endl;
std::vector<int> result;
result = MergeArray(a, b);
for (auto i = 0; i < m + n; ++i)
{
std::cout << result[i] << std::endl;
}
system("pause");
return 0;
}