| | |
Help with Map
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 6
Reputation:
Solved Threads: 0
I can't seem to make the map template work for me.
wxDev-C++ gives these errors:
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?
C++ Syntax (Toggle Plain Text)
//#include "PrecedenceXY.h" #include <map> #include <string> using namespace std; map<string, int> normalXPrecedence; map<string, int> normalYPrecedence; normalXPrecedence["x"] = 1; normalXPrecedence["x^2"] = 2; normalXPrecedence["x^3"] = 3; normalXPrecedence["x^4"] = 4; normalXPrecedence["x^5"] = 5; normalXPrecedence["x^6"] = 6; ect, filling up both X and Y lists.
wxDev-C++ gives these errors:
C++ Syntax (Toggle Plain Text)
10 FilepathCut expected constructor, destructor, or type conversion before '=' token 10 FilepathCut expected `,' or `;' before '=' token 11 FilepathCut expected constructor, destructor, or type conversion before '=' token 11 FilepathCut expected `,' or `;' before '=' token 12 FilepathCut expected constructor, destructor, or type conversion before '=' token
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?
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;
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*
C++ Syntax (Toggle Plain Text)
normalXPrecedence["x"] = 1; normalXPrecedence["x^2"] = 2; normalXPrecedence["x^3"] = 3; normalXPrecedence["x^4"] = 4; normalXPrecedence["x^5"] = 5; normalXPrecedence["x^6"] = 6;
C++ Syntax (Toggle Plain Text)
#include <map> std::map<int, int> m; int main() { m[10] = 1; }
C++ Syntax (Toggle Plain Text)
#include <map> std::map<int, int> m; m[10] = 1; int main() { }
New members chased away this month: 3
•
•
Join Date: Mar 2006
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
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)
normalXPrecedence["x"] = 1; normalXPrecedence["x^2"] = 2; normalXPrecedence["x^3"] = 3; normalXPrecedence["x^4"] = 4; normalXPrecedence["x^5"] = 5; normalXPrecedence["x^6"] = 6;
This doesn't:C++ Syntax (Toggle Plain Text)
#include <map> std::map<int, int> m; int main() { m[10] = 1; }
C++ Syntax (Toggle Plain Text)
#include <map> std::map<int, int> m; m[10] = 1; int main() { }
Thanks!
Also, I purposely changed the variable, key, and file names so that I wouldn't have to say what the project was. :p
•
•
•
•
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.
>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:
That saves you the effort of using a function or hardcoded loop to populate the map.
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)
#include <iostream> #include <string> #include <map> #include <utility> template <typename T, int N> char (&array(T(&)[N]))[N]; std::pair<std::string, int> init[] = { std::pair<std::string, int> ( "x", 1 ), std::pair<std::string, int> ( "x^2", 2 ), std::pair<std::string, int> ( "x^3", 3 ), std::pair<std::string, int> ( "x^4", 4 ), std::pair<std::string, int> ( "x^5", 5 ), std::pair<std::string, int> ( "x^6", 6 ) }; std::map<std::string, int> normalXPrecedence ( init, init + sizeof array ( init ) ); int main() { std::map<std::string, int>::iterator it = normalXPrecedence.begin(); while ( it != normalXPrecedence.end() ) { std::cout<< it->first <<" -- "<< it->second <<'\n'; ++it; } }
New members chased away this month: 3
•
•
Join Date: Mar 2006
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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:
That saves you the effort of using a function or hardcoded loop to populate the map.C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <map> #include <utility> template <typename T, int N> char (&array(T(&)[N]))[N]; std::pair<std::string, int> init[] = { std::pair<std::string, int> ( "x", 1 ), std::pair<std::string, int> ( "x^2", 2 ), std::pair<std::string, int> ( "x^3", 3 ), std::pair<std::string, int> ( "x^4", 4 ), std::pair<std::string, int> ( "x^5", 5 ), std::pair<std::string, int> ( "x^6", 6 ) }; std::map<std::string, int> normalXPrecedence ( init, init + sizeof array ( init ) ); int main() { std::map<std::string, int>::iterator it = normalXPrecedence.begin(); while ( it != normalXPrecedence.end() ) { std::cout<< it->first <<" -- "<< it->second <<'\n'; ++it; } }
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.
•
•
Join Date: Mar 2006
Posts: 6
Reputation:
Solved Threads: 0
Bleh, and here I thought I got everything,
Could you explain what this is?
from
I think it has something to do with
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.
Could you explain what this is?
C++ Syntax (Toggle Plain Text)
init + sizeof array ( init )
C++ Syntax (Toggle Plain Text)
std::map<std::string, int> normalXPrecedence ( init, init + sizeof array ( init ) );
I think it has something to do with
C++ Syntax (Toggle Plain Text)
template <typename T, int N> char (&array(T(&)[N]))[N];
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.
![]() |
Similar Threads
- Recursive Hash map - unhandled exception help (C)
- Using map<> with classes (C++)
- Looking for advice on creating a Virtual Map (Java)
- sorting stl::map (C++)
- connection between linkbutton and map (HTML and CSS)
- Add a Map Drive Button to the Toolbar (Windows tips 'n' tweaks)
Other Threads in the C++ Forum
- Previous Thread: Passing a matrix from main function to user defined function.
- Next Thread: Weird (?) problem using std::list ...
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int java lib library lines linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






