c++ - 使用 C++ 将数组的所有值与另一个数组的值进行比较

标签 c++ arrays

我必须比较两个数组,我需要知道另一个数组中是否存在同名的值。我的问题是它总是只返回一个匹配值,但有两个同名。

一个数组的长度比另外 3 个大 9。

是否有更简单的解决方案,因为我的解决方案看起来有点过于复杂,对吗?

提前致谢

这是我的代码:

for (int i=0;i<9;i++ )
    {
        int counter = 0;
        int j = 0;
        if (stockTest[j].getTestTitle() == products[i].getTitle())
        {

            cout << stockTest[j].getTestTitle() << " is available ";
            counter = counter + 1;  // counter + 1 because it is available

        }

        if ((j == 0) && (counter == 0) && (i == 9)) // try everything till i is 9 if counter is still 0 display message.
        {
            cout << stockTest[j].getTestTitle() << " is not available ";

        }

        if ((j == 1) && (counter == 0) && (i == 9)) // compare everything from stockTest[1], till i is 9 if counter is still 0 display message.
        {
            cout << stockTest[j].getTestTitle() << " is not available ";

        }

        if ((j == 2) && (counter == 0) && (i == 9)) //compare everything from stockTest[2], till i is 9 if counter is still 0 display message.
        {
            cout << stockTest[j].getTestTitle() << " is not available ";

        }

        if ( i == 9)
        {
            j = j + 1; //there are three values to compare in the other array so I will increment like this till 2 (the next if statement will end the loop if j == 2)
            i = 0;     // i again 0 so that again all 9 values from the array will be compared
            counter = 0; // counter = 0 so that if the value is not found the counter == 0 is true
        }

        if ((j == 2) && ( i = 9 ))
        i = 9; //i is now 9 which means that loop should end now however I can delete this line of code and the program would still display only one value. I expected an infinte loop if i delete it?



    }  

最佳答案

如果数组可以按标题排序,那​​么一种解决方案是使用 std::set_intersection

C++ 11 之前的代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <ostream>

struct stockTest
{
    std::string title;
    std::string getTitle() const { return title; }
    stockTest(const std::string s) : title(s) {}
    friend std::ostream& operator << (std::ostream& os, const stockTest&);
};

// for output purposes
std::ostream& operator << (std::ostream& os, const stockTest& s)
{
    os << s.getTitle();
    return os;
}

// compares two stockTest items
bool Comparer(const stockTest& f1, const stockTest& f2)
{
    return f1.getTitle() < f2.getTitle();
}

using namespace std;

int main()
{
    stockTest s1[] = {stockTest("abc"), stockTest("123"), stockTest("456")};
    stockTest s2[] = {stockTest("123"), stockTest("Joe"), stockTest("789"), stockTest("456")};

    // first, we sort our arrays
    std::sort(s1, s1 + 3, Comparer);
    std::sort(s2, s2 + 4, Comparer);

    // this vector will contain the similar items
    std::vector<stockTest> v_intersection;

    // use set_intersection to do the hard work
    std::set_intersection(s1, s1 + 3, s2, s2 + 4, std::back_inserter(v_intersection), Comparer);

    // output the results
    cout << "The similar names are: " << endl;
    copy(v_intersection.begin(), v_intersection.end(), ostream_iterator<stockTest>(cout, "\n"));
}

请注意,最后,我们创建了一个包含常用名称的 vector 。另请注意,我们必须先对数组进行排序,然后提供 set_intersection 以了解项目是如何排序的(根据 Comparer 仿函数)。

实例:http://ideone.com/GA8ey0

关于c++ - 使用 C++ 将数组的所有值与另一个数组的值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30511039/

相关文章:

c++ - 用于 int3 的 Cuda AtomicAdd

c++ - 未定义对 `WinMain@16' 的引用

c++ - Rapidjson 根本不编码 utf8 序列

python - 如何创建特定大小的二项式数组

java - 这个数组有什么问题

c++ - 调用 `string::c_str()` 时实际上做了什么?

Qt 插件接口(interface)中模板类的 C++ 替代方案

javascript - Highcharts .update 修改原始数据数组

arrays - 如何从 static "Array of TPoint"中删除/删除特定成员或元素?

Javascript - 从数组中选择随机页面但绝不显示页面两次