利用函数的重载功能编写子函数,返回一串string型字符串数组的连接结果
时间: 2024-02-05 13:12:56 浏览: 28
以下是一个示例函数,它接受两个string数组作为参数,并返回它们连接后的结果:
```cpp
#include <string>
#include <vector>
// 定义重载的函数
std::vector<std::string> join(const std::vector<std::string>& arr1, const std::vector<std::string>& arr2) {
std::vector<std::string> result(arr1);
result.insert(result.end(), arr2.begin(), arr2.end());
return result;
}
// 重载函数,支持两个字符串的连接
std::vector<std::string> join(const std::string& str1, const std::string& str2) {
std::vector<std::string> result(2);
result[0] = str1;
result[1] = str2;
return result;
}
```
使用示例:
```cpp
std::vector<std::string> arr1 = {"hello", "world"};
std::vector<std::string> arr2 = {"how", "are", "you"};
std::vector<std::string> result = join(arr1, arr2);
// result = {"hello", "world", "how", "are", "you"}
std::vector<std::string> strResult = join("hello", "world");
// strResult = {"hello", "world"}
```