Page 1 of 1

STL pair

PostPosted: Sun May 05, 2019 1:00 am
by Lena
Hello.
How to compile a project created in BCB6 for BCB2007.
Please tell me how to fix the error for BCB2007:
Code: Select all
//cpp
TMMint Sort;
    for(int i=1;i<StringGrid2->RowCount;i++)
    {
        Sort.insert(TMMint::value_type(StrToInt(StringGrid2->Cells[3][i]),StringGrid2->Cells[1][i]));//error
    }


Code: Select all
//h
typedef multimap<int, AnsiString> TMMint;

Could not find a match for 'pair<const int,AnsiString>::pair(AnsiString,AnsiString)'

Re: STL pair

PostPosted: Tue May 07, 2019 2:45 am
by Lena
Error disappeared after:
Code: Select all
   TMMint Sort;
    for(int i=1;i<StringGrid2->RowCount;i++)
   {
     int value = StrToInt(StringGrid2->Cells[3][i]);  // <---
     Sort.insert(TMMint::value_type(value, StringGrid2->Cells[1][i]));// Ok
   }

Re: STL pair

PostPosted: Tue May 07, 2019 11:59 am
by rlebeau
Lena wrote:
Code: Select all
        Sort.insert(TMMint::value_type(StrToInt(StringGrid2->Cells[3][i]),StringGrid2->Cells[1][i]));//error


Could not find a match for 'pair<const int,AnsiString>::pair(AnsiString,AnsiString)'


The code you showed originally is perfectly fine, so I don't know why the compiler would think you are trying to pass an AnsiString in the value_type constructor's 1st parameter when you are clearly passing an int instead. But, you can try using the std::make_pair() function instead of constructing the value_type directly:

Code: Select all
Sort.insert(std::make_pair(StringGrid2->Cells[3][i].ToInt(), StringGrid2->Cells[1][i]));

Re: STL pair

PostPosted: Wed May 08, 2019 2:29 am
by Lena
Thank you veru much!
std:: make_pair works fine.