地图印刷问题stl [英] Map printing problem stl

查看:81
本文介绍了地图印刷问题stl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是stl的新手....我试图获得如下输出但是gettig错误:

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:

#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;
}

推荐答案

尝试

Try
 #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]

如果你有一个过时的 C ++ 编译器然后尝试以下代码:



[update]
If you have an outdated C++ compiler then try the following code:

 #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]


这篇关于地图印刷问题stl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