重用相同的代码,在列表中的objcets的不同数据成员上执行相同的逻辑(Reuse the same code to do the same logic on different data membe

编程入门 行业动态 更新时间:2024-10-27 13:24:25
重用相同的代码,在列表中的objcets的不同数据成员上执行相同的逻辑(Reuse the same code to do the same logic on different data members of the objcets in a list)

具体来说,我有一个具有多个字符串对象数据成员(NID,customerNumber,studentNumber,fName,lName)的类的对象列表。

我想重用以下代码来搜索与搜索关键字匹配的节点,无论所查找的数据成员是NID还是该类的字符串数据成员的任何其他成员。

nodePtr = firstPtr; for(; nodePtr != NULL && nodePtr->str != str; nodePtr = nodePtr->nextPtr); if(nodePtr != NULL) //the nodePtr points to the node that matches the search key else //no node matched the search key

如果它是PHP代码我可以使用变量的值作为另一个的名称:

$node->${$var}

但是在C ++中无论如何都要重用代码?

specifically I have a list of objects of a class with multiple string object data members(NID, customerNumber, studentNumber, fName, lName).

I want to reuse the following code to search for the node that matches the search key whether the data member that is looked for is NID or any other of the class's string data members.

nodePtr = firstPtr; for(; nodePtr != NULL && nodePtr->str != str; nodePtr = nodePtr->nextPtr); if(nodePtr != NULL) //the nodePtr points to the node that matches the search key else //no node matched the search key

if it was PHP code I could use the value of a variable as the name of another:

$node->${$var}

but in C++ is there anyway to reuse the code?

最满意答案

最灵活的方法是提供谓词作为模板参数:

template <typename Pred> Node * find_if(Node * node, Pred pred) { for (; node && !pred(node); node = node->next); return node; }

在C ++ 11中,您可以使用lambda调用它:

if (Node * node = find_if(first, [&](Node * n){return n->NID == nid;})) { // node points to the matching node } else { // not found }

或者,如果你被困在过去的年代,一个函数对象:

struct CompareNID { CompareNID(std::string nid) : nid(nid) {} bool operator() {Node * n) {return n->NID == nid;} std::string nid; }; Node * node = find_if(first, CompareNID(nid));

或者,因为你的所有字段都是字符串,你可以使用成员指针牺牲灵活性,给出类似于PHP示例的内容:

Node * find(Node * node, std::string Node::*member, std::string const & value) { for (; node && node->*member != value; node = node->next); return node; } Node * node = find(first, &Node::NID, nid);

The most flexible way to do this is to provide a predicate as a template parameter:

template <typename Pred> Node * find_if(Node * node, Pred pred) { for (; node && !pred(node); node = node->next); return node; }

In C++11, you can call it with a lambda:

if (Node * node = find_if(first, [&](Node * n){return n->NID == nid;})) { // node points to the matching node } else { // not found }

or, if you're stuck in ages past, a function object:

struct CompareNID { CompareNID(std::string nid) : nid(nid) {} bool operator() {Node * n) {return n->NID == nid;} std::string nid; }; Node * node = find_if(first, CompareNID(nid));

or, since all your fields are strings, you could sacrifice flexibility for tersity using member pointers, giving something similar to your PHP example:

Node * find(Node * node, std::string Node::*member, std::string const & value) { for (; node && node->*member != value; node = node->next); return node; } Node * node = find(first, &Node::NID, nid);

更多推荐

本文发布于:2023-07-08 01:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1070197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:逻辑   成员   代码   数据   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!