C++ 将一个数组的所有值与另一个带有C++;
我必须比较两个数组,我需要知道另一个数组中是否存在相同名称的值。我的问题是,它总是只返回一个匹配的值,但有两个值具有相同的名称 一个数组的长度是9,另一个数组的长度是3 有没有更简单的解决办法,因为我的看起来有点太复杂了,对吗 先谢谢你 这是我的密码:C++ 将一个数组的所有值与另一个带有C++;,c++,arrays,C++,Arrays,我必须比较两个数组,我需要知道另一个数组中是否存在相同名称的值。我的问题是,它总是只返回一个匹配的值,但有两个值具有相同的名称 一个数组的长度是9,另一个数组的长度是3 有没有更简单的解决办法,因为我的看起来有点太复杂了,对吗 先谢谢你 这是我的密码: for (int i=0;i<9;i++ ) { int counter = 0; int j = 0; if (stockTest[j].getTestTitle() == prod
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?
}
对于(int i=0;i首先,您将计数器重新初始化为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++;
}
}
}
int计数器=0;
对于(int i=0;i首先,在第一个循环的每次迭代中,您都将计数器重新初始化为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++;
}
}
}
int计数器=0;
对于(int i=0;i首先,在第一个循环的每次迭代中,您都将计数器重新初始化为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++;
}
}
}
int计数器=0;
对于(int i=0;i首先,在第一个循环的每次迭代中,您都将计数器重新初始化为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++;
}
}
}
int计数器=0;
对于(int i=0;i如果数组可以按标题排序,那么一种解决方案是使用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"));
}
#包括
#包括
#include如果数组可以按标题排序,那么一种解决方案是使用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"));
}
#包括
#包括
#include如果数组可以按标题排序,那么一种解决方案是使用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"));
}
#包括
#包括
#include如果数组可以按标题排序,那么一种解决方案是使用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"));
}
#包括
#包括
#include有一个函数已经调用了std::set_intersection
。有一个函数已经调用了std::set_intersection
。有一个函数已经调用了std::set_intersection
。有一个函数已经调用了std::set_intersectionde>。谢谢,是的,这样做是一个好主意。但是由于某种原因,它仍然找不到我的其他值。它不认为它们是相同的。它们都是从txt文件中提取的。因此,可能有什么问题,但我想你已经回答了这个问题。它们是从同一个文本文件还是不同的文件中提取的?还有,try并逐步检查代码,看看它提取了什么。它可能无法正确处理某些字符或空格,但很高兴我能提供帮助。从不同的文件中,我认为我检查了所有内容,但我认为我需要再次检查。缺点是解决方案有O(n^2)
复杂度,其中n
是array1或array2中的最大项目数。因此,如果array1有1000个项目,而array2有1000个项目,则循环次数为1000000次。如果项目可以排序(使用适当的排序例程,如std::sort
)是的,你可能是对的,但是我仍然是C++初学者,所以我认为我的任务需要简单的循环一个。谢谢,是的,这样做是个好主意。但是,由于某种原因,它仍然没有找到我的其他价值。它不把它们看成是一样的。它们是AR。e都是从一个txt文件中提取的。因此,可能有什么问题,但我想你已经回答了这个问题。它们是从同一个文本文件中提取的还是从不同的文件中提取的?另外,请尝试单步检查代码,看看它提取了什么。它可能无法正确处理某些字符或空格,但很高兴我能提供帮助。来源不同租用文件时,我认为我检查了所有内容,但我认为我需要再次检查。缺点是解决方案具有O(n^2)
复杂性,其中n
是array1或array2中的最大项目数。因此,如果array1有1000个项目,array2有1000个项目,则循环次数为1000000次。如果项目可以排序(使用适当的排序例程,如std::sort
)是的,你可能是对的,但是我仍然是C++初学者,所以我认为我的任务需要简单的循环一个。谢谢,是的,这样做是个好主意。但是,由于某种原因,它仍然没有找到我的其他价值。它不把它们看成是一样的。它们是AR。e都是从一个txt文件中提取的。因此,可能有什么问题,但我想你已经回答了这个问题。它们是从同一个文本文件中提取的还是从不同的文件中提取的?另外,请尝试单步检查代码,看看它提取了什么。它可能无法正确处理某些字符或空格,但很高兴我能提供帮助。来源不同租用文件时,我认为我检查了所有内容,但我认为我需要再次检查。缺点是解决方案具有O(n^2)
复杂性,其中n
是array1或array2中的最大项目数。因此,如果array1有1000个项目,array2有1000个项目,则循环次数为1000000次。如果项目可以排序(使用一个象样的排序例程,如:代码> STD::Stase),那么<代码> SETIONGON/<代码>可以用来软化BLW。是的,你可能是对的,但我仍然是C++初学者,所以我认为我的任务需要简单的循环一个。谢谢,是的,这样做是个好主意。但是,由于某种原因,它是STI。