C++ 如何计算c++;?

C++ 如何计算c++;?,c++,arrays,C++,Arrays,我如何计算数组中的相同元素,例如,如果我有一个像这样的arr[5]={1,2,3,4,5}的5整数数组输出应该是:0,因为没有重复的元素,但是如果我有arr[5]={1,1,2,3,5}输出应为:2,因为1重复了2次,依此类推。我试图用下面的代码来解决这个问题: #include <iostream> using namespace std; int main(){ int arr[5] = { 1, 1, 2, 3, 4}; int cnt = 0; f

我如何计算数组中的相同元素,例如,如果我有一个像这样的
arr[5]={1,2,3,4,5}的
5
整数数组输出应该是:
0
,因为没有重复的元素,但是如果我有
arr[5]={1,1,2,3,5}输出应为:
2
,因为
1
重复了
2
次,依此类推。我试图用下面的代码来解决这个问题:

#include <iostream>

using namespace std;
int main(){
    int arr[5] = { 1, 1, 2, 3, 4};
    int cnt = 0;

    for (int i = 0; i < 5; i++){
     for (int j = i + 1; j < 5; j++){
        if (arr[i] == arr[j]){
            cnt++;
        }

     }

    }
     cout << cnt << endl;

    return 0;
}
预期输出为
4
,但当前输出为
6


这段代码有什么问题?
有时输出预期的结果,有时不输出

代码运行良好,让我演示一下如何:

arr[5]={1,1,2,3,4}

i=0=>a[i]=1将与j=1(a[j]=1)匹配

因此,计数为1

arr[5]={1,1,2,1,1}

i=0=>a[i]=1将与j=1、3、4=>count=3匹配

i=1将与j=3匹配,4=>count=5

i=3将与j=4=>count=6匹配

根据您编写的代码,这是您得到的输出;这是完全正确的。错误在于你的逻辑

这是正确的逻辑:

#include <iostream>
#include <map>

