C ++条件语句来测试变量是否为字符串
C++ conditional statement to test if variable is a string
我有一个使用模板的 C++ 方法 findID
,我希望能够 运行 此方法中基于输入类型的条件。模板参数 U
要么是 int 类型,要么是 string 类型。我想 运行 基于 ID 类型的不同条件。
我的代码如下:
template <typename S>
template <typename U>
S * findID(U ID){
for (typename vector<S*>::collectionsIter element = collection.begin() ; element != collection.end(); ++element)
if((*element)->getID() == ID) return *element;
return NULL;
}
我希望我的代码执行以下操作:
template <typename S>
template <typename U>
S * findID(U ID){
***if ID is an int:
for (typename vector<S*>::collectionsIter element = collection.begin() ; element != collection.end(); ++element)
if((*element)->getID() == ID) return *element;
***if ID is a string:
for (typename vector<S*>::collectionsIter element = collection.begin() ; element != collection.end(); ++element)
if((*element)->getStringID() == ID) return *element;
***else
return NULL;
}
我之所以要这样做是因为我希望能够将ID的字符串变量与getStringID()的字符串方法进行比较,将ID的整型变量与getID()的整型方法进行比较。此外,我不想将它们分解成单独的方法,因此我尝试使用模板和这些条件将其重构为 1 种方法。
这是使用 C++17 的一种方法if constexpr
:
struct Foo {
int id;
std::string stringId;
int getId() const { return id; }
const std::string& getStringId() const { return stringId; }
};
template <typename Cont, typename T>
auto findId(const Cont& c, const T& id) {
const auto pred = [&id](const auto& x) {
if constexpr (std::is_convertible_v<T, std::string>)
return x.getStringId() == id;
else if constexpr (std::is_convertible_v<T, int>)
return x.getId() == id;
else
static_assert(false, "Unsupported id type.");
return false;
};
const auto findIt = std::find_if(begin(c), end(c), pred);
return findIt == end(c) ? nullptr : &(*findIt);
}
int main() {
using namespace std;
vector<Foo> foos{{1, "1"}, {2, "2"}, {3, "3"}};
auto foo2Int = findId(foos, 2);
auto foo2String = findId(foos, "2"s);
cout << foo2Int->id << ", " << foo2String->stringId << '\n';
}
只需使用 2 个重载:
template <typename S>
S* findID(int ID){
for (auto* element : collection)
if (element->getID() == ID) return element;
return nullptr;
}
template <typename S>
S* findID(const std::string& ID){
for (auto* element : collection)
if (element->getStringID() == ID) return element;
return nullptr;
}
我有一个使用模板的 C++ 方法 findID
,我希望能够 运行 此方法中基于输入类型的条件。模板参数 U
要么是 int 类型,要么是 string 类型。我想 运行 基于 ID 类型的不同条件。
我的代码如下:
template <typename S>
template <typename U>
S * findID(U ID){
for (typename vector<S*>::collectionsIter element = collection.begin() ; element != collection.end(); ++element)
if((*element)->getID() == ID) return *element;
return NULL;
}
我希望我的代码执行以下操作:
template <typename S>
template <typename U>
S * findID(U ID){
***if ID is an int:
for (typename vector<S*>::collectionsIter element = collection.begin() ; element != collection.end(); ++element)
if((*element)->getID() == ID) return *element;
***if ID is a string:
for (typename vector<S*>::collectionsIter element = collection.begin() ; element != collection.end(); ++element)
if((*element)->getStringID() == ID) return *element;
***else
return NULL;
}
我之所以要这样做是因为我希望能够将ID的字符串变量与getStringID()的字符串方法进行比较,将ID的整型变量与getID()的整型方法进行比较。此外,我不想将它们分解成单独的方法,因此我尝试使用模板和这些条件将其重构为 1 种方法。
这是使用 C++17 的一种方法if constexpr
:
struct Foo {
int id;
std::string stringId;
int getId() const { return id; }
const std::string& getStringId() const { return stringId; }
};
template <typename Cont, typename T>
auto findId(const Cont& c, const T& id) {
const auto pred = [&id](const auto& x) {
if constexpr (std::is_convertible_v<T, std::string>)
return x.getStringId() == id;
else if constexpr (std::is_convertible_v<T, int>)
return x.getId() == id;
else
static_assert(false, "Unsupported id type.");
return false;
};
const auto findIt = std::find_if(begin(c), end(c), pred);
return findIt == end(c) ? nullptr : &(*findIt);
}
int main() {
using namespace std;
vector<Foo> foos{{1, "1"}, {2, "2"}, {3, "3"}};
auto foo2Int = findId(foos, 2);
auto foo2String = findId(foos, "2"s);
cout << foo2Int->id << ", " << foo2String->stringId << '\n';
}
只需使用 2 个重载:
template <typename S>
S* findID(int ID){
for (auto* element : collection)
if (element->getID() == ID) return element;
return nullptr;
}
template <typename S>
S* findID(const std::string& ID){
for (auto* element : collection)
if (element->getStringID() == ID) return element;
return nullptr;
}