Вопрос

So, i have two textboxes (defined early) and two vectors:

 std::vector<TCHAR*> v1;
 std::vector<int> v2;

and map:

std::map <TCHAR*, int> m1;
std::map <TCHAR*, int>:: iterator i1;

Map init:

void mapInit()
{
m1[L"one"] = 1;
m1[L"two"] = 2;
m1[L"three"] = 3;
m1[L"four"] = 4;
m1[L"five"] = 5;
m1[L"six"] = 6;
m1[L"seven"] = 7;
m1[L"eight"] = 8;
m1[L"nine"] = 9;
m1[L"ten"] = 10;
 }

I should to get the word "one" from the first textbox and write it to vector 'v1' (textbox must have more words in future, so i can't to do it without vector). After, the program have to find in the map desired value by key-name:

TCHAR *zr = v1.at(0); // v1.at(0) has the word `one` atm
i1 = m1.find(zr); // want to get value `1` by key `one`
int z = i1->second; // and get it completely

I got an debug assertion error "map/set iterator not dereferencable" .. :(

If i will do all the same but will change it to L"one":

TCHAR *zr = L"one"; // 
i1 = m1.find(zr);
int z = i1->second;

so it will works... Why doesn't it work with value by vector? I checked with debugger my value of vector - its 'L"one"' too! But doesn't work anyway...

NOTE: i also tried to find the word without .find() method:

TCHAR *zr = v1.at(0);
int z = m1[zr]; // - now `z` returns a null...

If i will change 'v1.at(0)' to L"one", it will work again

Heeeeeelp :( Why doesn't it work?

=================================================

Yeeeeeeeah now it works! :) Easy fix:

wstring zr = v1.at(0);
int z = m1[zr];

Double happy face ) Thanks to everybody for the help bro-s :)

Это было полезно?

Решение

I suppose that "map/set iterator not dereferencable" is because you are trying to dereference iterator that points to end(), you should check if find actually has found anything.

The reason is probably because your map:

std::map <TCHAR*, int> m1;

keeps as key pointers, their values are different in v1, so it is not able to find it. You might check it if you want to keep your design (iterate all map values and print key values, then do the same with v1), or do it the right way and use std::wstring as was suggested in comments.

Другие советы

Do not use TCHAR* as you map key, it will only cause you pain and headaches. You will have have memory leaks and/or crashes from referring to freed memory.

TCHAR is just a typedef for char if not unicode and wchar_t if unicode.

You can do something like this

typedef std::basic_string<TCHAR> tstring;

then declare your map as

std::map<tstring,int>

Note do not use a pointer to string. You should be able to convert whatever you get back from GetWindowText into a tstring.

You can use the win32 api

tstring window_text(max_window_text,0);
auto ncount = ::GetWindowText(window_hwnd,&window_text[0],window_text.size());
if(ncount){
    window_text.resize(ncount);
} else{
    // handle failure
}

Doing this will save you a lot of pain.

Knowing how to go from TCHAR* to std::string/wstring is very important in being able to write good native C++ programs for Windows. This is something you need to learn, so if you are still having trouble post code and the errors. If you do not learn how to do this, I would advise you to not write your programs in Windows in C++ and instead use something like C#.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top