vector<string>& strs
时间: 2023-12-09 18:03:04 浏览: 213
Vector<string> is a data structure in C++ that stores a dynamic array of strings. It is a part of the standard template library (STL) and can be used to store and manipulate a collection of strings. The vector class provides various member functions to manipulate the elements in the vector, such as push_back(), pop_back(), size(), empty(), and more. The vector<string> is useful for situations where you need to store a variable number of strings and need to access them in a sequential manner.
相关问题
string longestCommonPrefix(vector<string>& strs)
strs) {
if(strs.empty()) return "";
int minStrLen = INT_MAX;
for(auto s:strs) minStrLen = min(minStrLen, (int)s.length());
int l = 0, r = minStrLen-1;
while(l <= r) {
int mid = l + (r-l)/2;
string prefix = strs[0].substr(0, mid+1);
bool flag = true;
for(int i = 1; i < strs.size(); i++) {
if(strs[i].substr(0, mid+1) != prefix) {
flag = false;
break;
}
}
if(flag) l = mid + 1;
else r = mid - 1;
}
return strs[0].substr(0, l);
}
主函数怎么调用vector<string>& strs
`vector<string>& strs` 是一个引用类型参数,它表示传入的参数是一个字符串类型的向量,并且这个向量是按引用传递的。如果你想在主函数中调用一个函数,该函数的参数类型是 `vector<string>&`,你需要先创建一个 `vector<string>` 类型的向量,然后将其作为参数传递给该函数。以下是一个示例代码:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void myFunction(vector<string>& strs) {
// 在这里对传入的字符串向量进行操作
for (int i = 0; i < strs.size(); i++) {
cout << strs[i] << endl;
}
}
int main() {
vector<string> myStrings = {"Hello", "World", "!"};
myFunction(myStrings); // 将myStrings作为参数传递给myFunction函数
return 0;
}
```
在上面的示例代码中,我们首先创建了一个 `vector<string>` 类型的向量 `myStrings`,并将三个字符串添加到了这个向量中。然后我们调用了 `myFunction` 函数,并将 `myStrings` 作为参数传递给了这个函数。在 `myFunction` 函数中,我们遍历了传入的字符串向量,并将每个字符串打印到屏幕上。
阅读全文