C++ 如何检查数组中的所有值是否不同?
C++ How to check if all values in array are different?
您好,我正在尝试检查在任何大小的数组中输入的任何值是否不同。我正在尝试为此代码使用嵌套循环,但无法获得正确的 if 语句来检查数组中的每个值是否不同。如果有任何帮助,我将不胜感激!
for (unsigned i = 0; i < size; i++)
for (unsigned k = i + 1; k < size; k++)
if (arr[i] == arr[k]){
return false;
}
return true;
好的,谢谢你们的帮助,你们的建议奏效了!
第一个 for 循环是错误的。有一个 j
而不是 i
for (unsigned i = 0; i < size; i++)
...
这是一个方法..
//Implement an algorithm “Unique” to check if all elements of a given set of
//integers are distinct.
#include <iostream>
using namespace std;
int main()
{
int arr[10] = {10, 20, 50, 90, 30, 60, 35, 40, 85, 90};
int i, k, origVal = 0, newVal = 0;
for (i = 0; i < 10; i++)
{
origVal = arr[i];
for (k = i+1; k < 10; k++)
{
if (origVal == arr[k])
{
newVal = 1;
break;
}
}
if (newVal ){break;}
}
if (newVal == 1)
{
cout<<"The Array does not contain completely distinct values"<<endl;
}
else
{
cout<< "The Array is distinct"<<endl;
}
system("PAUSE");
return 0;
}
你的代码有两个问题:
1. first loop should have j instead if i.
2. The second loop should start from i+1.
希望对您有所帮助。
为什么不使用惯用的 C++ 结构?
if (std::equal(a1, a1 + sizeof a1 / sizeof *a1, a2))
其中 a1 和 a2 是您的两个数组。
我看到问题得到了更正,只有 i 而不是 i 和 j,这让我认为这是两个数组。我将留下这个答案作为在 C++11 之前的版本中使用 std::equal 的参考。但对于 C+11,请参阅下面的 Jens 解决方案。为此,必须包括 <algorithm>
和 <iterator>
。
A std::set 仅包含唯一元素。
#include <iostream>
#include <set>
int main()
{
int a[] = { 1, 9, 4, 5, 8, 3, 1, 3, 5 };
int b[] = { 1, 9, 4, 5, 8, 3 };
std::set<int> sa(a, a + 9);
std::cout << std::boolalpha << (sa.size() == (sizeof(a)/sizeof(*a))); //all values not different
std::set<int> sb(b, b + 6);
std::cout << std::boolalpha << (sb.size() == (sizeof(b)/sizeof(*b))); //true, all values are different
}
更快更好的方法是使用地图。例如
std::map<int, int> m;
for (int i = 0; i < size; ++i) {
if (m[i] > 0) return false;
m[i]++;
}
return true;
一个衬里看起来像这样
return std::unique(arr, arr + size) == (arr + size);
你能先把 arr
排序吗?
std::sort(std::begin(arr), std::end(arr));
auto pos = std::adjacent_find(std::begin(arr), std::end(arr));
if (pos != std::end(arr))
// we have a duplicate
std::sort( std::begin(arr), std::end(arr) );
auto u = std::unique( std::begin(arr), std::end(arr) );
bool containsDuplicate = u != std::end(arr);
如果您的序列已排序并且您想去除重复项,或者如果您有能力对它或它的副本进行排序,然后删除重复项,那么您可以使用 std::unique标准库算法。
http://en.cppreference.com/w/cpp/algorithm/unique 中有很好的示例,但您需要记住的是,如果将没有重复项的排序序列传递给 std::unique,它会 return 一个迭代器最后一个元素 - 通常是 end().
如果它 return 是其他任何东西,则从 returned 迭代器开始有重复项,这意味着它们被移动到 after 排序序列的非重复。然后你可以删除那些,例如用 std::vector 你做 v.erase(returnedIterator, v.end()).
#include<iostream>
#include<algorithm>
int main(){
int arr[] = {3, 2, 3, 4, 1, 5, 5, 5};
int len = sizeof(arr) / sizeof(*arr); // Finding length of array
std::sort(arr, arr+len);
int unique_elements = std::unique(arr, arr+len) - arr; // Finding number of unique elements
if(unique_elements == 1) std:: cout << "All elements of this array are Equal\n";
else std::cout << "All elements of this array are not Equal\n";
return 0;
}
您好,我正在尝试检查在任何大小的数组中输入的任何值是否不同。我正在尝试为此代码使用嵌套循环,但无法获得正确的 if 语句来检查数组中的每个值是否不同。如果有任何帮助,我将不胜感激!
for (unsigned i = 0; i < size; i++)
for (unsigned k = i + 1; k < size; k++)
if (arr[i] == arr[k]){
return false;
}
return true;
好的,谢谢你们的帮助,你们的建议奏效了!
第一个 for 循环是错误的。有一个 j
而不是 i
for (unsigned i = 0; i < size; i++)
...
这是一个方法..
//Implement an algorithm “Unique” to check if all elements of a given set of
//integers are distinct.
#include <iostream>
using namespace std;
int main()
{
int arr[10] = {10, 20, 50, 90, 30, 60, 35, 40, 85, 90};
int i, k, origVal = 0, newVal = 0;
for (i = 0; i < 10; i++)
{
origVal = arr[i];
for (k = i+1; k < 10; k++)
{
if (origVal == arr[k])
{
newVal = 1;
break;
}
}
if (newVal ){break;}
}
if (newVal == 1)
{
cout<<"The Array does not contain completely distinct values"<<endl;
}
else
{
cout<< "The Array is distinct"<<endl;
}
system("PAUSE");
return 0;
}
你的代码有两个问题:
1. first loop should have j instead if i.
2. The second loop should start from i+1.
希望对您有所帮助。
为什么不使用惯用的 C++ 结构?
if (std::equal(a1, a1 + sizeof a1 / sizeof *a1, a2))
其中 a1 和 a2 是您的两个数组。
我看到问题得到了更正,只有 i 而不是 i 和 j,这让我认为这是两个数组。我将留下这个答案作为在 C++11 之前的版本中使用 std::equal 的参考。但对于 C+11,请参阅下面的 Jens 解决方案。为此,必须包括 <algorithm>
和 <iterator>
。
A std::set 仅包含唯一元素。
#include <iostream>
#include <set>
int main()
{
int a[] = { 1, 9, 4, 5, 8, 3, 1, 3, 5 };
int b[] = { 1, 9, 4, 5, 8, 3 };
std::set<int> sa(a, a + 9);
std::cout << std::boolalpha << (sa.size() == (sizeof(a)/sizeof(*a))); //all values not different
std::set<int> sb(b, b + 6);
std::cout << std::boolalpha << (sb.size() == (sizeof(b)/sizeof(*b))); //true, all values are different
}
更快更好的方法是使用地图。例如
std::map<int, int> m;
for (int i = 0; i < size; ++i) {
if (m[i] > 0) return false;
m[i]++;
}
return true;
一个衬里看起来像这样
return std::unique(arr, arr + size) == (arr + size);
你能先把 arr
排序吗?
std::sort(std::begin(arr), std::end(arr));
auto pos = std::adjacent_find(std::begin(arr), std::end(arr));
if (pos != std::end(arr))
// we have a duplicate
std::sort( std::begin(arr), std::end(arr) );
auto u = std::unique( std::begin(arr), std::end(arr) );
bool containsDuplicate = u != std::end(arr);
如果您的序列已排序并且您想去除重复项,或者如果您有能力对它或它的副本进行排序,然后删除重复项,那么您可以使用 std::unique标准库算法。
http://en.cppreference.com/w/cpp/algorithm/unique 中有很好的示例,但您需要记住的是,如果将没有重复项的排序序列传递给 std::unique,它会 return 一个迭代器最后一个元素 - 通常是 end().
如果它 return 是其他任何东西,则从 returned 迭代器开始有重复项,这意味着它们被移动到 after 排序序列的非重复。然后你可以删除那些,例如用 std::vector 你做 v.erase(returnedIterator, v.end()).
#include<iostream>
#include<algorithm>
int main(){
int arr[] = {3, 2, 3, 4, 1, 5, 5, 5};
int len = sizeof(arr) / sizeof(*arr); // Finding length of array
std::sort(arr, arr+len);
int unique_elements = std::unique(arr, arr+len) - arr; // Finding number of unique elements
if(unique_elements == 1) std:: cout << "All elements of this array are Equal\n";
else std::cout << "All elements of this array are not Equal\n";
return 0;
}