【发布时间】:2015-06-14 13:34:28
【问题描述】:
我想知道如何在循环中的 map 中插入值。
我在下面的代码中使用了insert(),但这不起作用。
#include<stdio.h>
#include<map>
#include<utility>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, i;
map<char*, int> vote;
char name[20], v;
scanf("%d", &n);
for (i = 0; i<n; ++i)
{
scanf("%s %c", name, &v);
vote.insert(make_pair(name, 0));
vote[name] = 0;
if (v == '+')
vote[name]++;
else
vote[name]--;
printf("%d\n", vote[name]);
printf("Size=%lu\n", vote.size());
}
int score = 0;
for (map<char*, int>::iterator it = vote.begin(); it != vote.end(); ++it)
{
printf("%s%d\n", it->first, it->second);
score += it->second;
}
printf("%d\n", score);
}
}
每次我输入一个新的键(字符串)时,它都会更新前一个。 地图的大小始终为 1。
如何正确地将新元素添加到地图中?
【问题讨论】:
-
把你的代码放在你的帖子中而不是链接它
-
一次一期,链接代码不是特别长
-
饶了我你正义的 BS。仅仅因为原始发帖人不是发帖人而恢复编辑不是正当理由。
-
@πάνταῥεῖ 有代码的问题比没有代码的问题要好。此外,网站政策是代码应该在问题中,而不是外部链接。因此,通过编辑将代码从问题移动到外部站点,您违反了站点政策。你是谁要求一个问题保持原来的样子?如果您不喜欢这个问题,请投票并继续。
-
@πάνταῥεῖ 投反对票,VTC 并继续前进......
标签: c++ algorithm dictionary stl coding-style