运行 C++ 中的迭代,用于在满足特定条件时存储变量
Run an iteration in C++ to store variable when certain condition is met
我编写了一个程序来存储频道值> 0.1 时的频道号。
我已经定义了迭代。
在循环中,迭代在不同的通道上运行(例如:通道 1、通道 2 等)。
然后我调用了另一个程序来计算值。它将逐一计算每个通道的值。
我的任务是获取 Channel Value > 0.1 的频道。我不知道如何存储这些频道号。如果你们能帮助我,我将不胜感激。谢谢
list < int > GetChannels(Node* node)
{
list<int> Channels = GetList(node); //calling a list which I already defined.
list<int>::iterator itr;
for (itr=Channels.begin(); itr!=Channels.end(); ++itr) {
double ChannelValue = CalculateValue(node, *itr); //calling another func
if (ChannelValue > 0.1) {
`
你能为 return 创建另一个包含这些值的列表吗?
list < int > GetChannels(Node* node) {
list<int> Channels = GetList(node); //calling a list which I already defined.
list<int> output; // Output list.
list<int>::iterator itr;
for (itr=Channels.begin(); itr!=Channels.end(); ++itr) {
double ChannelValue = CalculateValue(node, *itr); //calling another func
if (ChannelCapacityValue > 0.1) {
output.insert(*itr);
}
}
return output;
}
如果我得到确切的要求,您只需要通过在特定谓词 CalculateValue(node, *itr) > 0.1f
.
上过滤现有集合来构建列表
这似乎是 std::copy_if
的任务,有点像:
list<int> channels;
list<int> filteredChannels;
...
copy_if(channels.begin(), channels.end(), back_inserter(filteredChannels),
[node] (const int& value) { return CalculateValue(node,value) > 0.1f; }
);
既然 STL 已经为您提供了合适的设施,为什么还要重新发明轮子?
如果您无权访问 C++11 lambda,请提供一个自定义函数,例如。 bool isValidChannel(const int& channel) { ... }
但这需要绑定 node
参数。
我编写了一个程序来存储频道值> 0.1 时的频道号。 我已经定义了迭代。 在循环中,迭代在不同的通道上运行(例如:通道 1、通道 2 等)。 然后我调用了另一个程序来计算值。它将逐一计算每个通道的值。 我的任务是获取 Channel Value > 0.1 的频道。我不知道如何存储这些频道号。如果你们能帮助我,我将不胜感激。谢谢
list < int > GetChannels(Node* node)
{
list<int> Channels = GetList(node); //calling a list which I already defined.
list<int>::iterator itr;
for (itr=Channels.begin(); itr!=Channels.end(); ++itr) {
double ChannelValue = CalculateValue(node, *itr); //calling another func
if (ChannelValue > 0.1) {
`
你能为 return 创建另一个包含这些值的列表吗?
list < int > GetChannels(Node* node) {
list<int> Channels = GetList(node); //calling a list which I already defined.
list<int> output; // Output list.
list<int>::iterator itr;
for (itr=Channels.begin(); itr!=Channels.end(); ++itr) {
double ChannelValue = CalculateValue(node, *itr); //calling another func
if (ChannelCapacityValue > 0.1) {
output.insert(*itr);
}
}
return output;
}
如果我得到确切的要求,您只需要通过在特定谓词 CalculateValue(node, *itr) > 0.1f
.
这似乎是 std::copy_if
的任务,有点像:
list<int> channels;
list<int> filteredChannels;
...
copy_if(channels.begin(), channels.end(), back_inserter(filteredChannels),
[node] (const int& value) { return CalculateValue(node,value) > 0.1f; }
);
既然 STL 已经为您提供了合适的设施,为什么还要重新发明轮子?
如果您无权访问 C++11 lambda,请提供一个自定义函数,例如。 bool isValidChannel(const int& channel) { ... }
但这需要绑定 node
参数。