Foreach loop in C++ equivalent of C#

前端 未结 11 1793
礼貌的吻别
礼貌的吻别 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 01:59

    After getting used to the var keyword in C#, I'm starting to use the auto keyword in C++11. They both determine type by inference and are useful when you just want the compiler to figure out the type for you. Here's the C++11 port of your code:

    #include 
    #include 
    
    using namespace std;
    
    array strarr = {"ram", "mohan", "sita"};
    for(auto str: strarr) {
      listbox.items.add(str);
    }
    

提交回复
热议问题