简体   繁体   中英

Given n positive integer numbers.Print how many numbers occurs at least 2 times in this array on C++

Input: 4 1 2 2 3 Output: 1

Hello guys. There is one problem..,I planned to make this through a map. but...

`#include <iostream>
#include <map>

using namespace std;

int main(){
    int n;
    cin >> n;
    int arr[n];
    map<int,int> m;
    map<int,int> :: iterator it = m.begin();
    for(int i = 0;i < n;i++){
        cin >> arr[i];
        it = m.find(arr[i]);
        if(*it == m.size());
    }
    return 0;
}`

ProblemC.cpp:15:26: note: 'std::pair<const int, int>' is not derived from 'const std::multimap<_Key, _Tp, _Compare, _Alloc>' if(*it == m.size()); ^

I don't even know how to properly solve this, please help me Here is a photo with more details

I am assuming that in if statement you want to check if find algorithm returned end of map. Better way to do it is to try to insert value into map and check operation return value. You should change it to:

if (!m.insert({arr[i], 1}).second)
{
    counter++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM