Map

Sep 6, 2010 at 12:32pm


map<myList,int> m;//where myList is a class


Mylist is a class where it has a pair<char*,char*> alpha_lst and a copy constructor.
I inserted elements in the map by:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
myList obj;//object has a memcopy of memory to alpha_lst.first
map(pair<myList,int>(obj,1));
display(m);
}

void display(map<myList,int> &myMap)
201 {
202         map<myList,int>::iterator iter = myMap.begin();
203
204         while(iter != myMap.end())
205         {
206                
207                 cout<<(*iter).first.alpha_lst.first<<"                   "<<(*iter).second<<endl;
208                 iter++;
209         }
210
211         cout<<endl<<endl;
212 }


However, no value is printed out in the display except for (*iter).second.why?
Thanks in advance.
Last edited on Sep 6, 2010 at 12:32pm
Sep 6, 2010 at 1:00pm
Maybe *(iter).first.alpha_lst.first is empty. Have you checked its length?
Sep 6, 2010 at 4:33pm
no its not empty. I have checked its length as well. I even created a copy constructor to initialize the object being passed.
Sep 6, 2010 at 4:49pm
Have you stepped thru the code with a debugger? Can you confirm the values are duff?
Sep 6, 2010 at 5:08pm
yes i did...k here is the code again:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
map<myList,int> m;//where myList is a class 

              {
                    Tokens Obj;
                    pair<char*,char*> test = Obj.tokenize_func(data);
               }
              cout<<"main: "<<m.size()<<endl;
              displayChar(m);

pair<char*,char*> tokenize_func(char *ptr)
{
 if(strcmp(ptr))
476         {
477                 bool flag;
478                 if (charcmp(ptr,charInit))
479                 {
480                         mem[i] = *ptr;
481                         mem[++i] = '\0';
482                         memcpy(alpha_val.first,mem,strlen(mem)+1);
483
484                 myList mObj;
                        mObj.setData(alpha_val);//creates the dynamic pointer
485                 lst1.push_back(mObj);
486                 flag = mapInsert(mObj);//function to check if the key is already there
487
488                 myList objMap(length);
489                 objMap.setData(alpha_val);//sets the pointer
490
491                 if(!flag)//if key not there then insert to map
492                 {
493                         m.insert(pair<myList,int>(objMap,1));
494                         displayChar(m);
495                         if(m.empty())
496                         {
497                                 cout<<"empty"<<endl;
498                         }
499                         else
500                         {
501                                 cout<<"not empty: "<<m.size()<<endl;
502
503                         }
504                 }
505}
}
506              

Sep 6, 2010 at 8:51pm
I don't quite understand that. What is the declaration of myList. What's the significance of pair<char*,char*>. Why is the parameter to tokenize_func not const?
Sep 7, 2010 at 2:07am
Your code makes no sense. You have braces {} coming from nowhere, as if you're trying to actually define the map and the pair...
Last edited on Sep 7, 2010 at 2:36am
Sep 7, 2010 at 2:15am
You have brackets {} coming from nowhere


Those are actually {braces} not [brackets].

</nitpick>
Sep 7, 2010 at 2:37am
Sorry, fixed. ;)

[/nitpick]
Last edited on Sep 7, 2010 at 2:37am
Sep 7, 2010 at 6:24am
actually the braces were added purposely to make the object go out of scope and destroyed....

I have figured the solution afte4r deep thinking.

The parameter to tokenize function is const read from a file but i was testing many new features and perhaps when i copied the code i didnt have that.

Data is elements read at stdin. This is a design of a cscan lexical analyser. well the problem is that i just had to push a class which has a defined dynamic memory into the list and map and the copy constructor would hence create a copy of the object which remains throughout the program life. This is what i wanted and i guess i have achieved by creating two diff class, one for the list and the other for map.

I apologize if i did not explain well as this was a huge program and i had to figure where to cut and paste to explain. However, i appreciate all your input, thank you guys.
Last edited on Sep 7, 2010 at 6:26am
Topic archived. No new replies allowed.