简体   繁体   中英

C++11 auto transform

i have this macro

#define DO_ALL_BLEND_INFO(iter) for (iter=s_blend_info.begin(); iter!=s_blend_info.end(); ++iter)

And this is the main function..

BLEND_ITEM_INFO* blend_info;
T_BLEND_ITEM_INFO::iterator iter;

DO_ALL_BLEND_INFO (iter)
{
    blend_info = *iter;
    if (blend_info->item_vnum == item->GetVnum())
    {

    }
}

What i want to do it's to get rid of this macro and to use auto. But i can't understand how to use auto here... I want to remove that macro, but what if i add auto in macro directly ?

The following, and nothing else:

for (const auto &blend_info:s_blend_info)
{
    if (blend_info->item_vnum == item->GetVnum())
    {

    }
}

And I mean, "nothing else" except that. No macros, no declaration of any iteration. No declaration of blend_info , the auto range iteration does it for you.

Here is another approach. I modify to code and replace s_blend_info with vector<int> to get a compilable solution.

 int main()
 {
vector<int> s_blend_info;
int  blend_info;
vector<int>::iterator iter;
int item = 10;
for (auto iter = s_blend_info.begin(); iter != s_blend_info.end(); ++iter)
{
    blend_info = *iter;
    if (blend_info == item)
    {

    }
}
for ( auto &iter  : s_blend_info)
{
    if (blend_info == item)
    {

    }
}
DO_ALL_BLEND_INFO_GENERIC(s_blend_info, iter)
{
    blend_info = *iter;
    if (blend_info == item)
    {

    }
}
}

#define DO_ALL_BLEND_INFO(iter) for (iter=s_blend_info.begin(); iter!=s_blend_info.end(); ++iter)
#define DO_ALL_BLEND_INFO_GENERIC(container,iterator)  for (iterator=container.begin(); iterator!=container.end(); ++iterator)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM