944,159 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 14153
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 22nd, 2006
0

Help with Map

Expand Post »
I can't seem to make the map template work for me.
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nexes300 is offline Offline
6 posts
since Mar 2006
Mar 22nd, 2006
0

Re: Help with Map

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;
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 22nd, 2006
0

Re: Help with Map

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. #include <map>
  2.  
  3. std::map<int, int> m;
  4.  
  5. int main()
  6. {
  7. m[10] = 1;
  8. }
This doesn't:
C++ Syntax (Toggle Plain Text)
  1. #include <map>
  2.  
  3. std::map<int, int> m;
  4.  
  5. m[10] = 1;
  6.  
  7. int main()
  8. {
  9. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 22nd, 2006
0

Re: Help with Map

Quote originally posted by Narue ...
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. #include <map>
  2.  
  3. std::map<int, int> m;
  4.  
  5. int main()
  6. {
  7. m[10] = 1;
  8. }
This doesn't:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nexes300 is offline Offline
6 posts
since Mar 2006
Mar 22nd, 2006
0

Re: Help with Map

>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??
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 22nd, 2006
0

Re: Help with Map

Quote 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?
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Mar 22nd, 2006
0

Re: Help with Map

>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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 22nd, 2006
0

Re: Help with Map

Quote 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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nexes300 is offline Offline
6 posts
since Mar 2006
Mar 23rd, 2006
0

Re: Help with Map

Bleh, and here I thought I got everything,

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

I think it has something to do with
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nexes300 is offline Offline
6 posts
since Mar 2006
Mar 23rd, 2006
0

Re: Help with Map

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?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Passing a matrix from main function to user defined function.
Next Thread in C++ Forum Timeline: Weird (?) problem using std::list ...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC