gpt4 book ai didi

c++ - c++(98) STL set 是否可以散列一个 int 数组,然后检查该数组是否存在于集合中

转载 作者:行者123 更新时间:2023-11-30 05:39:49 26 4
gpt4 key购买 nike

这可能很容易,但到目前为止我还没有找到解决方案。这是我想做的...

int a[] = {1,2,3};
int b[] = {1,2,3};

set<int*>S;

S.insert(a);

if(S.count(b))
{
cout<<"Job Done!"<<endl;
}

最佳答案

std::set是有序容器。一旦你插入 int*它将持有“地址”。在您的示例中,它将保存数组 a 的第一个元素的地址。 . S.count(b)将返回 0,因为地址为 b不等于 a 的地址.

std::set的定义是为了使第二个参数可以是定义“比较”操作的方法(link):

template <
class T, // set::key_type/value_type
class Compare = less<T>, // set::key_compare/value_compare
class Alloc = allocator<T> // set::allocator_type
> set;

因此,如果您编写一个函数来定义两个 int* 之间的“<”操作(比方说 intStarComp(const int*, const int*)),std::set<int*>::find将检查 a==b通过申请!intStarComp(a,b) && !intStarComp(b,a) .如果它将返回 true 而不是 a==bS.count(b)将返回 1。

Compare A binary predicate that takes two arguments of the same type as the elements and returns a bool. The expression comp(a,b), where comp is an object of this type and a and b are key values, shall return true if a is considered to go before b in the strict weak ordering the function defines. The set object uses this expression to determine both the order the elements follow in the container and whether two element keys are equivalent (by comparing them reflexively: they are equivalent if !comp(a,b) && !comp(b,a)). No two elements in a set container can be equivalent. This can be a function pointer or a function object (see constructor for an example). This defaults to less, which returns the same as applying the less-than operator (a

关于c++ - c++(98) STL set 是否可以散列一个 int 数组,然后检查该数组是否存在于集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32079126/

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