int compare<std::vector<T>>(const std::vector<T> &lhs, const std::vector<T> &rhs);
时间: 2023-08-06 22:07:44 浏览: 41
在模板函数的函数名中,使用尖括号 `<>` 显式指定模板参数是错误的语法。在模板函数的定义中,应该使用 `template <typename T>` 或 `template <class T>` 语法来声明模板参数,而不需要显式指定模板参数类型。
因此,正确的写法应该是:
```
template <typename T>
int compare(const std::vector<T> &lhs, const std::vector<T> &rhs);
```
或者使用 `typename` 的简化形式:
```
template <class T>
int compare(const std::vector<T> &lhs, const std::vector<T> &rhs);
```
这样定义的模板函数可以比较两个 vector 容器内的元素是否相等。
相关问题
template <typename T> int compare<std::vector<T>>(const std::vector<T> &lhs, const std::vector<T> &rhs);有什么错误
在模板函数的函数名中,应该使用模板参数名 `T`,而不是具体的类型 `std::vector<T>`。因此,正确的写法应该是:
```
template <typename T>
int compare(const std::vector<T> &lhs, const std::vector<T> &rhs);
```
或者使用 `typename` 的简化形式:
```
template <class T>
int compare(const std::vector<T> &lhs, const std::vector<T> &rhs);
```
这样定义的模板函数可以比较两个 vector 容器内的元素是否相等。
QString 转std::vector<std::string>
QString是Qt框架中的一个类,用于处理字符串。如果你想将QString转换为std::vector<std::string>,可以按照以下步骤进行操作:
1. 首先,将QString转换为std::string。可以使用QString的toStdString()函数来实现这一步骤。
2. 接下来,将std::string转换为std::vector<std::string>。可以使用std::istringstream和std::getline函数来实现这一步骤。具体步骤如下:
```cpp
#include <QString>
#include <vector>
#include <string>
#include <sstream>
std::vector<std::string> QStringToVector(const QString& qstr) {
std::vector<std::string> result;
std::string str = qstr.toStdString();
std::istringstream iss(str);
std::string line;
while (std::getline(iss, line)) {
result.push_back(line);
}
return result;
}
```
这样,你就可以将QString转换为std::vector<std::string>了。