Using unordered_map with C++Builder.

Posted by on in Programming

The other day, I have been to the research group meeting on C ++ in Tokyo/Japan.

It is a meeting to discuss "Optimized C++" Books.

In that discussion, we made an interesting discussion of std::unordered_map.

C++Builder can also use std::unordered_map. It's a C++11 So possible.

 

Kariya-san explains "std::unordered_map" in detail.

https://www.slideshare.net/kariya_mitsuru/ss-55842496

But it is Japanese.

 


 

[In case of initialization.]

///
std::unordered_map<String, TButton*> unordered_map1{
	{"Button_1", new TButton(this)},
	{"Button_2", new TButton(this)},
	{"Button_3", new TButton(this)}
};

 


 

[In the case of "for", processing can be done in std::pair.]

///
for (auto b : unordered_map1)
{
	b.second->Name = b.first;
	b.second->Text = b.first;
	int i_{StrToInt(StringReplace(b.first, "Button_", "", TReplaceFlags() << rfReplaceAll) )};
	b.second->Position->X = 10;
	b.second->Position->Y = 10+30*i_;
	b.second->OnClick     = do_add_button;
	this->AddObject(b.second);
}




 

[It can be called and used with "at".]

also possible to add and use "emplace".

Of course, "unordered_map1 [s_] = new TButton (this);" is okay.

////
void __fastcall TForm1::do_add_button(TObject *Sender)
{
	TButton* b_ = static_cast<TButton*>(Sender);
	UnicodeString s_ = "new" + b_->Name;
	unordered_map1.emplace(s_, new TButton(this));
	unordered_map1[s_]->Position->X = 200;
	unordered_map1[s_]->Position->Y = 40;
	unordered_map1[s_]->Name = s_;
    this->AddObject(unordered_map1[s_] );

}


 

 

 

 



About
Gold User, No rank,
Delphi / C++Builder blogger

Comments

Check out more tips and tricks in this development video: