Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am new in stl....i was trying to get the output like below but gettig errors:
input: 1 2 3 4 4 5 66 66 7 10 10 2 8 4
output: 1 3 5 7 8

only the values of array which is just occured not more than once...thanks in advance

What I have tried:

C++
#include <iostream>
#include
using namespace std;

int main()
{


    map<int,int>mymap;
    for(int i=0;i<10;i++)
    {
        int p;
        cin>>p;
        mymap[p]++;
    }

    for(std::map<int>::iterator it=mymap.begin();it!=mymap.end();it++)

    {
        if(mymap[it]==1)
            cout<<it<<" ";
    }


    //cout << "Hello world!" << endl;
    return 0;
}
Posted
Updated 24-May-16 20:53pm
v2

1 solution

Try
C++
 #include <iostream>
 #include <map>
 using namespace std;

int main()
{
    map<int,int> mymap;
    for(int i=0;i<10;i++)
    {
        int p;
        cin >> p;
        if ( mymap.find(p) != mymap.end())
          ++mymap[p];
        else
          mymap[p] = 1;
    }
    for ( const auto & item : mymap)
    {
      if ( item.second == 1)
      cout << item.first << " ";
    }
    cout << endl;

    return 0;
}


[update]
If you have an outdated C++ compiler then try the following code:
C++
 #include <iostream>
 #include <map>
 using namespace std;

int main()
{
    map<int,int>mymap;
    for(int i=0;i<10;i++)
    {
        int p;
        cin >> p;
        if ( mymap.find(p) != mymap.end())
          ++mymap[p];
        else
          mymap[p] = 1;
    }
    for ( map<int,int>::iterator it = mymap.begin(); it != mymap.end(); ++it)
    {
      if ( it->second == 1)
      cout << it->first << " ";
    }
    cout << endl;

    return 0;
}

[/update]
 
Share this answer
 
v3
Comments
Member 12270086 25-May-16 3:11am    
getting errors
CPallini 25-May-16 4:57am    
Probably because you have an outdated C++ compiler. Please see my updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900