gpt4 book ai didi

c++ - 如何在 vector 中找到一个项目?

转载 作者:行者123 更新时间:2023-11-28 02:10:05 26 4
gpt4 key购买 nike

我有一段这样的代码。

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <functional>

using namespace std;

class A
{
public:
A(int iVal) : _val(iVal) {}
int getVal() const { return _val; }
private:
int _val;
};

class B
{
public:
B(int iNum) : _num(iNum) {}
int getNum() const { return _num; }
private:
int _num;
};

bool isInVecA(vector<A> vectorA, int iVal)
{
for(vector<A>::const_iterator it=vectorA.begin(); it != vectorA.end(); it++)
{
if(it->getVal() == iVal)
return true;
}
return false;
}

bool isInVecB(vector<B> vectorB, int iNum)
{
for(vector<B>::const_iterator it=vectorB.begin(); it != vectorB.end(); it++)
{
if(it->getNum() == iNum)
return true;
}
return false;
}

int main()
{
A arrayA[] = { A(1), A(2), A(3) };
vector<A> vectorA(arrayA, arrayA + sizeof(arrayA) / sizeof(A));

B arrayB[] = { B(3), B(4), B(5) };
vector<B> vectorB(arrayB, arrayB + sizeof(arrayB) / sizeof(B));

int key = 3;

if(isInVecA(vectorA, key) && isInVecB(vectorB, key))
cout << "Key " << key << " is in both vectors." << endl;
else
cout << "Key " << key << " is not in both vectors." << endl;

return 0;
}

我想做的只是创建一些东西来替换函数 isInVecAisInVecB 因为它们太相似了。假设我不能更改A类B类


非常感谢大家。我对 StackOverflow 很陌生。不知道在哪里发布我和我的 friend 想出的解决方案。因此,我把它放在这里。

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <functional>
#include <boost/bind.hpp>

using namespace std;

class A
{
public:
A(int iVal) : _val(iVal) {}
int getVal() const { return _val; }
private:
int _val;
};

class B
{
public:
B(int iNum) : _num(iNum) {}
int getNum() const { return _num; }
private:
int _num;
};

template<typename T>
bool isInVec(vector<T> vec, int (T::*func)() const, int iVal)
{
return find_if(vec.begin(), vec.end(), boost::bind(func, _1) == iVal) != vec.end();
}

int main()
{
A arrayA[] = { A(1), A(2), A(3) };
vector<A> vectorA(arrayA, arrayA + sizeof(arrayA) / sizeof(A));

B arrayB[] = { B(3), B(4), B(5) };
vector<B> vectorB(arrayB, arrayB + sizeof(arrayB) / sizeof(B));

int key = 3;

if(isInVec<A>(vectorA, &A::getVal, key) && isInVec<B>(vectorB, &B::getNum, key))
cout << "Key " << key << " is in both vectors." << endl;
else
cout << "Key " << key << " is not in both vectors." << endl;

return 0;
}

最佳答案

您可以创建比较器并使用 std::find_if .

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <functional>

using namespace std;

class A
{
public:
A(int iVal) : _val(iVal) {}
int getVal() const { return _val; }
private:
int _val;
};

class B
{
public:
B(int iNum) : _num(iNum) {}
int getNum() const { return _num; }
private:
int _num;
};

class cmpA
{
private:
int target;
public:
cmpA(int t) : target(t) {}
bool operator()(const A& a) const {
return a.getVal() == target;
}
};
class cmpB
{
private:
int target;
public:
cmpB(int t) : target(t) {}
bool operator()(const B& b) const {
return b.getNum() == target;
}
};

template<class T, class V>
bool isInVec(const vector<V>& vector, int iNum)
{
return find_if(vector.begin(), vector.end(), T(iNum)) != vector.end();
}

int main(void) {
A arrayA[] = { A(1), A(2), A(3) };
vector<A> vectorA(arrayA, arrayA + sizeof(arrayA) / sizeof(A));

B arrayB[] = { B(3), B(4), B(5) };
vector<B> vectorB(arrayB, arrayB + sizeof(arrayB) / sizeof(B));

int key = 3;

if(isInVec<cmpA>(vectorA, key) && isInVec<cmpB>(vectorB, key))
cout << "Key " << key << " is in both vectors." << endl;
else
cout << "Key " << key << " is not in both vectors." << endl;

return 0;
}

更新:新代码,相似代码更少,基于Adapter Design Pattern的思想:

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <functional>

using namespace std;

class A
{
public:
A(int iVal) : _val(iVal) {}
int getVal() const { return _val; }
private:
int _val;
};

class B
{
public:
B(int iNum) : _num(iNum) {}
int getNum() const { return _num; }
private:
int _num;
};

// dummy number getter
template<class T> int getNumber(const T& x) { return 0; }

// number getter for class A
template<> int getNumber(const A& x) { return x.getVal(); }

// number getter for class B
template<> int getNumber(const B& x) { return x.getNum(); }

// comparator using the number getter
template<class T>
class cmp
{
private:
int target;
public:
cmp(int t) : target(t) {}
bool operator()(const T& a) const { return getNumber<T>(a) == target; }
};

template<class T>
bool isInVec(const vector<T>& vector, int iNum)
{
return find_if(vector.begin(), vector.end(), cmp<T>(iNum)) != vector.end();
}

int main(void) {
A arrayA[] = { A(1), A(2), A(3) };
vector<A> vectorA(arrayA, arrayA + sizeof(arrayA) / sizeof(A));

B arrayB[] = { B(3), B(4), B(5) };
vector<B> vectorB(arrayB, arrayB + sizeof(arrayB) / sizeof(B));

int key = 3;

if(isInVec(vectorA, key) && isInVec(vectorB, key))
cout << "Key " << key << " is in both vectors." << endl;
else
cout << "Key " << key << " is not in both vectors." << endl;

return 0;
}

关于c++ - 如何在 vector 中找到一个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36011327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com