In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what is the difference between mem_fun and mem_fun_ref in STL. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
How do you perform the same operation on all objects in the container? The first thing we may think of is to do it in a loop.
For example, there is a class like this:
Class ClxECS {
Public:
Int DoSomething () {
Cout DoSomething ()
However, there are many C++ masters and awesome people will give us a piece of advice, that is: when dealing with the containers in STL, try not to write your own loops.
So, we have to use for_each in the STL algorithm.
First, add the following function:
Int DoSomething (ClxECS * pECS)
{
Return pECS- > DoSomething ()
}
Then we can use for_each to achieve the function we want:
For_each (vECS.begin (), vECS.end (), & DoSomething)
After talking for a long time, it seems to have nothing to do with mem_fun and mem_fun_ref. In fact, all that is said is to lead to mem_fun and mem_fun_ref. When using for_each, what if we don't add the function above?
It's time for mem_fun and mem_fun_ref to make their grand debut. Just use the following line of code:
For_each (vECS.begin (), vECS.end (), mem_fun (& ClxECS::DoSomething))
It is actually the iterator that calls the member function.
Example:
one
List lpw
For_each (lpw.begin (), lpw.end (), mem_fun (& Widget::test)) / / pw- > test ()
two
Vector vw
For_each (vw.begin (), vw.end (), mem_fun_ref (& Widget::test)) / / w.test ()
three
If the member function has parameters: pass in the value, and then bind1st is this
Std::for_each (m_erased.begin (), m_erased.end (), std::bind1st (std::mem_fun (& SocketSet::_replace_with_last), this))
/ / equivalent to this- > _ replace_with_last (iter) / / iter
The difference between the two:
The function and usage of mem_fun_ref is the same as mem_fun, except that:
Use mem_fun_ref when the object entity is stored in the container
Use mem_fun when there is a pointer to the object in the container.
This is the difference between mem_fun and mem_fun_ref in STL. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.