C++ 求'的交集;n';泛型数组

C++ 求'的交集;n';泛型数组,c++,arrays,algorithm,generics,intersection,C++,Arrays,Algorithm,Generics,Intersection,编辑:添加了更多细节 我正在尝试编写一个算法来查找n个数组的交点(所有数组的公共点)。我的程序获取这些数组并将它们存储在一个二维数组中,在该数组上进行操作。例如,下面是一个示例main方法: int a[] = { 12, 54, 42 }; int b[] = { 54, 3, 42, 7 }; int c[] = { 3, 42, 54, 57, 3 }; IntersectionTableau<int> x(3); // Where 3 is the max numbe

编辑:添加了更多细节

我正在尝试编写一个算法来查找n个数组的交点(所有数组的公共点)。我的程序获取这些数组并将它们存储在一个二维数组中,在该数组上进行操作。例如,下面是一个示例main方法:

int a[] = { 12, 54, 42 };
int b[] = { 54, 3, 42, 7 };
int c[] = { 3, 42, 54, 57, 3 };

IntersectionTableau<int> x(3);    // Where 3 is the max number of arrays allowed
                                  // to be stored.
x.addArray(a, 3);
x.addArray(b, 4);
x.addArray(c, 9);
x.run();            // Finds the intersection.
}


第一个函数获取前两个数组并将其发送给第二个函数,该函数返回这两个数组之间的交点数组。然后,第一个函数将相交数组与
d_数组
中的下一个数组进行比较,依此类推。我的问题是,当我从
d_results
打印出一个元素时,会产生一个垃圾值,我不确定为什么。有人能告诉我我做错了什么吗?或者,还有一种更好的方法来实现这一点吗?

代码中至少有两个问题:


如果(第一[i]==第二[j]),这应该是


这是比较棘手的问题。我想您需要另一个变量(既不是
I
也不是
j
);让我们称之为
k

commonElements[k++] = first[i];

无论如何,既然你可以使用C++,你可以使用<代码> STD::vector < /Cord>。它把它的大小储存在里面;这将减少混淆:

template <class T>
std::vector<T> // note: adjusted the return type!
IntersectionTableau<T>::getIntersection(...)
{
    std::vector<T> commonElements;
    for (int i = 0; i < sizeOfFirst; ++i) {
        for (int j = 0; j < sizeOfSecond; ++j) {
            if (first[i] == second[j]) {
                commonElements.push_back(first[i]);
            }
        }
    }
    return commonElements;
}

代码中至少有两个问题:


如果(第一[i]==第二[j]),这应该是


这是比较棘手的问题。我想您需要另一个变量(既不是
I
也不是
j
);让我们称之为
k

commonElements[k++] = first[i];

无论如何,既然你可以使用C++,你可以使用<代码> STD::vector < /Cord>。它把它的大小储存在里面;这将减少混淆:

template <class T>
std::vector<T> // note: adjusted the return type!
IntersectionTableau<T>::getIntersection(...)
{
    std::vector<T> commonElements;
    for (int i = 0; i < sizeOfFirst; ++i) {
        for (int j = 0; j < sizeOfSecond; ++j) {
            if (first[i] == second[j]) {
                commonElements.push_back(first[i]);
            }
        }
    }
    return commonElements;
}

真不敢相信我错过了!除此之外,如果我使用vector,我是否能够将其转换为类型
T*
,以便返回它?我不熟悉
std::vector
。虽然反应不错,但还是帮了不少忙。谢谢。好的,我使用的是你的
std::vector
方法,它看起来很有效(程序构建正确),但是,当使用语句
return&commonElements[0]
返回时,程序会产生一个segfault。当我使用我的代码时,这不会发生,请保存以进行更正
if(first[I]==second[j])
。您知道为什么会发生这种情况吗?这是因为您返回的是本地对象的地址,从函数返回后将被删除。有没有其他方法可以在不删除的情况下执行此操作(将向量作为数组返回)?(第一[i]==第二[i]),不敢相信我错过了!除此之外,如果我使用vector,我是否能够将其转换为类型
T*
,以便返回它?我不熟悉
std::vector
。虽然反应不错,但还是帮了不少忙。谢谢。好的,我使用的是你的
std::vector
方法,它看起来很有效(程序构建正确),但是,当使用语句
return&commonElements[0]
返回时,程序会产生一个segfault。当我使用我的代码时,这不会发生,请保存以进行更正
if(first[I]==second[j])
。您知道为什么会发生这种情况吗?这是因为您返回的是本地对象的地址,从函数返回后将被删除。有没有其他方法可以在不删除的情况下执行此操作(将向量作为数组返回)?
template <class T>
std::vector<T> // note: adjusted the return type!
IntersectionTableau<T>::getIntersection(...)
{
    std::vector<T> commonElements;
    for (int i = 0; i < sizeOfFirst; ++i) {
        for (int j = 0; j < sizeOfSecond; ++j) {
            if (first[i] == second[j]) {
                commonElements.push_back(first[i]);
            }
        }
    }
    return commonElements;
}
T* commonElements = NULL;
for (int whatever = 0; whatever < 10; ++whatever)
{
    std::vector<T> elements = xxx.getIntersection(...); // will work
    commonElements = &elements[0]; // can pass this pointer to a function receiving T*
    print(commonElements); // will work
}
print(commonElements); // cannot possibly work, will probably segfault
print(elements); // cannot possibly work, and compiler will reject this