如何print("Some text")在UTF-8中使用python 3(3.1)到stdout,或者如何输出原始字节?
TestText = "Test - ??????..šŠ??žŽ" # this is UTF-8
TestText2 = b"Test2 - \xc4\x81\xc4\x80\xc4\x93\xc4\x92\xc4\x8d\xc4\x8c..\xc5\xa1\xc5\xa0\xc5\xab\xc5\xaa\xc5\xbe\xc5\xbd" # just bytes
print(sys.getdefaultencoding())
print(sys.stdout.encoding)
print(TestText)
print(TestText.encode("utf8"))
print(TestText.encode("cp1252","replace"))
print(TestText2)
Run Code Online (Sandbox Code Playgroud)
输出(在CP1257和I中将字符替换为字节值[x00]):
utf-8
cp1257
Test - [xE2][xC2][xE7][C7][xE8][xC8]..[xF0][xD0][xFB][xDB][xFE][xDE]
b'Test - \xc4\x81\xc4\x80\xc4\x93\xc4\x92\xc4\x8d\xc4\x8c..\xc5\xa1\xc5\xa0\xc5\xab\xc5\xaa\xc5\xbe\xc5\xbd'
b'Test - ??????..\x9a\x8a??\x9e\x8e'
b'Test2 - \xc4\x81\xc4\x80\xc4\x93\xc4\x92\xc4\x8d\xc4\x8c..\xc5\xa1\xc5\xa0\xc5\xab\xc5\xaa\xc5\xbe\xc5\xbd'
Run Code Online (Sandbox Code Playgroud)
print太聪明了......:D使用编码文本是没有意义的print(因为它总是只显示字节的表示而不是实际字节)并且根本不可能输出字节,因为无论如何打印并始终对其进行编码sys.stdout.encoding.
例如:print(chr(255))抛出错误:
Run Code Online (Sandbox Code Playgroud)Traceback (most recent call last): File "Test.py", line 1, in <module> print(chr(255)); File "H:\Python31\lib\encodings\cp1257.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode …
在Effective C++一书中,我看到了以下段落:
结果,如果你写
Run Code Online (Sandbox Code Playgroud)class Empty{};它基本上和你写的一样:
Run Code Online (Sandbox Code Playgroud)class Empty { public: Empty() { ... } Empty(const Empty& rhs) { ... } ~Empty() { ... } Empty& operator=(const Empty& rhs) { ... } // copy assignment operator };以下代码将导致生成每个函数:
Run Code Online (Sandbox Code Playgroud)Empty e1; Empty e2(e1); e2 = e1;
但是在拆解通过编译上面的代码创建的可执行文件之后,我意识到并非如此:没有任何函数被调用.
这是主要的汇编代码:
00000000004006cd <main>:
4006cd: 55 push %rbp
4006ce: 48 89 e5 mov %rsp,%rbp
4006d1: b8 00 00 00 00 mov $0x0,%eax
4006d6: 5d pop %rbp
4006d7: c3 retq
Run Code Online (Sandbox Code Playgroud)
段中没有任何名为"Empty"的函数.text.
那么在我们调用构造函数或赋值空类之后,编译器的行为究竟是什么?这本书说它会产生一些功能吗?如果是这样,他们存放在哪里?
我正在寻找一种简单的算法来"序列化"有向图.特别是我有一组在执行顺序上具有相互依赖性的文件,我想在编译时找到正确的顺序.我知道它必须是一件相当普遍的事情 - 编译器会一直这样做 - 但我的google-fu今天一直很弱.什么是'go-to'算法?
这是我的代码:
(function($){
$.fn.pluginbutton = function (options) {
myoptions = $.extend({ left: true });
return this.each(function () {
var focus = false;
if (focus === false) {
this.hover(function () {
this.animate({ backgroundPosition: "0 -30px" }, { duration: 0 });
this.removeClass('VBfocus').addClass('VBHover');
}, function () {
this.animate({ backgroundPosition: "0 0" }, { duration: 0 });
this.removeClass('VBfocus').removeClass('VBHover');
});
}
this.mousedown(function () {
focus = true
this.animate({ backgroundPosition: "0 30px" }, { duration: 0 });
this.addClass('VBfocus').removeClass('VBHover');
}, function () {
focus = false;
this.animate({ backgroundPosition: …Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
template<typename T>
void f(T&&) { cout << "f(T&&)" << endl; }
template<typename T>
void f(const T&&) { cout << "f(const T&&)" << endl; }
struct A {};
const A g1() { return {}; }
const int g2() { return {}; }
int main()
{
f(g1()); // outputs "f(const T&&)" as expected.
f(g2()); // outputs "f(T&&)" not as expected.
}
Run Code Online (Sandbox Code Playgroud)
问题描述嵌入在代码中.我的编译器是clang 5.0.
我只是好奇:
在这种情况下,为什么C++会以不同方式处理内置类型和自定义类型?
我有两种选择形式remote: true; 一个发送Ajax请求以执行create操作,另一个发送Ajax请求以执行destroy操作.
启用JavaScript后所有工作都会被罚款,但如果我禁用JavaScript,那么我点击,我收到此错误:
ActionController::InvalidAuthenticityToken PersonsController#create
Run Code Online (Sandbox Code Playgroud)
为什么会显示此错误,如何解决?
注意:我正在使用Rails 4
当我使用没有选项的普通表单时remote: true,rails会自动为身份验证令牌插入一个隐藏字段,但是当我remote: true在表单中使用时,HTML代码中没有这样的字段.看起来当有remote选项时,Rails会以不同的方式处理身份验证令牌,那么我如何才能在两种情况下都能使用它?
我开始用C语言了.我使用eclipse(juno)作为我的IDE并安装了CDT插件.我还解压缩了mingw64(GCC编译器).我写了一个非常简单的程序,看它是否有效.这是我的代码:
#include <stdio.h>
int main()
{
int age;
printf("Hello, please enter your age:\n");
scanf("%d", &age);
printf("Your age is %d", age);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是输出缓冲区填充了第一个字符串的值,printf但不会将其输出到控制台.我必须输入一个数字,然后只有缓冲区将所有数据倒入控制台,所以我看到控制台是这样的:
1
Hello, please enter your age:
Your age is 1
Run Code Online (Sandbox Code Playgroud)
而不是预期的:
Hello, please enter your age:
1
Your age is 1
Run Code Online (Sandbox Code Playgroud)
现在,我发现我可以fflush(stdout)在第一个之后使用,printf但我不认为这个解决方案是优雅的,甚至是必要的.关于如何克服这个问题的任何想法?
编辑 - 因为我在大学里学习这个,我不能使用课程中没有学到的东西,所以我只能使用printf和scanf
新编辑 - 我想我已经找到了解释.正如我所说,我正在输出到Eclipse内的控制台视图.奇怪的是,如果我从Windows的命令行编译并运行程序,我得到想要的结果.因此,我认为eclipse实际上是将输出写入文件并在控制台窗口中显示.如何强制eclipse在我的运行配置中打开一个真正的命令行窗口?
在运行时说,我想找出定义函数"printf"的位置.我该怎么做?我的第一次尝试是打印出"printf"的地址,并将其与进程的虚拟地址映射进行比较:
我的节目:
#include <stdio.h>
#include <unistd.h>
void main()
{
printf("address of printf is 0x%X\n", printf);
printf("pid is %d\n", getpid());
while (1);
}
Run Code Online (Sandbox Code Playgroud)
输出:
-bash-4.1$ ./a &
[1] 28837
-bash-4.1$ address of printf is 0x4003F8
pid is 28837
Run Code Online (Sandbox Code Playgroud)
但是,这表示该功能是在我自己的程序中定义的!
-bash-4.1$ head /proc/28837/maps
00400000-00401000 r-xp 00000000 08:06 6946857 /data2/temp/del/a <<<<<<< Address 0x4003F8 is in my own program?
00600000-00601000 rw-p 00000000 08:06 6946857 /data2/temp/del/a
397ec00000-397ec20000 r-xp 00000000 08:11 55837039 /lib64/ld-2.12.so
397ee1f000-397ee20000 r--p 0001f000 08:11 55837039 /lib64/ld-2.12.so
397ee20000-397ee21000 rw-p 00020000 08:11 55837039 /lib64/ld-2.12.so
397ee21000-397ee22000 …Run Code Online (Sandbox Code Playgroud) 我打印bool到这样的输出流:
#include <iostream>
int main()
{
std::cout << false << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
是否标准要求的流(例如特定结果0的false)?
这是什么技术问题std::shared_ptr::unique()是它在C++ 17中被弃用的原因?
根据cppreference.com,std::shared_ptr::unique()在C++ 17中被弃用
从C++ 17开始不推荐使用此函数,因为
use_count它只是多线程环境中的近似值.
我理解这是真的use_count() > 1:虽然我拿着它的引用,但其他人可能会同时放弃他或创建一个新副本.
但是如果use_count()返回1(这是我在调用时感兴趣的话unique())那么就没有其他线程可以以一种活泼的方式改变这个值,所以我希望这应该是安全的:
if (myPtr && myPtr.unique()) {
//Modify *myPtr
}
Run Code Online (Sandbox Code Playgroud)
我发现这个文件:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0521r0.html建议弃用以回应C++ 17 CD评论CA 14,但我找不到评论本身.
作为替代方案,该文件建议增加一些说明,包括以下内容:
注意:当多个线程可以影响返回值时
use_count(),结果应视为近似值.特别是,use_count() == 1并不意味着通过先前破坏的访问shared_ptr在任何意义上都已完成. - 结束说明
我理解这可能use_count()是当前指定方式的情况(由于缺乏保证的同步),但为什么分辨率不仅仅是指定这样的同步并因此使上述模式安全?如果有一个基本的限制,不允许这种同步(或使其成本高昂),那么如何才能正确实现析构函数?
我忽略了@ alexeykuzmin0和@rubenvb提出的明显案例,因为到目前为止我只unique()在shared_ptr其他线程本身无法访问的实例上使用过.因此,没有任何危险,特定的实例将以一种活泼的方式被复制.
我仍然有兴趣了解CA 14究竟是什么,因为我相信unique()只要保证与shared_ptr其他线程上的不同实例发生的任何事情同步,我的所有用例都会起作用.所以它对我来说似乎仍然是一个有用的工具,但我可能会忽略一些基本的东西.
为了说明我的想法,请考虑以下事项:
class MemoryCache {
public:
MemoryCache(size_t size)
: …Run Code Online (Sandbox Code Playgroud)