我必须比较两个数组,我需要知道是否存在另一个数组中相同的值。我的问题是它总是只返回一个匹配的值,但有两个具有相同的名称。
一个阵列长9个,另外3个。
是否有更简单的解决方案,因为我看起来有点过于复杂,我是对的吗?
提前谢谢
这是我的代码:
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?
}
答案 0 :(得分:2)
如果数组可以按标题排序,那么一种解决方案就是使用laraveltest.de/test2/public
。
Pre C ++ 11代码:
std::set_intersection
请注意,最后,我们创建了一个包含常用名称的向量。另请注意,我们必须先对数组进行排序,并提供#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"));
}
以了解项目的排序方式(根据set_intersection
仿函数)。
答案 1 :(得分:1)
首先,在第一个循环的每次迭代中将counter
重新初始化为0,这可能与它有关。
其次,我将使用两个for
循环执行以下操作:
int counter = 0;
for(int i = 0; i<3; i++)
{
for(int j=0; j<9; j++)
{
if(array1[i] == array2[j])
{
counter++;
}
}
}
很难理解你的逻辑,因为我不确定你使用counter
的原因是什么(或者为什么(但是简单来说,它只是一个计数器来存储等价值的次数)已经匹配。让我们不要太复杂了。)
所以它只是一个简单的外部和内部for
循环,您可以迭代并比较第二个数组的所有9个值到第一个数组的每个值(3个值)。我希望这会有所帮助。