微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在此代码中创建 find_if 函数?

如何解决如何在此代码中创建 find_if 函数?

Select t.*
  From your_table t
 Where action_type = 0
   And exists 
       (select 1 from your_table tt
         Where tt.group_id = t.group_id
           And tt.content = t.content
           And tt.action_type = 0
           And (tt.compositekey_part<> t.compositekey_part or t.version <> tt.version));

我想创建一个函数来查看边的权重是负的还是正的。我该如何制作这个功能

解决方法

我想创建一个函数,即使一个负权重为负也会输出 false。例如,如果第一个是 {0,1,-2} 而不是 {0,2} 它是假的

为什么要重新发明轮子?

如果我正确理解你想要什么,它适用于std::all_of()

#include <vector>
#include <iostream>
#include <algorithm>

struct Edge
 { int source,dest,weight; };

int main ()
 {
   std::vector<Edge> edges1 { {0,2},{0,2,3},{1,3,-1},{2,2} },edges2 { {0,+1},2} };

   auto l = [](auto const & elem) { return elem.weight >= 0; };

   auto b1 { std::all_of(edges1.cbegin(),edges1.cend(),l) },b2 { std::all_of(edges2.cbegin(),edges2.cend(),l) };

   std::cout << b1 << std::endl; // prints 0
   std::cout << b2 << std::endl; // prints 1
 }

更一般地说,一个建议:尝试使用标准库提供的结构和算法。

,

定义自定义函数并迭代向量以检查特定边的 weight 属性。

看看下面的实现:

#include <vector>
#include <iostream>

typedef struct Edge {
    int source,weight;
}Edge;

/*
Function to check if all weights are positive.
Ouput: Returns true (boolean) if all weights are positive and false (boolean) if any one of the weights is negative
*/
bool find_if(std::vector<Edge> edges){
    
    for(size_t i; i < edges.size(); i++){
        if(edges[i].weight < 0){
            
            return false;
        }
    }
    return true;
}

int main()
{
    std::vector<Edge> edges =
    {
        {0,2}           // (edge1,edge2,weight)
    };

    std::cout<<(find_if(edges)? "True" : "False")<<std::endl;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?