STL - map

This is a discussion on STL - map within the C++ Programming forums, part of the General Programming Boards category; This code doesnīt work! The value of values[tempS] is always 0. What is the problem?? Code: #include <map.h> #include <iostream.h> ...

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    STL - map

    This code doesnīt work! The value of values[tempS] is always 0.
    What is the problem??

    Code:
    #include <map.h>
    #include <iostream.h>
    
    int main(void){
    
      typedef std::map< char *, int, std::less<char *> > mapping;
    
      mapping values;
    
      values.insert( mapping::value_type("num1", 5 ));
      values.insert( mapping::value_type("num2", 15 ));
    
      char tempS[5];
      int tempI;
    
      cout << "Insert a string corresponding to a number:\n" ;
      cin.getline( tempS , 5 );
      cout << "Insert an int to be added:\n" ;
      cin >> tempI;
    
      cout << values[tempS] << " + " << tempI;
      tempI+=values[tempS];
      cout << " = " << tempI << endl;
      
      /*two cin.get to hold the screen, 
      donīt know better way*/
      cin.get(); 
      cin.get();
      
      return 0;
    }
    Nothing more to tell about me...
    Happy day =)

  2. #2
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    Code:
    #include <iostream>	//remove .h
    #include <map>
    using namespace std;	//always usefull
    
    int main()
    {
          map< char*, int> values;	// sorry i dont know what is less so i just removed it..
    
          values["num1"] = 5;	//this is a better way to initialize values
          values["num2"] = 15;
    
          char tempS[5] = "null";//either use pointer or array not both or u will get an array of char pointer..
          int tempI = 0;
    
          cout << "Insert a string corresponding to a number:"<< endl ;
          cin >> tempS;	//i removed getline cause i didnt tought u needed it for a char array of
    		    //only 5 char but u can always put it back..
          cout << "Insert an int to be added:"<< endl ;
          cin >> tempI;
          values[tempS] = tempI;	//thats why it always was 0 u forgot to put it in
          cout << values[tempS] << " + " << tempI;
          tempI+=values[tempS];
          cout << " = " << tempI << endl;
          return 0;
    }
    like i said i dunno what is less so could u explain it to me?
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Some notes:

    >like i said i dunno what is less so could u explain it to me?

    I copied this code from C++ how to program, and I read that less<class T> would be used for sorting. You misunderstood the program(my fault, I didnīt explain nothing, sorry). But the user would input "num1" or "num2" to get a number from the map. This was a solution to another problem from this forum. Anyway, what return 0 in my original program is values["num1"].
    I agree with your changes, and I tested your code, but I get the same error.
    Here is my console window at the end of the program(just a note, I removed the line: values[tempS] = tempI, because I need the value stored before in the map)

    Insert a string corresponding to a number:
    num1
    Insert an int to be added:
    6
    0 + 6 = 6

    Now, why is that the line: values["num1"] = 5 have no effect here?

    Thanks for the tips about my code!!
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    >> I removed the line: values[tempS] = tempI, because I need the value stored before in the map

    If u remove that line u will always get 0 :

    ...
    Code:
    tempI+=values[tempS]
    lets say tempI = 10 and tempS = "bob"

    so ur trying to addition 10 plus the associated value of bob..

    but since u never inserted bob in the map it will always return 0+10..

    >> Now, why is that the line: values["num1"] = 5 have no effect here?

    because ur not using it!
    But if what u really want to do is add tempI to num1..
    what u should do is :

    Code:
          values[tempS] = tempI;
          cout << values[tempS] << " + " << tempI;
          values[num1]+=values[tempS];
          cout << " = " << tempI << endl;
    but if u do not want to store tempS do like this:
    (if ur not gonna store the value of tempS in the map why asking a name for it?)

    Code:
          cout << values[tempS] << " + " << tempI;
          tempI+=values[num1];
          cout << " = " << tempI << endl;
    unless I got what u really wanna do wrong once again..
    hope that helped
    Last edited by Luigi; 05-04-2003 at 06:56 PM.
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  5. #5
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    opps i just red ur post again what u want to do is :
    user input either num1 or num2 then a number to add to it?
    if thats what u want second option should work..
    but ill check it out just in case ill be back to u with an answer later..
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  6. #6
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    Ok dont ask why but this will work..
    If u replace cstring by c++ string all goes fine..
    anyways i almost always use c++ string..

    Code:
    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    
    int main()
    {
          map< string, int> values;
          values["num1"] = 5;
          values["num2"] = 15;
    
          char tempS[5];
          int tempI = 0;
    
          cout << "Insert a string corresponding to a number:"<< endl ;
          cin >> tempS;
          cout << "Insert an int to be added:"<< endl ;
          cin >> tempI;
          
          cout << values[tempS] << " + " << tempI << " = " << (tempI+=values[tempS]) << endl;
          return 0;
    }
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  7. #7
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Unhappy

    Iīm so unhappy! None of the compilers that I have compiled the code.
    Using Borland C++, I get an error message that says that map without that inspid less<T> have to few arguments. And putting less<string> I get another error: Illegal struct operation. Damn! I hate these compilers. Anyway, I have here posted the definition of my compilerīs code to the less struct(??):
    Code:
    template <class T>
    struct less : public binary_function<T, T, bool>
    {
        bool operator() (const T& x, const T& y) const { return x < y; }
    };
    As you can see, there is no reason to get any errors:
    map< string, int, less <string> > values;
    *sigh*... I thank your help! Iīll try to find another compiler to get this right...
    Just for note, compiler are:
    Turbo C++(in worst case)
    Dev C++
    Borland C++
    Nothing more to tell about me...
    Happy day =)

  8. #8
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    If u use less only to sort values within the map its kinda useless.. cause:
    values within maps and multimaps are already sorted smallest value first..
    same for <set> and <multiset>..
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  9. #9
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    But...

    But to insert an element in the correct position, you must search the right position, comparing the value to be inserted with the others that are already in the map/set. Correct? But good that you mentioned the fact that they are already sorted. I read this today, but forgot(incredible isnīt?).
    Nothing more to tell about me...
    Happy day =)

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Maps with char* as key won't work without a custom sorter because less<> only compares pointer values in this case. The std::string class however provides the < operator necessary for less<> to work properly.

    The third template parameter to std::map is a function object type that is used for sorting the keys and deciding where to put them. The parameter should default to less<>, so if the borland compiler complains about too few parameters it's probably a compiler or std library bug.

    If less<string> doesn't work it's probably a bug too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Problem with static STL i.e. map
    By vijay_choudhari in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2007, 05:56 AM
  3. STL Map Object
    By nomes in forum C++ Programming
    Replies: 6
    Last Post: 09-11-2003, 01:51 PM
  4. Unsorted Map and Multimap :: STL
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2002, 10:22 PM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 08:11 AM

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