Like Tree1Likes
  • 1 Post By AndrewHunter

Newbie Maps Question

This is a discussion on Newbie Maps Question within the C++ Programming forums, part of the General Programming Boards category; Why does the top code work but the bottom one complains about << with the planets map. << planets.find(1)->second Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    6

    Newbie Maps Question

    Why does the top code work but the bottom one complains about << with the planets map. << planets.find(1)->second

    Code:
    map<char,int> mymap;
    
      mymap['a']=50;
      mymap['b']=100;
      mymap['c']=150;
      mymap['d']=200;
    
      // print content:
      cout << "elements in mymap:" << endl;
      cout << "c => " << mymap.find('c')->second << endl;
    Code:
    	map<int,string> planets;
    	planets[1]="Mercury";
    	planets[2]="Venus";
    	planets[3]="Earth";
    	planets[4]="Mars";
    	planets[5]="Jupiter";
    	planets[6]="Saturn";
    	planets[7]="Uranus";
    	planets[8]="Neptune";
    	planets[9]="Pluto";
    	
     cout << "p => " << planets.find(1)->second << endl;

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    What is the 'complaint' you get exactly?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Both of those compile and run fine for me.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    no operator "<<" matches these operands

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    What, well are you paying by the word to post or something?
    Anyhow it compiles and runs fine, did you include <map> and <iostream>, namespace std; maybe?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Have included them... using vis studio 2010 not my usual software...

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Your forgot to #include<string> on your second example would be my guess.
    hk_mp5kpdw likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Thanks! Sorry I have never written anything in C++(Java Girl)I dont know most of the syntax. But someone wanted this done in C so.... xD

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,509
    Quote Originally Posted by Scotty12 View Post
    Thanks! Sorry I have never written anything in C++(Java Girl)I dont know most of the syntax. But someone wanted this done in C so.... xD
    I hope they wanted it done in C++; because you are not writing C code when you use ">>" or "<<" for input/output.

    Tim S.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    I would certainly hope so too, because the STL isn't C either.....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    On a side note, I would recommend using map::insert() for insertion instead of operator [] for std::maps.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    It would also be a good idea if find succeeds or not. It doesn't throw an exception if it fails, so if it fails, you will try to dereference an invalid iterator and get undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if then else - newbie question
    By Penage in forum C Programming
    Replies: 3
    Last Post: 04-10-2010, 03:23 PM
  2. Question on Maps
    By dpp in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2009, 04:09 AM
  3. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 11:27 AM
  5. Newbie Question
    By Daniel in forum C# Programming
    Replies: 2
    Last Post: 01-18-2003, 09:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21