我尝试使用迭代器进行“正确的”Rust 版本的冒泡排序。明显的代码包含以下几行:
let mut vi = v.windows(2);
for mut i in vi {
if i[0] > i[1] {
i.swap(0, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到了错误
let mut vi = v.windows(2);
for mut i in vi {
if i[0] > i[1] {
i.swap(0, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法从迭代器获取对向量元素对的可变引用?
我正在使用微控制器,只能在闪存 RAM 中的固定地址静态分配一定大小的数组。如果所需的大小超过最大大小,我还需要在另一个固定地址分配第二个数组,其大小与差异相同。
unsigned char buffer[CONF_BUFF_SIZE] __attribute__ ((section ("SRAM_BANK2"))); // allocated on free SRAM at 0x1000
#ifdef CONF_BUFF_SIZE > BUFF_MAX
unsigned char buffer[CONF_BUFF_SIZE] __attribute__ ((section ("SRAM_BANK4"))); // allocated on 0xF000
#endif
// lots of yuck and muck with indexing and switching addresses
Run Code Online (Sandbox Code Playgroud)
我想将代码转换为现代 C++ (c++2x)。我还想对两个缓冲区进行迭代和索引,就好像它是一个连续的数组一样,而无需显式计算索引和切换数组。我如何在惯用的现代 C++ 中实现这一点?
缓冲区[1000] = 0xFE;// 修改“buffer2”中的内存
test.cpp
enum {
ERR_FILE_ACCESS = 1,
ERR_WHATEVER
};
enum ERR : unsigned int {
ERR_FILE_ACCESS = 5,
ERR_FILE_FORMAT = 6
};
int main(int, char**)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当然,重新定义枚举器“ERR_FILE_ACCESS”会失败。但:
test2.cpp
enum {
ERR_FILE_ACCESS = 1,
ERR_FILE_FORMAT,
ERR_WHATEVER
};
namespace Internal {
enum ERR : unsigned int {
ERR_FILE_ACCESS = 5,
ERR_FILE_FORMAT = 6
};
}
using namespace Internal;
int main(int, char**)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
确实编译成功。
我很惊讶/困惑,为什么命名空间 - after using namespace Internal;- 可以防止名称冲突?
为什么初始化std::stringto ""(通过 lambda)会崩溃?
这不会崩溃:
static std::string strTest2 =
[](){std::string * s = &strTest2; (*s) = "a"; return s->c_str();}();`
Run Code Online (Sandbox Code Playgroud)
这个技巧(首先将其初始化为非空,但预期的最终值为空)不会崩溃:
static std::string strTest3 =
[](){std::string * s = &strTest3; (*s) = "b"; (*s) = ""; return s->c_str();}();
Run Code Online (Sandbox Code Playgroud)
在这里崩溃了(*s) = ""。是因为它是空字符串吗?很特别吗?std::string分配给 时是否未构造/初始化""?
static std::string strTest1 =
[](){std::string * s = &strTest1; (*s) = ""; return s->c_str();}();`
Run Code Online (Sandbox Code Playgroud) 基本上,我在课堂上遇到过这段代码:
#include <stdio.h>
int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printf("%d", arr[0][2]);
printf("\n%d", arr[1][0]);
printf("\n%d", arr[0][2 + 1]);
}
Run Code Online (Sandbox Code Playgroud)
我有点困惑,因为最后一个printf语句arr[0][2 + 1]等于arr[0][3],但由于我们的数组维度是 3x3,所以这使得我们的最大索引值仅为 2,从而arr[0][3]超出范围。但是当我运行这个东西时,它显示4,这与第二条语句相同printf,即arr[1][0],为什么会发生这种情况?
我期待类似的错误,它会告诉我它超出了范围,但它继续打印数组的下一个索引值。
使用 OpenAI API 及以下是我的简单代码,但收到“steam_to_file”方法的弃用警告。
代码 -
from openai import OpenAI
from pathlib import Path
client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
)
speech_file_path = Path(__file__).parent / "speech.mp3"
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input= '''I see skies of blue and clouds of white
The bright blessed days, the dark sacred nights
And I think to myself
What a wonderful world
'''
)
response.stream_to_file(speech_file_path)
Run Code Online (Sandbox Code Playgroud)
IDE——Visual Studio 代码
警告如下 -
** DeprecationWarning:由于错误,此方法实际上并不传输响应内容,.with_streaming_response.method()应使用 response.stream_to_file("song.mp3")**
有人可以帮忙吗?
我尝试检查不同的论坛,但找不到与stream_to_file相关的错误。
我正在使用Python 3.12
我有一个 tvOS 项目,包含一个 App 目标和 3 个静态库:
\nEntryPoint \xe2\x80\x93 包含 main 、 AppDelegate 和 SceneDelegate 的静态库
\n体验包含我的 UI 元素的 \xe2\x80\x93 静态库
\n使用上述两个库构建的 AppTarget \xe2\x80\x93 可执行文件
\n我有一个“SelectionTable”类,它是体验目标中 UITableView 的子类:
\nimport UIKit\n\n class SelectionTable : UITableView\n{\n private var vDataSrc:[String]!\n\n func SetDataSrc (_ pDataSrc:[String])\n {\n self.vDataSrc = pDataSrc\n }\n\n func UpdateDataSrc (_ pStringList:[String])\n {\n self.vDataSrc += pStringList\n }\n\n func GetDataSrc () -> [String]\n {\n return self.vDataSrc\n }\n}Run Code Online (Sandbox Code Playgroud)\r\n我没有在任何地方使用此类,但在构建 AppTarget 时仍然遇到这些错误:
\n\n …
Go 1.22 今天发布了。根据发行说明,
“For”循环现在的范围可以是整数。例如:
Run Code Online (Sandbox Code Playgroud)package main import "fmt" func main() { for i := range 10 { fmt.Println(10 - i) } fmt.Println("go1.22 has lift-off!") }详细信息请参阅规格。
我想了解详细信息,但似乎规范根本没有解释语法。
我添加了#language-lawyer标签,因为我想要严格的理解。
考虑这个简短的 GHCi 会议:
ghci> import Data.Ratio
ghci> import Data.Word
ghci> 128 % 3 + 127 % 3 :: Ratio Word8
253 % 9
Run Code Online (Sandbox Code Playgroud)
为什么结果是253 % 9而不是255 % 3 (= 85 % 1)?
这确实是我的问题,但我很乐意详细说明。
首先,如果我删除类型,结果就是我所期望的:
ghci> 128 % 3 + 127 % 3
85 % 1
Run Code Online (Sandbox Code Playgroud)
类型Word8似乎很重要。我知道潜在的整数溢出,但即便如此,我也无法理解结果。例如
ghci> 128 + 127 :: Word8
255
Run Code Online (Sandbox Code Playgroud)
这里没有溢出。这首先发生在
ghci> 128 + 128 :: Word8
0
ghci> 128 + 129 :: Word8
1
Run Code Online (Sandbox Code Playgroud)
如果我除以二而不是三,我仍然可以理解结果:
ghci> 128 % 2 + …Run Code Online (Sandbox Code Playgroud) 寻找一种方法(也可以使用 Apache libs),将 Java 中的两个 String 与可能的 null/空值进行比较,其中主要要求是当一个为空null而另一个为空时(""),它将返回true。
强调 -允许任何空/空组合,并且所有组合都将被视为一个true语句。
one | two | res
------|------|-----
"" | "" | true
"" | null | true
null | "" | true
null | null | true
Run Code Online (Sandbox Code Playgroud)
我在这里检查了很多相关问题,但他们没有考虑我的主要要求。