Page 1 of 1

[STL] algorithm find

PostPosted: Mon Oct 30, 2017 10:09 am
by Lena
How to find the right value in the container map?

Code: Select all
#include <algorithm>

std::map<String, std::vector<int> > HowManyAndCheck;
//***
std::unique_ptr<TIniFile> FileINI(new TIniFile(path));
String name = FileINI->ReadString(AlliniSection->Strings[i], L"name", "");
item->Data[L"name"] = TValue::From<UnicodeString>(name);
//***

std::vector<int> &values = HowManyAndCheck[name];
values.push_back(1);//how many
values.push_back(0);//check 0/1
//***

std::map<String, std::vector<int> >::iterator ItHowMany;
ItHowMany = find(HowManyAndCheck.begin(), HowManyAndCheck.end(), item->Data[L"name"].AsString());//error


[bcc32 Error] algorithm(30): E2094 'operator==' not implemented in type 'pair<const UnicodeString,vector<int,allocator<int> > >' for arguments of type 'UnicodeString'

Re: [STL] algorithm find

PostPosted: Mon Oct 30, 2017 1:44 pm
by rlebeau
Lena wrote:
Code: Select all
std::map<String, std::vector<int> >::iterator ItHowMany;
ItHowMany = find(HowManyAndCheck.begin(), HowManyAndCheck.end(), item->Data[L"name"].AsString());//error



DON'T use std::find() with std::map. You need to use the std::map::find() member method instead:

Code: Select all
ItHowMany = HowManyAndCheck.find(item->Data[L"name"].AsString());


Lena wrote:[bcc32 Error] algorithm(30): E2094 'operator==' not implemented in type 'pair<const UnicodeString,vector<int,allocator<int> > >' for arguments of type 'UnicodeString'


The value_type of a std::map is a std::pair, and so std::find() is expecting you to pass it a std::pair as input, but you are passing it a UnicodeString instead, hence the error.

std::map::find(), on the other hand, takes the std::map's key_type as input instead, which in this case is UnicodeString.

Re: [STL] algorithm find

PostPosted: Tue Oct 31, 2017 4:59 am
by Lena
Thank You very much!