与strncmp相比,有没有一种方法可以使用标准库简化struct数组上的循环?
以下是我的尝试失败,因为std::count\U if抱怨没有重载函数std::begin matches的实例。
#include "stdafx.h"
#include "afx.h"
#include <string.h>
#include <iostream>
#include <algorithm>
#include <string>
struct nodeobject
{
CString ObjectType;
nodeobject() {}
explicit nodeobject(CString objectType) { ObjectType = objectType; }
};
struct nodeinput
{
struct nodeobject Object;
};
// Original function I want to rewrite to remove the for loop and the strncmp
static int ContainsObjectType(int collectionSize, struct nodeinput collection[], char* objectType)
{
auto found = 0;
for (auto idx = 0; idx < collectionSize; idx++)
{
if (strncmp(objectType,
collection[idx].Object.ObjectType,
strlen(collection[idx].Object.ObjectType)) == 0)
{
found = 1;
}
}
return found;
}
#if 0
// The implementation below does not compile because there is no instance of
// overloaded function std::begin matches
static int ContainsObjectType(int collectionSize, struct nodeinput collection[], char* objectType)
{
auto numFound = std::count_if(std::begin(collection),
std::end(collection),
[](struct nodeinput oneNode)
{
return strncmp(objectType, oneNode.Object.ObjectType, oneNode.Object.ObjectType) == 0);
});
return numFound > 0;
}
#endif
int main()
{
struct nodeobject node1("fokker");
struct nodeobject node2("airbus");
struct nodeobject node3("boing777");
struct nodeinput collection[] = {node1, node2, node3};
auto nintnode = 3;
auto found = ContainsObjectType(nintnode, collection, "boing777");
std::cout << found << std::endl;
return 0;
}
错误是:
C2784:常量元素*标准::开始(标准::初始值设定项\u列表
下面是完整的解决方案,以防有人搜索如何用库标准替换对象数组上的c风格循环。
#include "stdafx.h"
#include "afx.h"
#include <string.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
// Legacy code
class NodeObject
{
public:
CString objectType; // Keeping CString to minimize the impact for now
NodeObject() {}
explicit NodeObject(CString objectType) : objectType(objectType) { }
};
// Legacy code
class NodeInput
{
public:
NodeObject object;
explicit NodeInput(NodeObject object) : object(object) { }
};
// Code using vector and count_if
static int Contains(std::vector<NodeInput> collection, std::string objectType)
{
auto numFound = std::count_if(std::begin(collection),
std::end(collection),
[&](const NodeInput oneInput)
{
return (std::strncmp(objectType.c_str(),
oneInput.object.objectType,
strlen(oneInput.object.objectType)) == 0);
});
return numFound > 0;
}
// Code using array pointer and count_if
static int Contains(int collectionSize, NodeInput* collection, char* objectType)
{
auto numFound = std::count_if(collection,
collection + collectionSize,
[&](const NodeInput oneInput)
{
return (std::strncmp(objectType,
oneInput.object.objectType,
strlen(oneInput.object.objectType)) == 0);
});
return numFound > 0;
}
// Legacy code
static int ContainsOriginal(int collectionSize, NodeInput collection[], char* objectType)
{
auto found = 0;
for (auto idx = 0; idx < collectionSize; idx++)
{
if (strncmp(objectType,
collection[idx].object.objectType,
strlen(collection[idx].object.objectType)) == 0)
{
found = 1;
}
}
return found;
}
int main()
{
// Legacy code
NodeInput arrayCollection[] { NodeInput(NodeObject("fokker")),
NodeInput(NodeObject("airbus")),
NodeInput(NodeObject("boing777")) };
auto arrayCollectionSize = 3;
auto found = Contains(arrayCollectionSize, arrayCollection, "boing777");
std::cout << found << std::endl;
// Code using Standard Library
std::vector<NodeInput> collection{ NodeInput(NodeObject("fokker")),
NodeInput(NodeObject("airbus")),
NodeInput(NodeObject("boing777")) };
found = Contains(collection, "boing777");
std::cout << found << std::endl;
return 0;
}
只需将集合设置为指针,也可以是常量:
static int ContainsObjectType(int collectionSize, struct nodeinput *collection, char* objectType)
{
auto numFound = std::count_if(
collection,
collection + collectionSize,
...);
}
count\u if的前两个参数必须是迭代器,而指针实际上是基元迭代器。
std::begin(iterable)
只是指针
,std::end(iterable)
是指针
例如,我在golang中进行了以下测试: 如果我尝试构建它,我会收到以下内容: 有没有办法让编译器内联?如果是,是否有任何方法可以内联映射迭代?
我是javaScript的新手,在循环和编写函数方面有些吃力。我试图想出一个函数来循环我的rangeArray,其中用户输入的数字是数组的末尾,并且每个包含1的整数都被替换为“哔”,直到用户输入的数字为止。例:[0,“哔”,2,3,4,5,6,7,8,9,“哔”,“哔”,12]。12和2将是不同的,因为稍后我打算添加一个例外,如果一个数字包含任何2的整数,它将取代“beep”并将其替换为“boop
如果唯一的问题是循环数两次,我可以把结果减半,但我希望每个循环只走一次。同样的算法可能对其他事情有好处。
假设您有一个数组,其中包含一周中的几天: 现在假设你有一个数组,它记录一年中的每一个数字日,这个数组由366个元素组成。 有没有可能写一个循环或一些东西,当在天数组中循环时,它重置回星期一以保持输出看起来像: 周一:1周二:2周三:3周四:4周五:5周六:6周日:7周一:8周二:9 ect一直到366
我正在寻找一种用函数式编程方法替换嵌套foreach循环的方法。情况是这样的: 目前我的代码是这样的: 这将生成如下所示的输出: 有谁知道如何用函数式编程替代方案替换foreach代码块?
问题:我能否使用Java流API(即一行)实现方法。需要变量来定义字符串的长度;我尝试为每个循环,但它不工作。我需要一个循环的范围。 我的尝试: