【问题标题】:foreach in C++ int arrayC ++ int数组中的foreach
【发布时间】:2012-12-29 14:32:54
【问题描述】:

我是 C++ 新手,我正在编写以下代码。 我需要遍历调用函数中的所有插件 - testFunction。我知道这在 C# 中有效,但这段代码不起作用。谁能指出用 C++ 做的正确方法?

#include "stdafx.h"
#include <iostream>
#include "resource.h"

int testFunction(char* tester);
int _tmain()
{
    int mainProd=2;
    int Addons[]={7,8,9,10};

    testFunction(mainProd,Addons);


}
void testFunction(int mainProd,int addons[])
{
    for(int x = 0 ; addons.length;++x) ---- Not working 
    {
        std::cout<< addons[x];
    }
}

我尝试按照你们的以下建议实现向量

#include "stdafx.h"
#include <iostream>
#include "resource.h"
#include <vector>

void testFunction(std::vector<int> addons);

int _tmain(int argc, _TCHAR* argv[])
{

    std::vector<int>  Addons ;
    for(int i = 0 ;i<10;++i)
    {
        Addons.push_back(i);
    }
     testFunction(Addons);
}

void testFunction(std::vector<int> addons)
{
    for(int i =0 ; i<addons.size();++i)
    {
        std::cout<<addons.at(i);
    }
}

【问题讨论】:

  • 您从哪里得知.length 会起作用?也许您需要选择a better book
  • 你最喜欢的 C++ 书籍是怎么说的?
  • fordward 也声明了 testFunction 的正确版本。
  • 通过引用传递向量以避免不必要的复制void testFunction(const std::vector&lt;int&gt; &amp;addons) // note the 'const' and the '&amp;'
  • for(int i =0 ; i&lt;addons.size();++i) 是迭代向量的糟糕风格。请改用beginend

标签: c++ visual-c++ c++11


【解决方案1】:

数组(原始数组)在作为参数传递给函数时衰减为指针,因此您的数组没有大小信息。

您需要将数组的长度显式传递给函数才能在函数内部知道它。

或者,更好的是,使用std::vector,然后您将在需要时始终使用.size()

【讨论】:

  • 或者std::array,如果大小在编译时是固定的。
  • 因为您有一个带有 char* 参数的函数,在您的 main 之前声明,并且只在之后定义了正确的参数。 ideone.com/2H1xX8
  • 或者通过引用传递一个数组
【解决方案2】:

除了使用向量,正如 Tony 建议的那样,您可以使用模板并通过引用传递数组,以便编译器推断数组的大小:

template<int N>
void testFunction(int mainProd,int (&addons)[N])
{
    for(int x = 0; x < N; ++x) // ---- working 
    {
        std::cout<< addons[x];
    }
}

【讨论】:

    【解决方案3】:

    如果您要使用std::vectorstd::array,您可以使用std::foreach

    std::vector<int> addons = {7,8,9,10};
    std::array<int, 4> addons = {7,8,9,10}; // Only use one of these...
    std::foreach(addons.begin(), addon.end(), [](int i) {
        std::cout << i
    });
    

    【讨论】:

    • lambda 应该采用 int,而不是迭代器
    • @ArmenTsirunyan 我的错误,对不起。
    • 不如使用新的 range-for then。
    • @MarcGlisse 你可以,但我不喜欢这种语法;不那么可读。
    • std::foreach vs range-for could be a matter of taste?
    【解决方案4】:

    您在 C++ 中使用 C# 的概念,但是,即使我们假设两种语言相似,它们也不相同。

    C++ 中 ranged-for 的语法是 the following:

    for (type identifier : container) // note the ':', not ';'
    {
        // do stuff
    }
    

    如果您有C++11 compiler,您可以将其用于风味

    顺便说一句,您似乎在代码中使用了属性:

    for(int x = 0 ; addons.length;++x) // what is lenght?
    {
        std::cout<< addons[x];
    }
    

    C++中没有这样的东西,如果你想调用一个对象方法你需要把它作为一个函数来调用:

    // assuming that the object 'addons' have a method
    // named 'length'  that takes no parameters
    addons.length();
    

    但是addons 变量不是对象,而是一个数组(查看this tutorial),因此它没有名为length 的方法或属性;如果您需要知道它的长度以便对其进行迭代,您可以在某些情况下使用 sizeof 运算符(有关更多信息,请参阅教程)。

    假设addons 是一个容器:

    typedef std::vector<addon> Addons;
    Addons addons;
    

    如果要使用C++11 range-for进行迭代,可以这样写:

    for (addon a : addons)
    {
        // do stuff with a.
    }
    

    希望对你有帮助。

    【讨论】:

    • 这个答案有很多错别字。
    • @AlexChamberlain 你能帮我解决它吗?我没有发现任何错别字,也许你指的是我缺乏英语能力?
    【解决方案5】:

    代码正在处理这个

    for (int i = 0; i

    返回最大尺寸

    测试数组是否为空

    array::empty
    

    数组元素

    array::size
    

    数组大小

    sizeof()
    

    【讨论】:

      【解决方案6】:

      如果你不想使用任何 STL 容器,你只需要通过引用函数来传递数组。这里的问题是,如果没有数组的确切大小,您就无法定义这样的参数。您可以克服这个限制,将函数设为模板,将大小定义为模板参数:

      #include <iostream>
      
      template<int N>
      void testFunction(int mainProd,int (&addons)[N])
      {
          for(int x = 0 ; x < N; ++x)
          {
              std::cout<< addons[x];
          }
      }
      
      int main()
      {
          int mainProd=2;
          int Addons[]={7,8,9,10};
      
          testFunction(mainProd,Addons);
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2018-05-23
        • 2021-07-19
        • 2014-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-27
        • 1970-01-01
        • 2011-04-16
        相关资源
        最近更新 更多