c++ - How to insert values in STL map within a loop -


i wanted know how 1 can insert values in map within loop. had used insert() in following code did not worked.

#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);         (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;         (map<char*, int>::iterator = vote.begin(); != vote.end(); ++it)         {             printf("%s%d\n", it->first, it->second);             score += it->second;         }         printf("%d\n", score);      } } 

everytime type new key(string) updates previous one. size of map 1.

how correctly add new element map?

the map keyed pointer (char*). key in code same 1 - name pointer (although change content pointer points on, not change fact pointer not same).

you can use std::string key instead of char*.

changing definition of map (replace char* in std::string) fix problem.

edit: @mcnabb said, change it->first it->first.c_str().


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -