问题列表 - 第318084页

如何解释Java中for-each循环中对Iterable的强制转换方法引用?

我找到了以下代码:

try (Stream<String> lines = Files.lines(path, charset)) {
    for (String line : (Iterable<String>) lines::iterator) {
        //logic
    }
}
Run Code Online (Sandbox Code Playgroud)

该代码已编译并按预期工作。但是,我无法理解这一段:(Iterable<String>) lines::iterator。据我了解, for-each 循环是for (Foo foo: Iterable<Foo> iterable) {}. 但是如何将方法引用转换为 呢Iterable?有人能解释一下吗?

java for-loop

4
推荐指数
1
解决办法
82
查看次数

与“[[no_unique_address]]”斗争

我在 g++ 13.2 中得到了奇怪的sizeof()结果。[[no_unique_address]]

#include <iostream>

using namespace std;

template<bool T>
struct B
{
    struct Dummy {};

    int *a; // swap position with <below placemark>
    // In this position, results are "16" and "40".
    
    [[no_unique_address]] std::conditional_t<T, Dummy, size_t> y;
    [[no_unique_address]] std::conditional_t<T, Dummy, size_t> x;
    [[no_unique_address]] std::conditional_t<T, Dummy, size_t> h;
    [[no_unique_address]] std::conditional_t<T, Dummy, size_t> w;
    
    // <below placemark>
    // In this position, results are "8" and "40".
    
};

