C++ int数组中的foreach
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14338660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
foreach in C++ int array
提问by junni lomo
I am new to C++ and I am writing the following code.
I needed to iterate over all the addons in my calling function - testFunction
. I know this works in C#, but this code is not working. Can anyone please point out the right way to do it in C++?
我是 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];
}
}
I tried to implement vector as below suggestions by you guys
我尝试按照你们的以下建议实施矢量
#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);
}
}
回答by Tony The Lion
An array (a raw array) decays into a pointer when passed as an argument to a function, so your array has no size information.
数组(原始数组)在作为参数传递给函数时会衰减为指针,因此您的数组没有大小信息。
You need to pass the length of the array explicitly into the function to know it inside the function.
您需要将数组的长度显式传递给函数以在函数内部了解它。
Alternatively, and better, use a std::vector
and then you'll have the .size()
always available when needed.
或者,更好的是,使用 astd::vector
然后您将.size()
在需要时始终可用。
回答by Armen Tsirunyan
Apart from using vectors, as Tony suggests, you can use templates and pass the array by reference so that the compiler will deduce the array's size:
除了使用向量之外,正如 Tony 建议的那样,您可以使用模板并通过引用传递数组,以便编译器推断数组的大小:
template<int N>
void testFunction(int mainProd,int (&addons)[N])
{
for(int x = 0; x < N; ++x) // ---- working
{
std::cout<< addons[x];
}
}
回答by PaperBirdMaster
You're using concepts of C# in C++ but, even if we assume that both languages are similar, they're not equal.
您在 C++ 中使用 C# 的概念,但是,即使我们假设两种语言相似,它们也不相等。
The syntax for a ranged-for in C++ is the following:
C++ 中 ranged-for 的语法如下:
for (type identifier : container) // note the ':', not ';'
{
// do stuff
}
You can use this for flavourif you have a C++11 compiler.
如果您有C++11 编译器,则可以将其用于风味。
Btw, it seems that you're using properties on your code:
顺便说一句,您似乎在代码中使用了属性:
for(int x = 0 ; addons.length;++x) // what is lenght?
{
std::cout<< addons[x];
}
There's no such thing in C++, if you want to call an object method you need to call it as a function:
C++中没有这样的东西,如果你想调用一个对象方法,你需要把它作为一个函数来调用:
// assuming that the object 'addons' have a method
// named 'length' that takes no parameters
addons.length();
But the addons
variable isn't an object, is an array (take a look to this tutorial), so it doesn't have a method or property named length
; if you need to know its length in order to iterate it you can use in some contexts the sizeof
operator (see the tutorial for more information).
但是addons
变量不是对象,而是数组(查看本教程),因此它没有名为length
;的方法或属性。如果您需要知道它的长度以便迭代它,您可以在某些上下文中使用sizeof
运算符(有关更多信息,请参阅教程)。
Let's suppose that addons
were a container:
让我们假设这addons
是一个容器:
typedef std::vector<addon> Addons;
Addons addons;
If you want to iterate it using the C++11 range-for, you can write it as follows:
如果你想使用 C++11 range-for 来迭代它,你可以这样写:
for (addon a : addons)
{
// do stuff with a.
}
Hope it helps.
希望能帮助到你。
回答by Alex Chamberlain
If you were to use a std::vector
or std::array
, you could use std::foreach
,
如果要使用std::vector
或std::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
});
回答by Vivek Malik
Code is working with this
代码正在处理这个
for (int i = 0; i < (end(array) - begin(array)); i++)
for (int i = 0; i < (end(array) - begin(array)); i++)
Return maximum size
返回最大尺寸
Test whether array is empty
测试数组是否为空
array::empty
Element of array
数组元素
array::size
Array size
数组大小
sizeof()
回答by Sergey Goravsky
If you don't want to use any STL container, you just need to pass the array by reference to the function. Problem here is that you can't define such argument without exact size of the array. This restriction you can overcome making the function as a template, defining the size as the template parameter:
如果您不想使用任何 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;
}