map <char *,int =“”>如何正确使用它?

时间:2016-05-27 12:01:02

标签: c++ dictionary stl stdstring stdmap

代码有什么问题?我不想在地图中插入五个名字,并保留所有唯一名称的计数,最后打印。输入第一个输入后,此代码崩溃。还试过scanf()。无法进一步调试!!

#include<stdio.h>
#include<map>
#include<iostream>
#include<string>

using namespace std;

int main()
{
    map<char*, int> VoteCount;
    map<char*, int>::iterator it;
    char* str;
    int i, count;

    for(i = 1; i < 6; i++)
    {
        cin >> str;
        it = VoteCount.find(str);
        if (it != VoteCount.end()) //if name already present increment the count and insert <name, earlier_count+1>
        {
            count = it->second;
            VoteCount.erase(it);
            VoteCount.insert(pair<char*,int>(str,count+1));

        }
        else //insert the first element <name,count>
        {
            VoteCount.insert(pair<char*,int>(str,1));

        }
    }


    printf("Voting list:\n");
    for (it=VoteCount.begin(); it!=VoteCount.end(); it++)
            cout << it->first << " => " << it->second << '\n';


}

0 个答案:

没有答案