int main()
{
    cout << sizeof(B<true>) << "\n";
    cout << …
Run Code Online (Sandbox Code Playgroud)

c++ c++20

5
推荐指数
1
解决办法
140
查看次数

如何有效地将 const wchar* 移动到 std::wstring 中?

最近我一直在做一个项目,遇到一个问题,我需要不断地将 a 复制const wchar*wstring. 有没有什么方法可以将指针“移动”到字符串中,这样它就不会复制它?

我希望有一种直接的方法来做到这一点,但我未能找到任何关于此的资源

c++

2
推荐指数
1
解决办法
80
查看次数

Angular 17 中忽略了 HttpInterceptorFn

我正在努力学习 Angular (17.1.2),并竭尽全力让 HttpInterceptorFn 工作。它在我的 http 请求中被忽略。谁能看到我做错了什么吗?提前致谢。

auth.interceptor.ts

import { HttpInterceptorFn } from '@angular/common/http';

export const authInterceptor: HttpInterceptorFn = (req, next) => {
  console.log('interceptor ran');
  return next(req);
};
Run Code Online (Sandbox Code Playgroud)

应用程序配置.ts

import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';

import { routes } from './app.routes';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { authInterceptor } from './interceptors/auth.interceptor';

export const appConfig: ApplicationConfig = {
  providers: [
    provideRouter(routes), 
    provideHttpClient(withInterceptors([authInterceptor]))
  ]
};
Run Code Online (Sandbox Code Playgroud)

应用程序组件.ts

import { HttpClient, HttpClientModule } from '@angular/common/http'; …
Run Code Online (Sandbox Code Playgroud)

angular

6
推荐指数
1
解决办法
459
查看次数

将最小(有符号)32 位数字写入 C++ 文字

如果我想使用 <limits> 标头,则很容易编写最小整数:

std::int32_t small = std::numeric_limits<std::int32_t>::min();
Run Code Online (Sandbox Code Playgroud)

small等于-2147483648。

我在一家图书馆工作,里面有这样的数字。因此最好写成

std::int32_t small = -2147483648;
Run Code Online (Sandbox Code Playgroud)

我在 Visual Studio 上遇到了问题,但是当我阅读 C++ 规范时,我发现它表明我无法对任何编译器执行此操作。我认为我不能这样做的原因是规范表明整数文字不包含负号。负号实际上后来变成了一元减号。因此,C++ 处理整数文字 2147483648。这个数字太大,无法放入 int 中(假设 int 是 32 位,这是我正在考虑的平台上的),因此它必须是一个 long int (64 位) 。

(顺便说一句,这是我在 Visual Studio 上出错的地方。它似乎将 2147483648 视为无符号长整型,而不是有符号长整型,但这个问题与规范行为有关)

据我所知,无法将 32 位数字 -2147483648 写为 32 位文字。您必须首先将其设为 64 位文字 2147483648UL,然后将其取反。这似乎是一个疏忽,但它似乎也可以在任何地方编译(除了 Visual Studio),那么我错过了什么?

c++ literals

6
推荐指数
1
解决办法
164
查看次数

为什么 std::errno 不存在而“errno”存在?

为了让我的代码更符合C++风格而不是C风格,我想替换errnostd::errno,并替换include <errno.h>include <cerrno>.

令我惊讶的是,这些都不是std::errno

众所周知,许多 C 事物都有 C++ 等价物。举例来说:

在 c-source.c 中,我们通常这样编码:

#include <string.h>
print( strerro(1) );
Run Code Online (Sandbox Code Playgroud)

在c++source.cpp中,我们可以这样做:

#include <cstring>
std::cout << std::strerro(1);
Run Code Online (Sandbox Code Playgroud)

为什么存在却不std::errno存在std::strerro

c++ namespaces std errno

3
推荐指数
1
解决办法
66
查看次数

T-SQL FOR XML EXPLICIT - 将属性“xsi:nil”添加到元素

我需要生成符合第三方规范的 XML 文件。我遇到麻烦的一种情况是存在 NULL 列。规范规定,除了该xsi:nil="true"属性之外,还需要有一个原因属性。

该节点需要如下所示:

<Phone xsi:nil="true" Reason="none" />
Run Code Online (Sandbox Code Playgroud)

我可以生成具有属性和值的节点,但不能xsi:nil生成具有附加属性的节点。

这将为除属性之外的所有内容生成正确的格式xsi:nil

DECLARE @T TABLE(
    [Id] [int],
    [OwnerId] [int],
    [Number] [char](12),
    [Type] [char](4),
    [Reason] [char](4))
INSERT INTO @T VALUES
    (1,1,'414-555-1212','cell',NULL),
    (2,2,NULL,NULL,'None'),
    (3,3,'202-555-1212','work',NULL),
    (4,3,'212-555-1212','cell',NULL)

SELECT
    1 AS Tag,
    NULL AS Parent,
    NULL AS [Root!1],
    NULL AS [Phone!2!OwnerId],
    NULL AS [Phone!2!Type],
    NULL AS [Phone!2!Reason],
    NULL AS [Phone!2]
union all
SELECT
    2 AS Tag,
    1 AS Parent,
    NULL,
    OwnerId,
    Type,
    Reason,
    Number
FROM
    @T
FOR XML EXPLICIT; …
Run Code Online (Sandbox Code Playgroud)

xml t-sql sql-server

3
推荐指数
1
解决办法
67
查看次数

将 cy.get() 与动态类名一起使用?

我在我的 React 应用程序中使用动态类名称,它们是由 CSS 模块生成的。

编译className={styles.myclass}class="Myclass__1234"这样,cypresscy.get(.'myclass')将无法工作。

举个例子:

import styles from 'styles/About.module.css';

const About = () => {

  return (
    <div className={styles.about}>
       <h1 className={styles.title}>About Me Page</h1>
    </div>
  );
};

export default About;
Run Code Online (Sandbox Code Playgroud)

要检查标题文本,您需要执行以下操作:

cy.get('About_title__1234').should('not.be.empty');
Run Code Online (Sandbox Code Playgroud)

这意味着我每次都必须深入开发工具才能找到真正的类名。

我还可以使用其他方法吗?

我想cy.get('h1')可以,但稍后我可能需要在页面上使用多个 h1 标签。

reactjs cypress

2
推荐指数
1
解决办法
41
查看次数

IBM Quantum Platform 上是否有调用作业的估计等待时间的函数?

我试图编写一个脚本,使用 Qiskit 获取有关 IBM Quantum Platform 上作业的当前信息。

我设法使用 JobID 调用当前状态,但似乎没有函数或 API 调用可以显示作业的当前估计等待时间。

此外,虽然我设法收集有关作业当前状态的信息(例如已排队),但我得到的输出为“无”,也许我们也可以设法解决此问题。

有人可以帮我吗?

我尝试包含一个函数来调用估计等待时间,但似乎没有 API 调用。

quantum-computing qiskit

5
推荐指数
1
解决办法
42
查看次数

为什么 Rust 的函数移植比 C++ 慢 2 倍?

我有一个在 C++ 中计算字符串编辑距离的函数:

#include <string>
#include <vector>
#include <algorithm>

size_t sed_diff(const std::string & a, const std::string & b) {

    std::vector<size_t> cp(b.length() + 1);
    std::vector<size_t> cc(b.length() + 1);

    // Edit distance on postorder traversal.
    for (int j = 0; j <= b.length(); ++j) {
        cp[j] = j;
    }
    for (int i = 1; i <= a.length(); ++i) {
        cc[0] = i;
        for (int j = 1; j <= b.length(); ++j) {
            unsigned int c1 = a[i - 1];
            unsigned int c2 …
Run Code Online (Sandbox Code Playgroud)

c++ optimization benchmarking edit-distance rust

3
推荐指数
1
解决办法
307
查看次数