-
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;
}
-
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?
-
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!! :)
-
>> 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
-
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..
-
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;
}
-
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++
-
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>..
-
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?).
-
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.