1

My program is written in C++.

#include <iostream> #include <string> #include <map> using namespace std; class Details { int x; int y; }; typedef std::map<string, Details> Det; Det det; Details::Details(int p, int c) { x = p; y = c; } int main(){ det.clear(); insertNew("test", 1, 2); cout << det["test"] << endl; return 0; } 

I want to print the value of a key with the simplest way. Eg det["test"] fails to compile. How to I print values (1,2) for (x,y) that correspond to key "test"?

3
  • 4
    The above code is full of syntax errors and is not even a valid program. Please give the actual code you couldn't compile. Commented Oct 25, 2011 at 20:18
  • 1
    You're missing two close parentheses at the end of your code. Commented Oct 25, 2011 at 20:19
  • @ybungalobill, we are right. I update my question with actual code Commented Oct 25, 2011 at 20:24

4 Answers 4

3

My best guess is that you have no default or copy constructor in your Obj (you don't have any in the code you posted, but I assume you have one that takes two integers). You've also got a typo in the catalog.insert() line. Here is what worked for me, using your code:

class Obj { public: Obj() {} Obj(int x, int y) : x(x), y(y) {} int x; int y; }; int main (int argc, char ** argv) { std::map<std::string, Obj> catalog; catalog.insert(std::map<std::string, Obj>::value_type("test", Obj(1,2))); std::cout << catalog["test"].x << " " << catalog["test"].y << std::endl; return 0; } 
Sign up to request clarification or add additional context in comments.

3 Comments

Ah you ninja edited me! :) You need to either create an operator << as noted in another answer, or access the x, y members.
Performing operator[] multiple times for the same value can severely degrade performance.
Agreed, as I'm sure converting the same C-string to a std::string over and over does too. The code was just meant to illustrate accessing the map in a way similar to the original question.
2

Create an operator<< for your class Obj and then you can do something like std::cout << catalog["test"]; (I'm assuming that the missing parens in the insert call are just a copy-paste-o).

Comments

1

I've changed a bit your code.

#include <map> #include <iostream> #include <string> using namespace std; class Obj { public: Obj( int in_x, int in_y ) : x( in_x ), y( in_y ) {}; int x; int y; }; int main() { std::map< string, Obj* > catalog; catalog[ "test" ] = new Obj(1,2); for( std::map<string, Obj*>::iterator i=catalog.begin(); i != catalog.end(); ++i ) { cout << "x:" << i->second->x << " y:" << i->second->y << endl; } } 

Comments

0

Given these types:

class Obj { int x; int y; }; std::map<string, Obj> catalog; 

Given a populated catalog object:

for(auto ob = catalog.begin(); ob != catalog.end(); ++ob) { cout << ob->first << " " << ob->second.x << " " << ob->second.y; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.