小编Joh*_*ann的帖子

uint8_t iostream行为

摘要:我期待代码:cout << uint8_t(0); 打印"0",但它不打印任何东西.

长版本:当我尝试将uint8_t对象流式传输到cout时,我会使用gcc获得奇怪的字符.这是预期的行为吗?可能是因为uint8_t是某些基于字符的类型的别名吗?请参阅代码示例中的编译器/系统说明.

// compile and run with:
// g++ test-uint8.cpp -std=c++11 && ./a.out
//                    -std=c++0x (for older gcc versions)
/**
 * prints out the following with compiler:
 *     gcc (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)
 * on the system:
 *     Linux 3.7.9-101.fc17.x86_64
 * Note that the first print statement uses an unset uint8_t
 * and therefore the behaviour is undefined. (Included here for
 * completeness)

> g++ test-uint8.cpp -std=c++11 && ./a.out
>>>?<<<    >>>194<<<
>>><<<    >>>0<<<
>>><<<    >>>0<<< …
Run Code Online (Sandbox Code Playgroud)

c++ iostream c++11

13
推荐指数
2
解决办法
5633
查看次数

如何将 WSL 中的环境变量传递到 Windows 可执行文件中

从 Linux (v1) Alpine bash 终端的 Windows 子系统中,我想设置一个环境变量,该变量被传递到 Windows 可执行文件中。有没有办法做到这一点?

我希望打印“Hello, World!”的示例:

windows-10:~# export X=World
windows-10:~# cmd.exe /c 'echo Hello, %X%!'
Hello, %X%!
Run Code Online (Sandbox Code Playgroud)

请参阅下面菲利普的回答。

这是来自https://docs.microsoft.com/en-us/windows/wsl/interop的相关信息的副本

在 Windows 和 WSL 之间共享环境变量

Available in Windows Insider builds 17063 and later.
Run Code Online (Sandbox Code Playgroud)

在 17063 之前,只有 WSL 可以访问的 Windows 环境变量是 PATH(因此您可以从 WSL 下启动 Win32 可执行文件)。

从 17063 年开始,WSL 和 Windows 共享 WSLENV,这是一个特殊的环境变量,用于连接在 WSL 上运行的 Windows 和 Linux 发行版。

WSLENV 的属性:

It is shared; it exists in both Windows and WSL environments.
It is …
Run Code Online (Sandbox Code Playgroud)

bash environment-variables windows-subsystem-for-linux

4
推荐指数
1
解决办法
3707
查看次数

std :: unordered_multiset的用例

我想知道为什么会用std::unordered_multiset。我的猜测是它与插入/擦除后迭代器的无效或非无效有关,但也许更深层吗?非常相似的问题在这里:std :: multimap的用例,但更多是关于地图的讨论。

stl hashset c++11 unordered-multiset

3
推荐指数
1
解决办法
1379
查看次数

python coverage模块可以有条件地忽略单元测试中的行吗?

使用nosetests和coverage模块,我希望代码的覆盖率报告能够反映正在测试的版本.考虑以下代码:

import sys
if sys.version_info < (3,3):
    print('older version of python')
Run Code Online (Sandbox Code Playgroud)

当我在python版本3.5中测试时,print()显示为未经测试.我希望覆盖范围忽略该行,但仅当我使用python版本3.3+进行测试时

有没有办法# pragma: no coverprint()声明中做一些事情只是为了什么时候sys.version_info不小于(3,3)?实际上,我想做这样的事情:

import sys
if sys.version_info < (3,3):
    print('older version of python') # pragma: [py26,py27,py32] no cover
Run Code Online (Sandbox Code Playgroud)

python unit-testing code-coverage python-coverage

3
推荐指数
2
解决办法
1525
查看次数

使用std :: ostream在每行之前插入文本

我想知道是否可以从std :: ostream继承,并以某种方式覆盖flush(),以便将某些信息(例如行号)添加到每行的开头.然后我想通过rdbuf()将它附加到std :: ofstream(或cout),这样我得到这样的东西:

ofstream fout("file.txt");
myostream os;
os.rdbuf(fout.rdbuf());

os << "this is the first line.\n";
os << "this is the second line.\n";
Run Code Online (Sandbox Code Playgroud)

会把它放到file.txt中

1 this is the first line.
2 this is the second line.
Run Code Online (Sandbox Code Playgroud)

c++ iostream stl

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