Help with Map

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 6
Reputation: nexes300 is an unknown quantity at this point 
Solved Threads: 0
nexes300 nexes300 is offline Offline
Newbie Poster

Help with Map

 
0
  #1
Mar 22nd, 2006
I can't seem to make the map template work for me.
  1. //#include "PrecedenceXY.h"
  2. #include <map>
  3. #include <string>
  4. using namespace std;
  5.  
  6. map<string, int> normalXPrecedence;
  7. map<string, int> normalYPrecedence;
  8.  
  9.  
  10. normalXPrecedence["x"] = 1;
  11. normalXPrecedence["x^2"] = 2;
  12. normalXPrecedence["x^3"] = 3;
  13. normalXPrecedence["x^4"] = 4;
  14. normalXPrecedence["x^5"] = 5;
  15. normalXPrecedence["x^6"] = 6;
  16. ect, filling up both X and Y lists.

wxDev-C++ gives these errors:
  1. 10 FilepathCut expected constructor, destructor, or type conversion before '=' token
  2. 10 FilepathCut expected `,' or `;' before '=' token
  3. 11 FilepathCut expected constructor, destructor, or type conversion before '=' token
  4. 11 FilepathCut expected `,' or `;' before '=' token
  5. 12 FilepathCut expected constructor, destructor, or type conversion before '=' token
and so on for every single time I try to add something into the list.

So, could someone tell me what Im doing wrong? I tried to initialize the map in a variety of ways like:
map<string, int> normalXPrecedence = map<string, int>::map();
map<string, int> normalXPrecedence = map();
But nothing works *sigh* Can anyone help me?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Help with Map

 
0
  #2
Mar 22nd, 2006
Hope these help

http://www.cppreference.com/cppmap/index.html
http://www.cprogramming.com/tutorial/stl/stlmap.html


May I ask what you are trying to do here? What kind of project is this?

normalXPrecedence["x"] = 1;
normalXPrecedence["x^2"]= 2;
normalXPrecedence["x^3"]= 3;
normalXPrecedence["x^4"] = 4;
normalXPrecedence["x^5"] = 5;
normalXPrecedence["x^6"] = 6;
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,823
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 748
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Help with Map

 
0
  #3
Mar 22nd, 2006
  1. normalXPrecedence["x"] = 1;
  2. normalXPrecedence["x^2"] = 2;
  3. normalXPrecedence["x^3"] = 3;
  4. normalXPrecedence["x^4"] = 4;
  5. normalXPrecedence["x^5"] = 5;
  6. normalXPrecedence["x^6"] = 6;
Are these lines actually in the global scope, or are you summarizing by removing the function that they reside in? Only declarations and definitions are allowed in the global scope, and assignment to an existing object isn't either. This works:
  1. #include <map>
  2.  
  3. std::map<int, int> m;
  4.  
  5. int main()
  6. {
  7. m[10] = 1;
  8. }
This doesn't:
  1. #include <map>
  2.  
  3. std::map<int, int> m;
  4.  
  5. m[10] = 1;
  6.  
  7. int main()
  8. {
  9. }
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 6
Reputation: nexes300 is an unknown quantity at this point 
Solved Threads: 0
nexes300 nexes300 is offline Offline
Newbie Poster

Re: Help with Map

 
0
  #4
Mar 22nd, 2006
Originally Posted by Narue
  1. normalXPrecedence["x"] = 1;
  2. normalXPrecedence["x^2"] = 2;
  3. normalXPrecedence["x^3"] = 3;
  4. normalXPrecedence["x^4"] = 4;
  5. normalXPrecedence["x^5"] = 5;
  6. normalXPrecedence["x^6"] = 6;
Are these lines actually in the global scope, or are you summarizing by removing the function that they reside in? Only declarations and definitions are allowed in the global scope, and assignment to an existing object isn't either. This works:
  1. #include <map>
  2.  
  3. std::map<int, int> m;
  4.  
  5. int main()
  6. {
  7. m[10] = 1;
  8. }
This doesn't:
  1. #include <map>
  2.  
  3. std::map<int, int> m;
  4.  
  5. m[10] = 1;
  6.  
  7. int main()
  8. {
  9. }
I see, that would make sense I guess...*scratch* Now Im going to have to find some way to initialize them I guess. *sigh* Function calls....unecessary function calls....ugly.

Thanks!

Also, I purposely changed the variable, key, and file names so that I wouldn't have to say what the project was. :p
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Help with Map

 
0
  #5
Mar 22nd, 2006
>Also, I purposely changed the variable, key, and file names so that I wouldn't have to say what the project was.

