对于像这样的 OrderLine 表:
| 命令 | 线 | 第 3 列 | 第 4 列 |
|---|---|---|---|
| 1 | 1 | X | X |
| 1 | 2 | X | 是 |
| 2 | 1 | 是 | X |
| 2 | 2 | 是 | X |
| 3 | 1 | X | 是 |
| 3 | 2 | 是 | 是 |
| 4 | 1 | X | 是 |
| 4 | 2 | X | 是 |
Col4 可以是 X 或 Y,但不能同时是两者。该表中的命令 1 违反了规则。如何查找同一订单在 Col4 中具有不同值的所有订单?
我最初的想法是多次加入表,然后使用 HAVING 子句。
SELECT DISTINCT A.[Order], A.Line, A.Col3, A.Col4, A.Col4
FROM OrderLines A
INNER JOIN OrderLines B ON B.[Order]= A.[Order]
AND B.Line = A.Line
GROUP BY A.[Order], A.Line, …Run Code Online (Sandbox Code Playgroud) 我有枚举 A、B、C。在一个方法中,我得到两个不同的枚举,并且必须返回剩余的枚举。
示例:我收到 A,CI 必须返回 B
我的解决方案是使用 if elseif else:
private EnumABC findRemaining(EnumABC pEnum1, EnumABC pEnum2){
if((pEnum1 == EnumABC.A || pEnum2 == EnumABC.A)
&& (pEnum1 == EnumABC.B || pEnum2 == EnumABC.B)){
return EnumABC.C;
} else
if((pEnum1 == EnumABC.A || pEnum2 == EnumABC.A)
&& (pEnum1 == EnumABC.C || pEnum2 == EnumABC.C)){
return EnumABC.B;
} else{
return EnumABC.A;
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有比这更可读的解决方案。
我有:
$rejected->{join ',', @needed}++ unless @filtered;
Run Code Online (Sandbox Code Playgroud)
但perlcritic抱怨
title=带引号的哈希键 - 严重性 5::[HashKeyQuotes] 避免无用的引号
显然我可以降低严重程度,但我想知道是否还有其他方法?
我能想到的两个是:
join之外{}。还有其他方法吗?
meow()这里第二次调用怎么编译呢?
#include <iostream>
struct F {
struct E {
using is_it_safe = bool;
};
E e;
};
template<typename T, typename = typename T::E::is_it_safe>
const void* meow(T const& t)
{
return &t;
}
int main() {
F f;
meow(f); // meow<F, bool>
meow(f.e); // meow<F::E, bool> ??
}
Run Code Online (Sandbox Code Playgroud)
F::E没有嵌套E,那么编译器如何推导第二个模板参数?
以下程序使用自动参数生成函数模板,然后显式实例化它们,将在 g++ 和 clang (x86-64 trunk, -std=c++20) 中正常编译,但在 MSVC (x64 v19.latest) 中给出错误 C2945 和 C3190 /std:c++20)。
https://godbolt.org/z/xsWcezzz6
#include <cstdint>
#include <concepts>
template <typename T>
concept MyType = std::same_as<T,uint16_t> || std::same_as<T,uint32_t>;
class A {
public:
void Foo(uint32_t arg1, MyType auto arg2);
void Bar(uint32_t agr1, MyType auto arg2, decltype(arg2) arg3);
};
void A::Foo(uint32_t arg1, MyType auto arg2) {} // define
template void A::Foo(uint32_t, uint16_t); // explicit instantiate for uint16_t
template void A::Foo(uint32_t, uint32_t); // explicit instantiate for uint32_t
void A::Bar(uint32_t arg1, MyType …Run Code Online (Sandbox Code Playgroud) 当我编写一个要与 一起使用的库时LD_PRELOAD,如何调试它的__attribute__((__constructor__))功能?它们似乎总是在 GDB 停止进程之前运行。作为 MCVE,运行以下命令:
cat > preflight.c <<EOF
#include <stdio.h>
#include <stdlib.h>
__attribute__((__constructor__))
void preflight(void) {
puts("Exiting from preflight");
exit(42);
}
EOF
gcc -g -fPIC -shared preflight.c -o preflight.so
gdb /bin/true -ex 'set environment LD_PRELOAD ./preflight.so' \
-ex 'set breakpoint pending on' \
-ex 'break preflight' \
-ex 'starti'
Run Code Online (Sandbox Code Playgroud)
GDB 输出的末尾将如下所示:
Function "preflight" not defined.
Breakpoint 1 (preflight) pending.
Starting program: /usr/bin/true
Exiting from preflight
During startup program exited with code 42.
(gdb)
Run Code Online (Sandbox Code Playgroud)
观察到该preflight …
我正在编写文件 I/O 代码
我发现每个操作系统使用不同的换行符
所以我想编写一个对于每个操作系统都有不同工作方式的代码,
如下代码所示
if(isWindows)
{
Console.Write("Hello World\r\n");
}
else
{
Console.Write("Hello World\n");
}
Run Code Online (Sandbox Code Playgroud)
如果您知道好的方法请告诉我!
字符串数据位于之前以“Mon Jan 08 18:55:57 GMT+05:30 2024”格式存储的文件中。我使用以下代码来转换字符串以获取值“08 Jan 2024”
Boolean goahead = false;
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
SimpleDateFormat formatterOut = new SimpleDateFormat("dd MMM yyyy");
try {
pundate = formatter.parse(punchDate);
System.out.println(pundate);
System.out.println(formatterOut.format(pundate));
} catch (ParseException e) {
e.printStackTrace();
}
String convertedtodaydate = dateFormater(String.valueOf(LocalDateTime.now()), "dd MMM yyyy", "yyyy-MM-dd"); //Output is 16 Feb 2024
try {
if(Objects.requireNonNull(sdf.parse(convertedtodaydate)).after(sdf.parse(formatterOut.format(pundate)))) {
goahead = true;
}
} catch (ParseException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
W/System.err: java.text.ParseException: Unparseable date: "16 Feb 2024"
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法来检查存储的日期是否早于当前日期?
我正在执行 SQL 查询
SELECT Id, COMMENT
FROM student
WHERE dbo.Remove (COMMENT) like '%N8AABENAABEPIVAA#GVCIZNV';
Run Code Online (Sandbox Code Playgroud)
这是Remove函数:
CREATE FUNCTION [dbo].[Remove]
(@StringData VARCHAR(255))
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE @word VARCHAR(200) = '';
WITH Numbers AS
(
SELECT TOP (LEN(@StringData )) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS N
FROM master.dbo.spt_values
)
SELECT @word += SUBSTRING(@StringData, N, 1)
FROM Numbers
WHERE (N - 1) % 4 <> 0;
RETURN @word;
Run Code Online (Sandbox Code Playgroud)
我尝试了这个函数,它返回的结果很好,但是当应用于 1'000'000 行时,它需要很多时间 - 大约 40 秒。
如何优化此功能以花费更少的时间或以任何其他方式执行相同的操作?
在其他现代编程语言中,当父类具有多个构造函数时,可以初始化只读属性,而无需重复代码。但 C# 似乎有一个相当烦人的限制。例如我有以下 C# 类:
class MyClass
{
string? str1 { get; init; }
string? str2 { get; init; }
public MyClass(string s1)
{
this.str1 = s1;
this.str2 = null;
}
public MyClass(string s1, string s2)
{
this.str1 = s1;
this.str2 = s2;
}
}
Run Code Online (Sandbox Code Playgroud)
可以修改构造函数来部分解决问题,如以下代码所示:
class MyClass
{
string? str1 { get; init; }
string? str2 { get; init; }
public MyClass(string s1): this(s1, null) { }
public MyClass(string s1, string s2)
{
this.str1 = s1;
this.str2 = s2; …Run Code Online (Sandbox Code Playgroud) c# ×2
c++ ×2
java ×2
sql ×2
.net ×1
android ×1
c ×1
compare ×1
date ×1
debugging ×1
decltype ×1
enums ×1
gdb ×1
ld-preload ×1
perl ×1
perl-critic ×1
platform ×1
readability ×1
sql-server ×1
t-sql ×1
template-argument-deduction ×1
templates ×1