简体   繁体   English

在C ++中的Foreach循环相当于C#

[英]Foreach loop in C++ equivalent of C#

How would I convert this code to C++? 我如何将此代码转换为C ++?

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

ranged based for : 范围基于

std::array<std::string, 3> strarr = {"ram", "mohan", "sita"};
for(const std::string& str : strarr) {
  listbox.items.add(str);
}

pre c++11 前c ++ 11

std::string strarr[] = {"ram", "mohan", "sita"};
for(int i = 0; i < 3; ++i) {
  listbox.items.add(strarr[i]);
}

or 要么

std::string strarr[] = {"ram", "mohan", "sita"};
std::vector<std::string> strvec(strarr, strarr + 3);
std::vector<std::string>::iterator itr = strvec.begin();
while(itr != strvec.end()) {
  listbox.items.add(*itr);
  ++itr;
}

Using Boost : 使用Boost

boost::array<std::string, 3> strarr = {"ram", "mohan", "sita"};
BOOST_FOREACH(std::string & str, strarr) {
  listbox.items.add(str);
}

In C++0x you have 在C ++ 0x中你有

for(string str: strarr) { ... }

But till then use ordinary for loop. 但直到那时使用普通的循环。

After getting used to the var keyword in C#, I'm starting to use the auto keyword in C++11. 在习惯了C#中的var关键字之后,我开始在C ++ 11中使用auto关键字。 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: 这是代码的C ++ 11端口:

#include <array>
#include <string>

using namespace std;

array<string, 3> strarr = {"ram", "mohan", "sita"};
for(auto str: strarr) {
  listbox.items.add(str);
}

Boost has a macro that will do this for you. Boost有一个宏,可以为你做到这一点。

http://www.boost.org/doc/libs/1_44_0/doc/html/foreach.html http://www.boost.org/doc/libs/1_44_0/doc/html/foreach.html

Just for fun (new lambda functions): 只是为了好玩(新的lambda函数):

      static std::list<string> some_list;

      vector<string> s; 
      s.push_back("a");
      s.push_back("b");
      s.push_back("c");

      for_each( s.begin(), s.end(), [=](string str) 
        {
          some_list.push_back(str);
        }

  );

  for_each( some_list.begin(), some_list.end(), [](string ss) { cout << ss; } );

Although doing a simple loop is recommended :-) 虽然建议做一个简单的循环:-)

Something like: 就像是:

const char* strarr = {"ram","mohan","sita", 0L};

for(int i = 0; strarr[i]; ++i)
{
  listbox.items.add(strarr[i]);
}

Also works for standard C. Not sure in C++ how to detect the end of the strarr without having a null element, but the above should work. 也适用于标准C.在C ++中不确定如何在没有null元素的情况下检测strarr的结尾,但上述应该可行。

string[] strarr = {"ram","mohan","sita"}; string [] strarr = {“ram”,“mohan”,“sita”};

#include <string>
std::string strarr = { "ram", "mohan", "sita" };

or 要么

const char* strarr[] = { "ram", "mohan", "sita" };

foreach(string str in strarr) { listbox.items.add(str); foreach(strarr中的字符串str){listbox.items.add(str); } }

for (int i = 0; i < sizeof strarr / sizeof *strarr; ++i)
    listbox.items.add(strarr[i]);

Note: you can also put the strings into a std::vector rather than an array: 注意:您还可以将字符串放入std :: vector而不是数组:

std::vector<std::string> strvec;
strvec.push_back("ram");
strvec.push_back("mohan");
strvec.push_back("sita");

for (std::vector<std::string>::const_iterator i = strvec.begin(); i != strvec.end(); ++i)
    listbox.items.add(*i);

The simple form: 简单的形式:

std::string  data[] = {"ram","mohan","sita"};
std::for_each(data,data+3,std::bind1st(std::mem_fun(&Y::add), &(listbox.items)));

An example in action: 行动中的一个例子:

#include <algorithm>
#include <string>
#include <iostream>
#include <functional>

class Y
{
    public:
      void add(std::string value)
      {
          std::cout << "Got(" << value << ")\n";
      }
};
class X
{
    public:
      Y  items;
};

int main()
{
    X listbox;

    std::string  data[] = {"ram","mohan","sita"};
    std::for_each(data,data+3,std::bind1st(std::mem_fun(&Y::add), &(listbox.items)));
}

If you have an array you can simply use a for loop. 如果你有一个数组,你可以简单地使用for循环。 (I'm sorry, but I'm not going to type out the code for a for loop for you.) (对不起,我不打算为你输​​入for循环的代码。)

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 使用boost是最好的选择,因为它可以帮助您提供简洁明了的代码,但是如果您想坚持使用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));

}

using C++ 14: 使用C ++ 14:

#include <string>
#include <vector>


std::vector<std::string> listbox;
...
std::vector<std::string> strarr {"ram","mohan","sita"};    
for (const auto &str : strarr)
{
    listbox.push_back(str);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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