每个 C++,从向量元素中提取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15027282/
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
C++ for each, pulling from vector elements
提问by Springfox
I am trying to do a foreach on a vector of attacks, each attack has a unique IDsay, 1-3.
我正在尝试对攻击向量进行 foreach,每次攻击都有一个唯一的 ID,例如 1-3。
The class method takes the keyboard input of 1-3.
类方法采用键盘输入 1-3。
I am trying to use a foreach to run through my elements in m_attack to see if the number matches, if it does... do something.
我正在尝试使用 foreach 来遍历 m_attack 中的元素以查看数字是否匹配,如果匹配...
The problem I'm seeing is this:
我看到的问题是:
a'for each' statement cannot operate on an expression of type "std::vector<Attack
Am I going about this totally wrong, I have C# experience and is kind of what I'm basing this on, any help would be appreciated.
我是不是完全错了,我有 C# 经验,这就是我所基于的,任何帮助将不胜感激。
My code is as follows:
我的代码如下:
In header
在标题中
vector<Attack> m_attack;
In class
在班上
int Player::useAttack (int input)
{
for each (Attack* attack in m_attack) // Problem part
{
//Psuedo for following action
if (attack->m_num == input)
{
//For the found attack, do it's damage
attack->makeDamage();
}
}
}
回答by
For next examples assumed that you use C++11. Example with ranged-based for loops:
对于接下来的示例,假设您使用 C++11。基于范围的 for 循环示例:
for (auto &attack : m_attack) // access by reference to avoid copying
{
if (attack->m_num == input)
{
attack->makeDamage();
}
}
You should use const auto &attack
depending on the behavior of makeDamage()
.
您应该const auto &attack
根据makeDamage()
.
You can use std::for_each
from standard library + lambdas:
您可以使用std::for_each
标准库 + lambdas:
std::for_each(m_attack.begin(), m_attack.end(),
[](Attack * attack)
{
if (attack->m_num == input)
{
attack->makeDamage();
}
}
);
If you are uncomfortable using std::for_each
, you can loop over m_attack
using iterators:
如果你不习惯使用std::for_each
,你可以m_attack
使用迭代器循环:
for (auto attack = m_attack.begin(); attack != m_attack.end(); ++attack)
{
if (attack->m_num == input)
{
attack->makeDamage();
}
}
Use m_attack.cbegin()
and m_attack.cend()
to get const
iterators.
使用m_attack.cbegin()
和m_attack.cend()
获取const
迭代器。
回答by juanchopanza
This is how it would be done in a loop in C++(11):
这是在 C++(11) 的循环中完成的方式:
for (const auto& attack : m_attack)
{
if (attack->m_num == input)
{
attack->makeDamage();
}
}
There is no for each
in C++. Another option is to use std::for_eachwith a suitable functor (this could be anything that can be called with an Attack*
as argument).
for each
C++ 中没有。另一种选择是将std::for_each与合适的函子一起使用(这可以是任何可以用Attack*
as 参数调用的东西)。
回答by Nikos Athanasiou
The for each
syntax is supported as an extension to native c++ in Visual Studio.
for each
在 Visual Studio 中,该语法被支持作为本机 c++ 的扩展。
The example provided in msdn
msdn 中提供的示例
#include <vector>
#include <iostream>
using namespace std;
int main()
{
int total = 0;
vector<int> v(6);
v[0] = 10; v[1] = 20; v[2] = 30;
v[3] = 40; v[4] = 50; v[5] = 60;
for each(int i in v) {
total += i;
}
cout << total << endl;
}
(works in VS2013) is not portable/cross platform but gives you an idea of how to use for each
.
(在 VS2013 中有效)不是可移植/跨平台的,但可以让您了解如何使用for each
.
The standard alternatives (provided in the rest of the answers) apply everywhere. And it would be best to use those.
标准替代方案(在其余答案中提供)适用于任何地方。最好使用这些。
回答by andre
C++ does not have the for_each
loop feature in its syntax. You have to use c++11 or use the template function std::for_each
.
C++for_each
在其语法中没有循环功能。您必须使用 c++11 或使用模板函数std::for_each
。
struct Function {
int input;
Function(int input): input(input) {}
void operator()(Attack& attack) {
if(attack->m_num == input) attack->makeDamage();
}
};
Function f(input);
std::for_each(m_attack.begin(), m_attack.end(), f);