百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
I

iter

> 编程语言
开源

C++ STL 迭代器和算法的 Go 实现。

198 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

C++ STL 迭代器和算法的 Go 实现。

iter

Note: The generic v2 API is still under development and has not been released. It requires Go 1.26 or later. For the previous version, please check the v1 branch.

Go implementation of C++ STL iterators and algorithms.

Less hand-written loops, more expressive code.

README translations: 简体中文

Motivation

Go now has generics, and we deserve to have reusable general algorithms. iter helps improve Go code in several ways:

  • Some simple loops are unlikely to be wrong or inefficient, but calling an algorithm instead will make the code more concise and easier to comprehend. Such as AllOf, FindIf, Accumulate.

  • Some algorithms are not complicated, but it is not easy to write them correctly. Reusing code makes them easier to reason for correctness. Such as Shuffle, Sample, Partition.

  • STL also includes some complicated algorithms that may take hours to make it correct. Implementing it manually is impractical. Such as NthElement, StablePartition, NextPermutation.

  • The implementation in the library contains some imperceptible performance optimizations. For instance, MinmaxElement is done by taking two elements at a time. In this way, the overall number of comparisons is significantly reduced.

There are alternative libraries have similar goals, such as gostl, gods and go-stp. What makes iter unique is:

  • Non-intrusive. Instead of introducing new containers, iter tends to reuse existed containers in Go (slice, string, list.List, etc.) and use iterators to adapt them to algorithms.

  • Full algorithms (>100). It includes almost all algorithms come before C++17. Check the Full List.

Examples

The examples keep package qualifiers so the generic v2 API is explicit. See examples_test.go for complete, executable examples.

Print a list.List

go
l := list.New()
for i := 1; i ")
  }
}
// Output:
// 1->2->3->4->5
go
l := list.New()
algo.GenerateN(lists.ListBackInserter[int](l), 5, iter.IotaGenerator(1))
algo.Copy(lists.Begin[int](l), lists.End[int](l), iter.IOWriter[int](os.Stdout, "->"))
// Output:
// 1->2->3->4->5

Reverse a string

go
s := "!dlrow olleH"
var sb strings.Builder
for i := len(s) - 1; i >= 0; i-- {
  sb.WriteByte(s[i])
}
fmt.Println(sb.String())

b := []byte(s)
for i := len(s)/2 - 1; i >= 0; i-- {
  j := len(s) - 1 - i
  b[i], b[j] = b[j], b[i]
}
fmt.Println(string(b))
// Output:
// Hello world!
// Hello world!
go
s := "!dlrow olleH"
fmt.Println(strs.MakeString(strs.RBegin(s), strs.REnd(s)))

b := []byte(s)
iterslices.Reverse(b)
fmt.Println(string(b))
// Output:
// Hello world!
// Hello world!

In-place deduplicate (from SliceTricks, with minor change)

go
in := []int{3, 2, 1, 4, 3, 2, 1, 4, 1}
sort.Ints(in)
j := 0
for i := 1; i  y })
algo.Copy(iterslices.Begin(top), iterslices.End(top), iter.IOWriter[int](os.Stdout, ", "))

Print all permutations of ["a", "b", "c"]

go
// Usually requires some sort of recursion.
go
s := []string{"a", "b", "c"}
for ok := true; ok; ok = iterslices.NextPermutation(s) {
  fmt.Println(s)
}
// Output:
// [a b c]
// [a c b]
// [b a c]
// [b c a]
// [c a b]
// [c b a]

Thanks

  • cppreference.com
  • LLVM libc++

License

BSD 3-Clause

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Goalgorithmsdatastructuresstl

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言