#include <iostream> #include <vector> // Function to merge two sorted arrays std::vector<int> mergeSortedArrays(const std::vector<int>& arr1, const std::vector<int>& arr2) { std::vector<int> mergedArray; int i = 0; // Pointer for the first array int j = 0; // Pointer for the second array // Compare elements of both arrays and insert them into the new array in sorted order while (i < arr1.size() && j < arr2.size()) { if (arr1[i] <= arr2[j]) { mergedArray.push_back(arr1[i]); i++; } else { mergedArray.push_back(arr2[j]); j++; } } // Insert the remaining elements from the first array while (i < arr1.size()) { mergedArray.push_back(arr1[i]); i++; } // Insert the remaining elements from the second array while (j < arr2.size()) { mergedArray.push_back(arr2[j]); j++; } return mergedArray; } int main() { // Example usage std::vector<int> arr1 {1, 3, 5, 7}; std::vector<int> arr2 {2, 4, 6, 8}; std::vector<int> mergedArr = mergeSortedArrays(arr1, arr2); // Output the merged array for (int i = 0; i < mergedArr.size(); i++) { std::cout << mergedArr[i] << " "; } return 0; }
时间: 2024-04-17 21:23:02 浏览: 25
这是一个用于合并两个已排序数组的函数。它将两个数组作为参数传入,并返回一个合并后的已排序数组。
在函数内部,我们使用两个指针i和j分别指向第一个和第二个数组的起始位置。我们比较arr1[i]和arr2[j]的值,将较小的值插入到新的合并数组mergedArray中,并将相应的指针向后移动一位。重复这个过程直到其中一个数组被完全遍历。
最后,我们检查剩下的元素,将它们插入到合并数组中。然后返回合并数组。
在主函数中,我们创建了两个已排序数组arr1和arr2,并调用mergeSortedArrays函数将它们合并在一起。最后,我们输出合并后的数组。
相关问题
#include <iostream>#include <ctime>#include <cstdlib>#include <stack>#include <vector>
在C++中,`<iostream>`、`<ctime>`、`<cstdlib>`、`<stack>`和`<vector>`是一些标准库头文件,分别用于输入输出、时间操作、系统资源操作、栈(后进先出数据结构)和动态数组(向量)。
以下是使用这些头文件的一些简单示例:
1. 输入输出(iostream)
```cpp
#include <iostream>
int main() {
std::cout << "Hello, World!\n"; // 输出 "Hello, World!"
std::cin >> someVariable; // 从用户那里读取输入
return 0;
}
```
2. 时间操作(ctime)
```cpp
#include <ctime>
std::time_t now = std::time(0); // 获取当前时间
std::cout << "Current time: " << std::ctime(&now);
```
3. 随机数生成(cstdlib)
```cpp
#include <cstdlib>
#include <ctime>
int random_number = std::rand(); // 生成一个随机整数
```
4. 栈(stack)
```cpp
#include <stack>
std::stack<int> myStack;
myStack.push(1); // 入栈
std::cout << "Top of stack: " << myStack.top(); // 输出栈顶元素
```
5. 向量(vector)
```cpp
#include <vector>
std::vector<int> myVector;
myVector.push_back(5); // 向向量添加元素
std::cout << "Last element: " << myVector.back(); // 输出向量最后一个元素
```
#include <iostream> #include <string> #include <stack> #include <vector> #include <iomanip> using namespace std; 逐行注释以上代码
这是一段C++的程序代码,使用了几个标准库的头文件。以下是逐行的注释:
```
#include <iostream> // 输入输出流库头文件
#include <string> // 字符串库头文件
#include <stack> // 栈库头文件
#include <vector> // 向量库头文件
#include <iomanip> // 格式控制库头文件
using namespace std; // 命名空间
```
这段程序代码导入了五个标准库的头文件,分别是iostream、string、stack、vector、iomanip,它们分别用于处理输入输出、字符串、栈、向量和格式控制。其中iostream是C++中最常用的输入输出流库,string是用于字符串操作的库,stack是用于栈数据结构的库,vector是用于动态数组的库,iomanip是用于格式化输出的库。
最后一行代码使用了命名空间std,这意味着所有的标准库函数和变量都在std命名空间中。如果不使用using namespace std,则需要在使用标准库函数时加上std::前缀。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)