用c++写,stringfilterbylength根据用户设定的长度区间,筛选出符合条件的字符串并输出 ,给出stringfilterbylength函数的定义
时间: 2024-03-05 16:47:54 浏览: 13
好的,下面是stringfilterbylength函数的定义:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> stringfilterbylength(const vector<string>& strList, const int& minLength, const int& maxLength) {
vector<string> result;
for (int i = 0; i < strList.size(); i++) {
if (strList[i].length() >= minLength && strList[i].length() <= maxLength) {
result.push_back(strList[i]);
}
}
return result;
}
int main() {
// 测试代码
vector<string> strList = {"Hello", "World", "C++", "Programming"};
vector<string> result = stringfilterbylength(strList, 3, 5);
for (int i = 0; i < result.size(); i++) {
cout << result[i] << endl;
}
return 0;
}
```
这里我们使用了vector容器来保存符合条件的字符串,函数的参数包括一个字符串列表和两个整数,分别表示最小长度和最大长度。函数中使用for循环遍历字符串列表,对于每个字符串,判断其长度是否在给定的区间内,如果是则将其添加到结果中。最后返回结果vector。