By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

How to store and Display map content (STL Map)

P: 93
It is displaying only first name of first pair and second value of last pair
totally mismatch. Can any one help me ?

Expand|Select|Wrap|Line Numbers
  1. #include<string>
  2. #include<iostream>
  3. #include<map>
  4.  
  5. #define SIZE 100
  6.  
  7. using namespace std;
  8.  
  9. class ObjectStore
  10. {
  11.         map<char *, char *> smap;
  12.  
  13.     public:
  14.  
  15.         void setProperty(char *, char *, char *);
  16.         char* getProperty(char *, char *);
  17.  
  18. };
  19.  
  20. void ObjectStore::setProperty(char *area, char *property,
  21.                                 char* value)
  22. {
  23.         strcat(area, property);
  24.         smap.insert( make_pair( area, value ) );
  25. }
  26.  
  27. char* ObjectStore::getProperty(char* area, char* property)
  28. {
  29.         char *tmp;
  30.         char name[SIZE];
  31.         strcat(name, area);
  32.         strcat(name, property);
  33.         map<char *, char *>::iterator iter = smap.begin();
  34.  
  35.         while(iter != smap.end())
  36.         {
  37.                 cout << " Name : "<<iter->first<<" Value : "<<iter->second;
  38.                 iter++;
  39.         }
  40.         tmp = new char[SIZE];
  41. //      strcpy(tmp, iter->second);
  42.         return tmp;
  43. }
  44.  
  45. int main()
  46. {
  47.         char area[50], property[50], value[50], *tmp;
  48.         int i;
  49.         ObjectStore ost;
  50.         for(i=1;i<4;i++)
  51.         {
  52.                 cout<<" Enter Area, Property and value :"<<endl;
  53.                 cin>>area>>property>>value;
  54.                 ost.setProperty(area, property, value);
  55.                 cout<<" Inserting record number "<<i;
  56.         }
  57.         strcpy(area, "ice");
  58.         strcpy(property, "feels");
  59.         tmp = ost.getProperty("ice", "feels");
  60.         cout<<endl<<endl<<tmp;
  61.         //delete []tmp;
  62.         return 0;
  63. }  
Jun 8 '07 #1
Share this Question
Share on Google+
5 Replies


AdrianH
Expert 100+
P: 1,251
It is displaying only first name of first pair and second value of last pair
totally mismatch. Can any one help me ?

Expand|Select|Wrap|Line Numbers
  1. #include<string>
  2. #include<iostream>
  3. #include<map>
  4.  
  5. #define SIZE 100
  6.  
  7. using namespace std;
  8.  
  9. class ObjectStore
  10. {
  11.         map<char *, char *> smap;
  12.  
  13.     public:
  14.  
  15.         void setProperty(char *, char *, char *);
  16.         char* getProperty(char *, char *);
  17.  
  18. };
  19.  
  20. void ObjectStore::setProperty(char *area, char *property,
  21.                                 char* value)
  22. {
  23.         strcat(area, property);
  24.         smap.insert( make_pair( area, value ) );
  25. }
  26.  
  27. char* ObjectStore::getProperty(char* area, char* property)
  28. {
  29.         char *tmp;
  30.         char name[SIZE];
  31.         strcat(name, area);
  32.         strcat(name, property);
  33.         map<char *, char *>::iterator iter = smap.begin();
  34.  
  35.         while(iter != smap.end())
  36.         {
  37.                 cout << " Name : "<<iter->first<<" Value : "<<iter->second;
  38.                 iter++;
  39.         }
  40.         tmp = new char[SIZE];
  41. //      strcpy(tmp, iter->second);
  42.         return tmp;
  43. }
  44.  
  45. int main()
  46. {
  47.         char area[50], property[50], value[50], *tmp;
  48.         int i;
  49.         ObjectStore ost;
  50.         for(i=1;i<4;i++)
  51.         {
  52.                 cout<<" Enter Area, Property and value :"<<endl;
  53.                 cin>>area>>property>>value;
  54.                 ost.setProperty(area, property, value);
  55.                 cout<<" Inserting record number "<<i;
  56.         }
  57.         strcpy(area, "ice");
  58.         strcpy(property, "feels");
  59.         tmp = ost.getProperty("ice", "feels");
  60.         cout<<endl<<endl<<tmp;
  61.         //delete []tmp;
  62.         return 0;
  63. }  
Yeah, you are using char* but not allocating them. Doing this will make the pointers all point to the same value at the end of your loop. Use strings everywhere instead.


Adrian
Jun 8 '07 #2

P: 93
Yeah, you are using char* but not allocating them. Doing this will make the pointers all point to the same value at the end of your loop. Use strings everywhere instead.


Adrian
If I use strings instead of Char * then, I cannot use strcat() and strcpy()
functions. These functions work only with char *.

Please suggest me the ultimate approach for this.

Regards,
Girish.
Jun 8 '07 #3

AdrianH
Expert 100+
P: 1,251
If I use strings instead of Char * then, I cannot use strcat() and strcpy()
functions. These functions work only with char *.

Please suggest me the ultimate approach for this.

Regards,
Girish.
string equivalents:
Expand|Select|Wrap|Line Numbers
  1. char cstring1[]="Hello ", cstring2[]="there!", cstring3[100];
  2. strcpy(cstring3, cstring1);
  3. strcat(cstring3, cstring2);
  4.  
Expand|Select|Wrap|Line Numbers
  1. string cppstring1 = "Hello ", cppstring2 = "there!", cppstring3;
  2. cppstring3 = cppstring1;
  3. cppstring3 += cppstring2; // or
  4. cppstring3 = cppstring3 + cppstring2;
  5.  

Adrian
Jun 8 '07 #4

P: 93
string equivalents:
Expand|Select|Wrap|Line Numbers
  1. char cstring1[]="Hello ", cstring2[]="there!", cstring3[100];
  2. strcpy(cstring3, cstring1);
  3. strcat(cstring3, cstring2);
  4.  
Expand|Select|Wrap|Line Numbers
  1. string cppstring1 = "Hello ", cppstring2 = "there!", cppstring3;
  2. cppstring3 = cppstring1;
  3. cppstring3 += cppstring2; // or
  4. cppstring3 = cppstring3 + cppstring2;
  5.  

Adrian
It is working fine. Thanks a million.

Girish.
Jun 8 '07 #5

AdrianH
Expert 100+
P: 1,251
It is working fine. Thanks a million.

Girish.
Glad to help.


Adrian
Jun 8 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.