STL pair

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

STL pair

Postby Lena » Sun May 05, 2019 1:00 am

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

Re: STL pair

Postby Lena » Tue May 07, 2019 2:45 am

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

Re: STL pair

Postby rlebeau » Tue May 07, 2019 11:59 am

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]));
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1588
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: STL pair

Postby Lena » Wed May 08, 2019 2:29 am

Thank you veru much!
std:: make_pair works fine.
Lena
BCBJ Master
BCBJ Master
 
Posts: 601
Joined: Sun Feb 06, 2011 1:28 pm


Return to Technical

Who is online

Users browsing this forum: No registered users and 13 guests

cron