如何检查数组中的所有值是否不同
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;
好的,谢谢你们的帮助,你们的建议起作用了!
可以先排序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
第一个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.
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
}
一个更快更好的方法是使用map。例如
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);
如果你的序列已经排序了,你想去掉重复的部分,或者你有能力对它或它的一个副本进行排序,然后去掉重复的部分,那么你可以使用std::unique标准库算法。
在http://en.cppreference.com/w/cpp/algorithm/unique上有很好的例子,但您只需要记住,如果将一个没有重复项的排序序列传递给std::unique,它将返回一个指向最后一个元素的迭代器——通常是end()。
如果返回其他内容,则从返回的迭代器开始有重复项,这意味着它们被移动到之后的非重复项排序序列。然后,您可以删除这些,例如,对于std::vector,您执行v.r ease (returnedIterator, v.p end())。
为什么不使用惯用的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>
。
std::sort( std::begin(arr), std::end(arr) );
auto u = std::unique( std::begin(arr), std::end(arr) );
bool containsDuplicate = u != std::end(arr);
#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 Equaln";
else std::cout << "All elements of this array are not Equaln";
return 0;
}
相关文章:
- 是否可以检查悬挂光纤的调用堆栈?
- 从双精度转换为整数的显式类型是否始终检查整数溢出?
- 是否可以检查变量是否位于 L1/L2/L3 缓存中
- 是否可以检查给定exception_ptr实例是否拥有某种类型的异常?
- 要检查 SetFilePointerEx、ReadFile 或 WriteFile 是否成功,我是否需要检查 lpNew
- 是否可以检查存储在堆栈上的单词是否是回文,而C++中没有任何附加数据结构
- 在尝试擦除元素之前,我们是否需要检查unordered_set是否包含元素
- std::迭代器是否可以检查该迭代器上是否存在下一个元素?(此设计受到家庭作业的限制)
- 标准库是否提供检查双精度值是否与整数相同的函数
- 我是否必须检查此方法的指针有效性
- 是否可以检查接口是否在 pcap 中激活
- 是否可以检查是否为类定义了成员函数,即使该成员继承自未知基类
- 是否可以检查串口是空闲还是占用
- wxwidgets中是否有检查pdf软件可用的功能
- try_lock()+unlock()是否是检查boost::interprocess::file_lock是否已锁定的
- 是否可以检查是否为给定类型和参数定义了用户字面值
- 是否可以检查以字符串作为其名称的函数是否存在?(c++)
- 是否支持检查mmap
- 是否可以检查函数是否具有 void 返回类型
- 是否可以检查变量的存在性