V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  MoYi123  ›  全部回复第 2 页 / 共 20 页
回复总数  400
1  2  3  4  5  6  7  8  9  10 ... 20  
30 天前
回复了 Nazz 创建的主题 Go 编程语言 抛块砖演示下 range over func 用法
type Node struct {
Left *Node
Right *Node
Val int
}
type TreeVisiter struct {
root *Node
}

func makeNode(val int) *Node {
return &Node{Val: val}
}

func (fr *TreeVisiter) InOrder(visit func(*Node) bool) {
fr.p_inOrder(visit, fr.root)
}

func (fr *TreeVisiter) p_inOrder(visit func(*Node) bool, cur *Node) {
if cur.Left != nil {
if !visit(cur.Left) {
return
}
}
if !visit(cur) {
return
}
if cur.Right != nil {
if !visit(cur.Right) {
return
}
}
}

func main() {
root := makeNode(2)
root.Left = makeNode(1)
root.Right = makeNode(3)
tv := TreeVisiter{root: root}
for node := range tv.InOrder {
if node.Val == 1 {
fmt.Printf("first node is %d\n", node.Val)
}
if node.Val == 2 {
fmt.Printf("second node is %d\n", node.Val)
break
}
}
}


中序遍历二叉树, 这个至少比 cpp 的 begin,end 好多了,
我公司项目里写的 cpp 的用来遍历树的 forward_iterator 写了快 300 行, 虽然情况也比这个复杂很多.
是 levenshtein distance 吧, 大概.
要 std::is_trivially_copyable_v 的 struct 才能 memcpy, 更别说你这是 2 个不同的 struct 了.
@Nazz 如果这个数据结构基于硬盘, 提供的接口是 async 的, 这种传入回调函数的方式可以大幅减少 await 的次数, 性能会比 await iter.next() 好很多.

虽然 go 里用不到这个.
Backward 里的 func 套 func 是函数式编程里的常见的惰性求值的写法. 不觉得有什么问题.
33 天前
回复了 assassing 创建的主题 程序员 如何理解 Go 语言中作用域延伸?
c++的例子有文档吗? 我测试了 clang++ 18.1.6 和 g++ 14.0.1 在 else 里都能访问 b, 和 go 是一样的.
33 天前
回复了 chanlk 创建的主题 程序员 求解一个蛮奇怪的搜索场景, leetcode medium?
andAll 和 orAll 能互相嵌套吗? 它好像没说不能.
34 天前
回复了 abc0def 创建的主题 程序员 一道 10 年前面试问到的算法题
有 review 机制, CI 里没有 fmt 吗? 如果符合 fmt, 手动对齐又有什么影响?
36 天前
回复了 gosky 创建的主题 Python Python “安全”序列化复杂对象的问题
@gosky 所以需要你给一些例子啊.
36 天前
回复了 gosky 创建的主题 Python Python “安全”序列化复杂对象的问题
问题最关键的 param 是怎么样子的, 让你用 param: "..." 这样三个点跳过了.
优化器没写好呗, 能报 bug 的话就报一下, 这种应该很好修.
42 天前
回复了 Betsy 创建的主题 C++ 求教个 C++ Get 函数怎么写的问题
一般用
const Student& Get(const std::string& key) const { return this->map_.at(key); }
这样拷贝构造发生在外部.

如果有需要再加上 Student& Get(const std::string& key) const { return this->map_.at(key); }
可以支持 move

Status Get(const std::string& key, Student* value); 这种写法是 C 语言的风格. 不建议用
43 天前
回复了 shendaowu 创建的主题 MySQL 求写一段生成数据库测试数据的代码
几天时间能写入一千万行, 还要考虑利用硬件?
不搞特殊优化的测试数据导入速度是 1 秒 10-20 万. 一千万也就 1 分钟左右.
44 天前
回复了 Betsy 创建的主题 C++ 求教个神奇的 C++ 打印问题
编译有警告
main.cpp:43:46: warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl]
const ReducedGroupId& reduced_group_id = memo.GetReduceGroupId(group_id).value();

运行有 asan
==7760==ERROR: AddressSanitizer: stack-use-after-scope on address 0x00016ba93320 at pc 0x00010436df8c bp 0x00016ba93250 sp 0x00016ba93248
READ of size 8 at 0x00016ba93320 thread T0
#0 0x10436df88 in main+0x610 (a.out:arm64+0x100001f88)
#1 0x187dbe0dc (<unknown module>)

Address 0x00016ba93320 is located in stack of thread T0 at offset 192 in frame
#0 0x10436d984 in main+0xc (a.out:arm64+0x100001984)

This frame has 7 object(s):
[32, 40) 'ref.tmp.i.i124'
[64, 72) 'ref.tmp.i.i103'
[96, 104) 'ref.tmp.i.i'
[128, 152) 'tmp'
[192, 208) 'ref.tmp' <== Memory access at offset 192 is inside this variable
[224, 232) 'ref.tmp15'
[256, 264) 'ref.tmp28'
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-use-after-scope (a.out:arm64+0x100001f88) in main+0x610
49 天前
回复了 youngxxx 创建的主题 商业模式 有人搞过少儿/青少年 编程培训没?
codeforces 先上个 2100 分吧, 不然可能不配教初中生竞赛.
52 天前
回复了 assassing 创建的主题 程序员 Go 语言存在隐式类型转换?
1.5e3 这是个字面量, 编译的时候会根据上下文推一个类型出来, 如果推不出来就是 float.
1  2  3  4  5  6  7  8  9  10 ... 20  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1874 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 42ms · UTC 16:26 · PVG 00:26 · LAX 09:26 · JFK 12:26
Developed with CodeLauncher
♥ Do have faith in what you're doing.