小编vis*_*nda的帖子

如何用libc ++ istream_iterator读取文件中的0xFF?

请考虑以下示例代码:

#include <iostream>

using namespace std;

int main()
{
  istreambuf_iterator<char> eos;
  istreambuf_iterator<char> iit(cin.rdbuf());
  int i;
  for (i = 0; iit != eos; ++i, ++iit) {
    cout << *iit;
  }
  cout << endl << i << endl;
}
Run Code Online (Sandbox Code Playgroud)

以及包含以下内容的输入文件:"foo\xffbar":

$ hexdump testin
0000000 66 6f 6f ff 62 61 72
0000007
Run Code Online (Sandbox Code Playgroud)

现在使用clang libc ++ vs gnu libstdc ++进行测试:

$ make test
clang++ -std=c++11 -stdlib=libc++ -Wall -stdlib=libc++ -o bug-libcc bug.cpp
clang++ -std=c++11 -stdlib=libc++ -Wall -stdlib=libstdc++ -o bug-libstd bug.cpp
./bug-libcc < testin
foo …
Run Code Online (Sandbox Code Playgroud)

c++ istream-iterator libstdc++ libc++

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

为什么Golang在goroutines中处理不同的闭包?

考虑以下Golang代码(也在Go Playground上):

package main

import "fmt"
import "time"

func main() {
    for _, s := range []string{"foo", "bar"} {
        x := s
        func() {
            fmt.Printf("s: %s\n", s)
            fmt.Printf("x: %s\n", x)
        }()
    }
    fmt.Println()
    for _, s := range []string{"foo", "bar"} {
        x := s
        go func() {
            fmt.Printf("s: %s\n", s)
            fmt.Printf("x: %s\n", x)
        }()
    }
    time.Sleep(time.Second)
}
Run Code Online (Sandbox Code Playgroud)

此代码生成以下输出:

s: foo
x: foo
s: bar
x: bar

s: bar
x: foo
s: bar
x: bar
Run Code Online (Sandbox Code Playgroud)

假设这不是一些奇怪的编译器错误,我很好奇为什么a)s的值在goroutine版本中然后在常规func调用中被解释不同而b)以及为什么将它分配给循环内部的局部变量两种情况.

closures go

5
推荐指数
2
解决办法
2422
查看次数

标签 统计

c++ ×1

closures ×1

go ×1

istream-iterator ×1

libc++ ×1

libstdc++ ×1