C++ 如何有效地具体比较向量和数组?
我想比较一个向量和一个数组,假设元素的顺序不同。 我有一个如下的结构(这个结构没有“operator==”和“operator”等)C++ 如何有效地具体比较向量和数组?,c++,arrays,vector,find,compare,C++,Arrays,Vector,Find,Compare,我想比较一个向量和一个数组,假设元素的顺序不同。 我有一个如下的结构(这个结构没有“operator==”和“operator”等) bool checkIfTheSame(const std::vector&l_v,const A&l_A) { 用于(使用指定的整数i=0;i,而不包括“operator==”和“operator>”等 检查两个容器是否包含具有谓词的相同数据:具有自定义谓词 如果您只想检查您的容器是否具有特定值,请查看此处:查看此处: 用lambda表达式查找 函数,该函数只能
bool checkIfTheSame(const std::vector&l_v,const A&l_A)
{
用于(使用指定的整数i=0;i<3;++i)
{
如果(!std::某个函数X(l_v.begin(),l_v.end(),l_a,
[](施工A&lhs、施工A&rhs){
返回lhs.index==rhs.index;
}))返回false;
}
返回true;
}
谢谢。看这里:
用lambda表达式查找
函数,该函数只能通过类中的特定字段比较特定字段,如“lhs.index==rhs.index”->,而不包括“operator==”和“operator>”等
检查两个容器是否包含具有谓词的相同数据:具有自定义谓词
如果您只想检查您的容器是否具有特定值,请查看此处:查看此处:
用lambda表达式查找
函数,该函数只能通过类中的特定字段比较特定字段,如“lhs.index==rhs.index”->,而不包括“operator==”和“operator>”等
检查两个容器是否包含具有谓词的相同数据:具有自定义谓词
如果您只想检查您的容器是否具有特定值,请查看此处:查看此处:
用lambda表达式查找
函数,该函数只能通过类中的特定字段比较特定字段,如“lhs.index==rhs.index”->,而不包括“operator==”和“operator>”等
检查两个容器是否包含具有谓词的相同数据:具有自定义谓词
如果您只想检查您的容器是否具有特定值,请查看此处:查看此处:
用lambda表达式查找
函数,该函数只能通过类中的特定字段比较特定字段,如“lhs.index==rhs.index”->,而不包括“operator==”和“operator>”等
检查两个容器是否包含具有谓词的相同数据:具有自定义谓词
如果您只想检查您的容器是否具有特定值,请查看以下内容:
此结构没有“运算符==”和“运算符”
此结构没有“运算符==”和“运算符”
此结构没有“运算符==”和“运算符”
这个结构没有“operator==”和“operator我不确定我是否完全理解你想要什么,但我猜你想检查两个数组(具有不同的容器类型)是否等效,也就是说,对于数组a中的每个项,数组B中的某个地方都有一个对应项,其中某个谓词返回true 您希望在两个阵列上执行此操作,这两个阵列不仅具有不同的类型,而且可能具有不同的顺序 这是一种方法:
struct Foo
{
Foo(int i) : _index { i } {}
int index() const {
return _index;
}
private:
int _index;
};
// params:
// first1, last1 bound the first array
// first2, last2 bound the second array
// pred is a predicate that checks for equivalents
// order is a predicate that performs weak ordering
template<class Iter1, class Iter2, class Pred, class Order>
bool checkIfTheSame(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Pred pred, Order order)
{
const auto len1 = last1 - first1;
const auto len2 = last2 - first2;
if (len1 != len2)
return false;
using namespace std;
using item1_type = typename std::iterator_traits<Iter1>::value_type;
using item2_type = typename std::iterator_traits<Iter2>::value_type;
std::vector<std::reference_wrapper<item1_type>> refs1 { first1, last1 };
std::sort(begin(refs1), end(refs1), order);
std::vector<std::reference_wrapper<item2_type>> refs2 { first2, last2 };
std::sort(begin(refs2), end(refs2), order);
for (size_t i = 0 ; i < len1 ; ++i) {
if (!pred(refs1[i], refs2[i]))
return false;
}
return true;
}
structfoo
{
Foo(inti):_index{i}{}
int index()常量{
回归指数;
}
私人:
int_指数;
};
//参数:
//first1,last1绑定第一个数组
//first2,last2绑定第二个数组
//pred是检查等价项的谓词
//order是执行弱排序的谓词
模板
bool检查名称(Iter1 first1、Iter1 last1、Iter2 first2、Iter2 last2、Pred Pred、Order)
{
const auto len1=last1-first1;
const auto len2=last2-first2;
if(len1!=len2)
返回false;
使用名称空间std;
使用item1\u type=typename std::iterator\u traits::value\u type;
使用item2\u type=typename std::iterator\u traits::value\u type;
std::向量refs1{first1,last1};
排序(开始(参考s1)、结束(参考s1)、顺序);
std::向量refs2{first2,last2};
排序(开始(参考S2),结束(参考S2),顺序);
对于(尺寸i=0;i
简单的例子:
using namespace std;
bool same_indexes(const Foo& f1, const Foo& f2) {
return f1.index() == f2.index();
}
bool sort_indexes(const Foo& f1, const Foo& f2) {
return f1.index() < f2.index();
}
int main(int argc, const char * argv[])
{
vector<Foo> x { 1, 2, 3 };
Foo y[] = { 3, 2, 1 };
cout << checkIfTheSame(begin(x), end(x), begin(y), end(y), same_indexes, sort_indexes) << endl;
return 0;
}
使用名称空间std;
bool相同的索引(常量Foo&f1、常量Foo&f2){
返回f1.index()==f2.index();
}
布尔排序索引(常量Foo&f1、常量Foo&f2){
返回f1.index() cout我不确定我是否完全理解您想要的内容,但我猜您想检查两个数组(具有不同的容器类型)是否等效,也就是说,对于数组a中的每个项,数组B中的某个位置都有一个对应项,其中某个谓词返回true
您希望在两个阵列上执行此操作,这两个阵列不仅具有不同的类型,而且可能具有不同的顺序
这是一种方法:
struct Foo
{
Foo(int i) : _index { i } {}
int index() const {
return _index;
}
private:
int _index;
};
// params:
// first1, last1 bound the first array
// first2, last2 bound the second array
// pred is a predicate that checks for equivalents
// order is a predicate that performs weak ordering
template<class Iter1, class Iter2, class Pred, class Order>
bool checkIfTheSame(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Pred pred, Order order)
{
const auto len1 = last1 - first1;
const auto len2 = last2 - first2;
if (len1 != len2)
return false;
using namespace std;
using item1_type = typename std::iterator_traits<Iter1>::value_type;
using item2_type = typename std::iterator_traits<Iter2>::value_type;
std::vector<std::reference_wrapper<item1_type>> refs1 { first1, last1 };
std::sort(begin(refs1), end(refs1), order);
std::vector<std::reference_wrapper<item2_type>> refs2 { first2, last2 };
std::sort(begin(refs2), end(refs2), order);
for (size_t i = 0 ; i < len1 ; ++i) {
if (!pred(refs1[i], refs2[i]))
return false;
}
return true;
}
structfoo
{
Foo(inti):_index{i}{}
int index()常量{
回归指数;
}
私人:
int_指数;
};
//参数:
//first1,last1绑定第一个数组
//first2,last2绑定第二个数组
//pred是检查等价项的谓词
//order是执行弱排序的谓词
模板
bool检查名称(Iter1 first1、Iter1 last1、Iter2 first2、Iter2 last2、Pred Pred、Order)
{
const auto len1=last1-first1;
const auto len2=last2-first2;
if(len1!=len2)
返回false;
使用名称空间std;
使用item1\u type=typename std::iterator\u traits::value\u type;
使用item2\u type=typename std::iterator\u traits::value\u type;
std::向量refs1{first1,last1};
排序(开始(参考s1)、结束(参考s1)、顺序);
std::向量refs2{first2,last2};
排序(开始(参考S2),结束(参考S2),顺序);
对于(尺寸i=0;i
简单的例子:
using namespace std;
bool same_indexes(const Foo& f1, const Foo& f2) {
return f1.index() == f2.index();
}
bool sort_indexes(const Foo& f1, const Foo& f2) {
return f1.index() < f2.index();
}
int main(int argc, const char * argv[])
{
vector<Foo> x { 1, 2, 3 };
Foo y[] = { 3, 2, 1 };
cout << checkIfTheSame(begin(x), end(x), begin(y), end(y), same_indexes, sort_indexes) << endl;
return 0;
}
使用名称空间std;
bool相同的索引(常量Foo&f1、常量Foo&f2){
返回f1.index()==f2.index();
}
布尔排序索引(常量Foo&f1、常量Foo&f2){
返回f1。
struct Foo
{
Foo(int i) : _index { i } {}
int index() const {
return _index;
}
private:
int _index;
};
// params:
// first1, last1 bound the first array
// first2, last2 bound the second array
// pred is a predicate that checks for equivalents
// order is a predicate that performs weak ordering
template<class Iter1, class Iter2, class Pred, class Order>
bool checkIfTheSame(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Pred pred, Order order)
{
const auto len1 = last1 - first1;
const auto len2 = last2 - first2;
if (len1 != len2)
return false;
using namespace std;
using item1_type = typename std::iterator_traits<Iter1>::value_type;
using item2_type = typename std::iterator_traits<Iter2>::value_type;
std::vector<std::reference_wrapper<item1_type>> refs1 { first1, last1 };
std::sort(begin(refs1), end(refs1), order);
std::vector<std::reference_wrapper<item2_type>> refs2 { first2, last2 };
std::sort(begin(refs2), end(refs2), order);
for (size_t i = 0 ; i < len1 ; ++i) {
if (!pred(refs1[i], refs2[i]))
return false;
}
return true;
}
using namespace std;
bool same_indexes(const Foo& f1, const Foo& f2) {
return f1.index() == f2.index();
}
bool sort_indexes(const Foo& f1, const Foo& f2) {
return f1.index() < f2.index();
}
int main(int argc, const char * argv[])
{
vector<Foo> x { 1, 2, 3 };
Foo y[] = { 3, 2, 1 };
cout << checkIfTheSame(begin(x), end(x), begin(y), end(y), same_indexes, sort_indexes) << endl;
return 0;
}