[STL] algorithm find

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

[STL] algorithm find

Postby Lena » Mon Oct 30, 2017 10:09 am

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'
Lena
BCBJ Master
BCBJ Master
 
Posts: 524
Joined: Sun Feb 06, 2011 1:28 pm

Re: [STL] algorithm find

Postby rlebeau » Mon Oct 30, 2017 1:44 pm

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.
Last edited by rlebeau on Tue Oct 31, 2017 1:19 pm, edited 1 time in total.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1457
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: [STL] algorithm find

Postby Lena » Tue Oct 31, 2017 4:59 am

Thank You very much!
Lena
BCBJ Master
BCBJ Master
 
Posts: 524
Joined: Sun Feb 06, 2011 1:28 pm


Return to Technical

Who is online

Users browsing this forum: No registered users and 12 guests

cron