Foreach loop in C++ equivalent of C#

前端 未结 11 1771
礼貌的吻别
礼貌的吻别 2020-12-13 01:20

How would I convert this code to C++?

string[] strarr = {\"ram\",\"mohan\",\"sita\"};    
foreach(string str in strarr) {
  listbox.items.add(str);
}
         


        
11条回答
  •  臣服心动
    2020-12-13 02:18

    Using boost is the best option as it helps you to provide a neat and concise code, but if you want to stick to STL

    void listbox_add(const char* item, ListBox &lb)
    {
        lb.add(item);
    }
    
    int foo()
    {
        const char* starr[] = {"ram", "mohan", "sita"};
        ListBox listBox;
        std::for_each(starr,
                      starr + sizeof(starr)/sizeof(char*),
                      std::bind2nd(std::ptr_fun(&listbox_add), listBox));
    
    }
    

提交回复
热议问题