Re: stl map error error C2440
swtsvn wrote:
void SN_Input::init_keymap(){
//vector
vector<int>* vec=new vector<int>;
vec->push_back(KEYBOARD_PARAM::LEFT_ARROW);
vec->push_back(DIK_LEFT);
keyMap=new map<int,int>();
keyMap->insert(vec->begin(),vec->end());
Along with the other good advice you've gotten else thread, I just
wanted to add that if you intended to use the second element of the
std::vector pointed to by vec, *vec->end() is not what you want to get
the value of that element.
You may want to consider using std::vector::back for this purpose.
keyMap->insert( std::make_pair(*vec->begin(),vec->back()) );
This might make your intent clearer:
keyMap->insert( std::make_pair(vec->front(), vec->back()) );
Alternatively, you may want to use std::vector::operator[]
keyMap->insert( std::make_pair(*vec->begin(),vec->operator[](1)) );
All of this assumes that you did in fact want DIK_LEFT to have the key
KEYBOARD_PARAM::LEFT_ARROW.
I agree that it's probably not the best idea to use pointers and new
here, but perhaps there is some good reason for it? However, if you
must use pointers it might be better to look into some sort of smart
pointer that will delete whatever was newed when its dtor gets called.
Also, if you're initializing the std::map to the same values each time,
there may be easier ways to accomplish this. I expect this kind of thing
to be even easier with the next standard, but in the mean time, please
consider if one of these examples would make your intent clearer,
(I just picked some constants to show how these can be initialized,
beware magic numbers.)
static const std::pair<int,int> x[] = {
std::make_pair(100,200),
std::make_pair(0,2),
};
static const std::map<int,int> m(x, x+sizeof(x)/sizeof(x[0]) );
Or,
typedef std::map<int,int> MyMap;
static const MyMap::value_type x[] = {
std::make_pair(100,200),
std::make_pair(0,2),
};
static const MyMap m(x, x+sizeof(x)/sizeof(x[0]) );
LR