我不明白为什么下面的 C++ 代码可以工作。在此程序中,用户指定数组的大小和数组本身,然后程序打印数组的长度及其元素。我用来reserve()为数组元素分配内存,据我所知,我必须用来push_back()将元素添加到向量中,但我不这样做,虽然它总是打印length 0,但它访问向量元素没有任何问题并正确打印它们。为什么会这样呢?
#include <iostream>
#include <vector>
using namespace std;
int main(){
unsigned size;
cin >> size;
vector<unsigned long> nums;
nums.reserve(size);
for(unsigned i{0}; i < size; ++i) cin >> nums[i];
cout << "length " << nums.size() << '\n';
for(unsigned i{0}; i < size; ++i) cout << i << ' ' << nums[i] << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
#include <vector>
using namespace std;
int main(){
unsigned size;
cin …Run Code Online (Sandbox Code Playgroud) 我一直在阅读 c#12 中主要构造函数的介绍指南,并且得到了一些意想不到的结果(https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/primary-constructors)。
我有一些代码,例如:
public class ServiceA(IServiceB service)
{
public async Task<bool> Method() {
return await service.GetValue();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我创建一个像这样的单元测试:
[Fact]
public void Test1() {
var mockB = Substitute.For<IServiceB>();
_sut = new SeviceA(mockB);
_sut.Method();
Assert.NotNull(mockB);
}
Run Code Online (Sandbox Code Playgroud)
在对测试_sut.Method();中service通过模拟提供的 的调用中,它实际上null位于 ServiceA.Method 中。模拟在单元测试中正确创建,但它没有进入方法。
我发现解决此问题的一种方法是创建一个属性或字段来捕获主构造函数参数,然后使用它而不是参数。像这样的东西:
public class ServiceA(IServiceB service)
{
private IServiceB ServiceProperty { get; } = service; //capture the parameter
public async Task<bool> Method() {
return await ServiceProperty.GetValue(); //use property instead of parameter
}
} …Run Code Online (Sandbox Code Playgroud) 错误:(0, tanstack_react_query__WEBPACK_IMPORTED_MODULE_3 _.useQuery) 不是函数
我正在使用 next js 和 @tanstack/react-query 来获取数据,但在获取数据时出现上述错误。
我所做的代码如下。
这是我用于获取和呈现数据的组件。
import React from 'react'
import ServiceProps from '../interfaces/ServiceProps'
import { fetchServices } from '../api/services/api'
import { useQuery } from "@tanstack/react-query";
const Service = ({ title, content }: ServiceProps) => {
const { data, isLoading, isError, error, } = useQuery({ queryKey: ["fetchServices"], queryFn: fetchServices });
console.log(data)
return (
<div className="dark:bg-transparent rounded-lg dark:border-[#212425] dark:border-2 bg-blue-50 p-8">
{/* <img alt="icon" srcset="/images/icons/icon-1.svg 1x, /images/icons/icon-1.svg 2x" src="/images/icons/icon-1.svg" width="40" height="40" decoding="async" data-nimg="1" className="w-10 h-10 …Run Code Online (Sandbox Code Playgroud) 考虑以下程序
\nextern const int foo;\nextern void blah(void);\n\nint toto(void) {\n int x = foo;\n blah();\n int y = foo;\n return x + y;\n}\nRun Code Online (Sandbox Code Playgroud)\narm-linux-gnueabihf-gcc -std=c99 -O2 -fno-pic -S extern_const2.c将其编译为
toto:\n @ args = 0, pretend = 0, frame = 0\n @ frame_needed = 0, uses_anonymous_args = 0\n movw r3, #:lower16:foo\n movt r3, #:upper16:foo\n push {r4, lr}\n ldr r4, [r3]\n bl blah\n lsls r0, r4, #1\n pop {r4, pc}\nRun Code Online (Sandbox Code Playgroud)\n请注意,foo被读取一次,然后左移 1,这意味着假设gcc其值在函数调用期间保持不变。观察到类似的行为 …
我在 Flutter 应用程序中使用警报对话框,它运行良好。但是将flutter版本升级到3.16.0后,AlertDialog的backgroundColor属性无法按预期工作。
void _showDialog() {
BuildContext? dialogContext;
// set up the buttons
Widget cancelButton = TextButton(
child: const Text("Cancel"),
onPressed: () {
if (dialogContext != null) {
Navigator.of(dialogContext!).pop();
}
},
);
Widget emailButton = TextButton(
child: const Text("Email us"),
onPressed: () {},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
backgroundColor: Colors.white,
title: const Text("Title"),
content: Container(
color: Colors.white,
child: const Text('Content'),
),
actions: [
cancelButton,
emailButton,
],
);
// show the dialog
showDialog(
context: context,
builder: …Run Code Online (Sandbox Code Playgroud) 有谁知道 Rust 是否提供某种包装迭代器?例如:
let data = vec!(1, 2, 3, 4, 5);
let mut it = data.into_iter();
let mut wrapped = some_wrapping_iter(it); // wrap it
wrapped.take(2).for_each(|e| process_first_2_elems(e));
it = wrapped.into(); // and get back the original
it.for_each(|e| process_remaining(e));
Run Code Online (Sandbox Code Playgroud) 我正在创建一个新的 ASP.NET Core 8 Web API。在我之前使用 .NET 6 或 .NET 7 的项目中,我可以使用:
services.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(1, 0);
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true;
});
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
Run Code Online (Sandbox Code Playgroud)
现在,该软件包Microsoft.AspNetCore.Mvc.ApiExplorer已被弃用,我将其替换为新软件包Asp.Versioning.Mvc.ApiExplorer,但没有AddVersionedApiExplorer.
在 Swagger 的配置中,我有以下代码:
public class ConfigureSwaggerOptions : IConfigureNamedOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider _provider;
public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider)
{
_provider = provider;
}
public void Configure(SwaggerGenOptions options)
{
foreach (var description in _provider.ApiVersionDescriptions)
{
options.SwaggerDoc(
description.GroupName,
CreateVersionInfo(description));
} …Run Code Online (Sandbox Code Playgroud) 我正在学习 Java 中的多线程,我有一个简短的问题。我有一个同步块或方法和 2 个(或更多)线程。而且我只有一个只有一个核心的CPU,因此当CPU决定从一个线程切换到另一个线程时,2个线程将按顺序工作。
public synchronized void doSomething() {
// do something
}
Run Code Online (Sandbox Code Playgroud)
因此,一次只有一个线程会执行此方法,因为它是同步的。另一个线程被阻塞,在第一个线程完成执行之前无法执行该方法。
我认为有两种情况,CPU可以在thread1完成doSomething()的执行之前将执行从thread1切换到thread2。如果CPU这样做,则意味着线程1将停止执行doSomething()并让它未完成,线程2将看到doSomething()被锁定,因为线程1执行了它。之后我认为CPU会切换回thread1并继续执行doSomething()。在 CPU 切换回线程 2 之前,线程 1 可能会再次开始执行 doSomething() 。
第二种情况,线程1执行doSomething(),线程2在线程1执行该方法的整个期间被阻塞。当线程 1 完成 doSomething() 的执行后,CPU 将切换到线程 2,该线程将开始执行 doSOthing(),因为此时该方法尚未锁定。
有人可以向我解释什么是正确的情况吗?
“WillPopScope”已被弃用,不应使用。请改用 PopScope。此功能在 v3.12.0-1.0.pre 之后已弃用
WillPopScope(
onWillPop: () async {
// your logic
return false;
},
)
Run Code Online (Sandbox Code Playgroud)