int main(){
    int arr[5] = { 1, 1, 2, 1, 1};
    int cnt = 0;

    std::map<int, int> mp;

    for (int i = 0; i < 5; i++){
     mp[arr[i]]++;
    }
    for(auto it=mp.begin(); it!=mp.end(); ++it)
    {
        if(it->second==1)
            continue;
        else
            cnt +=it->second;
    }
     std::cout << cnt << "\n";

    return 0;
}
#包括
#包括
int main(){
int-arr[5]={1,1,2,1,1};
int-cnt=0;
std::map-mp;
对于(int i=0;i<5;i++){
mp[arr[i]]++;
}
for(auto it=mp.begin();it!=mp.end();+it)
{
如果(它->秒==1)
继续;
其他的
cnt+=it->second;
}

您可以使用标准模板库(STL)的一些函数。
std::sort()
对数组进行排序,这有助于使用
std::unique()
计算重复次数,因为它需要一个已排序的容器

首先对数组进行排序,然后在
std::unique()时使用比较器计算重复次数
从数组中删除重复。代码可以存储重复的唯一元素的计数,并对删除的元素进行计数。因此,最后,您可以将删除元素的计数与具有重复的唯一元素的计数相加

我的例子如下:

#include <iostream>
#include <algorithm>
#include <map>

int main(int argc, const char * argv[])
{
    const int32_t SIZE = 7;
    int32_t ArrInput[SIZE]{1, 3, 4, 1, 1, 5, 4};

    std::sort(ArrInput, ArrInput+SIZE);

    int32_t CountRemovedRepetitions = 0;
    std::map<int32_t, int32_t> MapAllUniqueRepetition;

    std::unique(ArrInput, ArrInput+SIZE,
        [&MapAllUniqueRepetition, &CountRemovedRepetitions](const int32_t v1, const int32_t v2) {
            if (v1 == v2) {
                MapAllUniqueRepetition[v1]++;
                CountRemovedRepetitions++;
                return true;
            }
            return false;
        }
    );

    int32_t CountTotalRepetitions = MapAllUniqueRepetition.size() + CountRemovedRepetitions;
    std::cout << "Count removed repetitions:" << CountRemovedRepetitions << " and count total repetiotions: " << CountTotalRepetitions << std::endl;

    return 0;
}
#包括
#包括
#包括
int main(int argc,const char*argv[]
{
常数int32_t SIZE=7;
int32_t ArrInput[SIZE]{1,3,4,1,1,5,4};
标准::排序(ArrInput,ArrInput+大小);
int32_t CountRemovedRepeations=0;
地图地图查询竞争;
标准::唯一(ArrInput,ArrInput+大小,
[&MAPALLUNIQUEREPITION,&CountRemovedRepeations](常量int32_t v1,常量int32_t v2){
如果(v1==v2){
MAPALLUNIQUEREPITION[v1]+;
countremovedrepeations++;
返回true;
}
返回false;
}
);
int32_t CountTotalRepetitions=MapalUniquerPetation.size()+CountRemovedRepetitions;

std::cout映射是将唯一键映射到非唯一值。每次我们在数组中找到一个值,您只需增加看到它的次数(值)

#包括
#包括
#包括
使用名称空间std;
int main(){
向量a={1,1,2,1,1};
//关联的关联数组
//键(数组中的值)和值,
//(找到给定密钥的次数)
地图m;
int n=a.size();
//重复的总数,我们开始
//在0处,我们将添加所有值
//在我们的地图上超过1。
整数和=0;
//在地图上迭代
对它进行迭代器;
//循环通过数组/向量
对于(int i=0;i秒>1){
sum+=it->second;
}
}
//找到的重复项总数

不欢迎使用堆栈溢出!听起来您可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,您可以逐行执行程序,并查看它与预期的偏差。如果您要进行任何编程,这是一个必不可少的工具。进一步阅读:代码运行良好,您的l逻辑错误您计算的是一个数字被复制的次数,而不是重复数字的实例数。这不是一回事。这是一个很好的例子,有助于“通过”写在纸上的代码。写下数组,遍历循环,并在
cnt
增加时写下。它应该会告诉你为什么上一个示例以
6
而不是
4
@AbhishekKeshri结尾。我知道我的逻辑是错误的,这就是为什么我发布这个问题。你能解释一下map的作用吗?map就像一个associative数组中,数组中的索引只能是0、1、2、3、4…但在映射中,索引可以是任何内容-字符、字符串、浮点、结构、类、模板等。
#include <iostream>
#include <algorithm>
#include <map>

int main(int argc, const char * argv[])
{
    const int32_t SIZE = 7;
    int32_t ArrInput[SIZE]{1, 3, 4, 1, 1, 5, 4};

    std::sort(ArrInput, ArrInput+SIZE);

    int32_t CountRemovedRepetitions = 0;
    std::map<int32_t, int32_t> MapAllUniqueRepetition;

    std::unique(ArrInput, ArrInput+SIZE,
        [&MapAllUniqueRepetition, &CountRemovedRepetitions](const int32_t v1, const int32_t v2) {
            if (v1 == v2) {
                MapAllUniqueRepetition[v1]++;
                CountRemovedRepetitions++;
                return true;
            }
            return false;
        }
    );

    int32_t CountTotalRepetitions = MapAllUniqueRepetition.size() + CountRemovedRepetitions;
    std::cout << "Count removed repetitions:" << CountRemovedRepetitions << " and count total repetiotions: " << CountTotalRepetitions << std::endl;

    return 0;
}
Count removed repetitions:3 and count total repetiotions: 5 Program ended with exit code: 0
#include <map>
#include <vector>
#include <iostream>

using namespace std;

int main() {

  vector<int>   a = {1, 1, 2, 1, 1};

  // Associative array that associates
  // keys (values from the array) and values,
  // (the number of times we find a given key)
  map<int, int> m;
  int           n = a.size();

  // Total number of duplicates, we start
  // at zero, and we will add all the values
  // that exceed 1 in our map.
  int           sum = 0;

  // Iterates over map
  map<string, int>::iterator it;

  // Loop through array / vector
  for (int i = 0; i < n; i++) {

    // Add 1 to the value associated
    // with the key a[i]
    m[ a[i] ] += 1;

  }

  // Loop through the map
  for (auto it=m.begin(); it!=m.end(); ++it) {

    // If a given value was found
    // more than once, add the number of
    // times we found it to the running sum
    if (it->second > 1) {
      sum += it->second;
    }

  }

  // Overall sum of duplicates found
  cout << sum << "\n";

  return 0;
}