在c++中相当于c#的每个循环

时间:2021-08-13 16:50:07

How would I convert this code to C++?

如何将这些代码转换为c++ ?

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

11 个解决方案

#1


99  

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

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::array<std::string, 3> strarr = {"ram", "mohan", "sita"};
BOOST_FOREACH(std::string & str, strarr) {
  listbox.items.add(str);
}

#2


22  

In C++0x you have

在c++ 0 x

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

But till then use ordinary for loop.

但在此之前用普通的for循环。

#3


9  

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:

在习惯了c#中的var关键字之后,我开始在c++ 11中使用auto关键字。它们都是根据推断确定类型的,当您只是希望编译器为您确定类型时,它们都是有用的。这里是您的代码的c++ 11端口:

#include <array>
#include <string>

using namespace std;

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

#4


5  

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

#5


5  

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 :-)

虽然做一个简单的循环是推荐的:

#6


2  

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++中不确定如何在没有空元素的情况下检测到strarr的结束,但是上面的工作应该是可行的。

#7


1  

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

string[]strarr = {“内存”,“汉”、“升达”};

#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中的string 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);

#8


1  

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)));
}

#9


0  

If you have an array you can simply use a for loop. (I'm sorry, but I'm not going to type out the code for a for loop for you.)

如果你有一个数组,你可以简单地使用for循环。(对不起,我不会为您输入for a for a for a loop的代码。)

#10


0  

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));

}

#11


0  

using C++ 14:

使用c++ 14:

#include <string>
#include <vector>


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

#1


99  

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

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::array<std::string, 3> strarr = {"ram", "mohan", "sita"};
BOOST_FOREACH(std::string & str, strarr) {
  listbox.items.add(str);
}

#2


22  

In C++0x you have

在c++ 0 x

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

But till then use ordinary for loop.

但在此之前用普通的for循环。

#3


9  

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:

在习惯了c#中的var关键字之后,我开始在c++ 11中使用auto关键字。它们都是根据推断确定类型的,当您只是希望编译器为您确定类型时,它们都是有用的。这里是您的代码的c++ 11端口:

#include <array>
#include <string>

using namespace std;

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

#4


5  

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

#5


5  

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 :-)

虽然做一个简单的循环是推荐的:

#6


2  

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++中不确定如何在没有空元素的情况下检测到strarr的结束,但是上面的工作应该是可行的。

#7


1  

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

string[]strarr = {“内存”,“汉”、“升达”};

#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中的string 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);

#8


1  

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)));
}

#9


0  

If you have an array you can simply use a for loop. (I'm sorry, but I'm not going to type out the code for a for loop for you.)

如果你有一个数组,你可以简单地使用for循环。(对不起,我不会为您输入for a for a for a loop的代码。)

#10


0  

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));

}

#11


0  

using C++ 14:

使用c++ 14:

#include <string>
#include <vector>


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