Oh I want to know what the project is...

Please tell us??
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 492
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Help with Map

 
0
  #6
Mar 22nd, 2006
Originally Posted by nexes300
I see, that would make sense I guess...*scratch* Now Im going to have to find some way to initialize them I guess. *sigh* Function calls....unecessary function calls....ugly.
I assume that this is some kind of a lookup table? How about wrapping your map inside a class, and using the constructor to initialise it?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,823
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 748
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Help with Map

 
0
  #7
Mar 22nd, 2006
>Now Im going to have to find some way to initialize them I guess.
You can initialize an array of pairs, and then use one of the std::map constructors with the array to initialize normalXPrecedence:
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <utility>
  5.  
  6. template <typename T, int N>
  7. char (&array(T(&)[N]))[N];
  8.  
  9. std::pair<std::string, int> init[] = {
  10. std::pair<std::string, int> ( "x", 1 ),
  11. std::pair<std::string, int> ( "x^2", 2 ),
  12. std::pair<std::string, int> ( "x^3", 3 ),
  13. std::pair<std::string, int> ( "x^4", 4 ),
  14. std::pair<std::string, int> ( "x^5", 5 ),
  15. std::pair<std::string, int> ( "x^6", 6 )
  16. };
  17.  
  18. std::map<std::string, int> normalXPrecedence ( init, init + sizeof array ( init ) );
  19.  
  20. int main()
  21. {
  22. std::map<std::string, int>::iterator it = normalXPrecedence.begin();
  23.  
  24. while ( it != normalXPrecedence.end() ) {
  25. std::cout<< it->first <<" -- "<< it->second <<'\n';
  26. ++it;
  27. }
  28. }
That saves you the effort of using a function or hardcoded loop to populate the map.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 6
Reputation: nexes300 is an unknown quantity at this point 
Solved Threads: 0
nexes300 nexes300 is offline Offline
Newbie Poster

Re: Help with Map

 
0
  #8
Mar 22nd, 2006
Originally Posted by Narue
>Now Im going to have to find some way to initialize them I guess.
You can initialize an array of pairs, and then use one of the std::map constructors with the array to initialize normalXPrecedence:
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <utility>
  5.  
  6. template <typename T, int N>
  7. char (&array(T(&)[N]))[N];
  8.  
  9. std::pair<std::string, int> init[] = {
  10. std::pair<std::string, int> ( "x", 1 ),
  11. std::pair<std::string, int> ( "x^2", 2 ),
  12. std::pair<std::string, int> ( "x^3", 3 ),
  13. std::pair<std::string, int> ( "x^4", 4 ),
  14. std::pair<std::string, int> ( "x^5", 5 ),
  15. std::pair<std::string, int> ( "x^6", 6 )
  16. };
  17.  
  18. std::map<std::string, int> normalXPrecedence ( init, init + sizeof array ( init ) );
  19.  
  20. int main()
  21. {
  22. std::map<std::string, int>::iterator it = normalXPrecedence.begin();
  23.  
  24. while ( it != normalXPrecedence.end() ) {
  25. std::cout<< it->first <<" -- "<< it->second <<'\n';
  26. ++it;
  27. }
  28. }
That saves you the effort of using a function or hardcoded loop to populate the map.
Ooer! Awesome! Thanks!

Ok ok, I'll tell you what it's for. It's a table to store strings, and the number represents the precedence of that string in relation to the others in that list. I can then use parts of that list in another list and sort them based on the int they're mapped to. I know I could have used an enumeration or all kinds of other things, but I wanted to use a map because a map allows me to change the ints at run time and I want to leave that option open instead of using Macroed definitions or something.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 6
Reputation: nexes300 is an unknown quantity at this point 
Solved Threads: 0
nexes300 nexes300 is offline Offline
Newbie Poster

Re: Help with Map

 
0
  #9
Mar 23rd, 2006
Bleh, and here I thought I got everything,

Could you explain what this is?
  1. init + sizeof array ( init )
from
  1. std::map<std::string, int> normalXPrecedence ( init, init + sizeof array ( init ) );

I think it has something to do with
  1. template <typename T, int N>
  2. char (&array(T(&)[N]))[N];
but Im not quite sure, since I don't see the <> to intialize that template or anything.

Actually nevermind, Im just going to move it into a class, requires less rewriting. *grumble* Kind of annoying though, I didn't really want another class just for this.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Help with Map

 
0
  #10
Mar 23rd, 2006
Can I just ask does this have anything to do with sorting polynomials into their order of precedence or is the:
x
x^2
x^3

just a decoy, masking sumthing else?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC