standard-library

Why does ofstream require a flush?

妖精的绣舞 提交于 2019-11-30 04:22:26
问题 If I run the following code, no file is created at all: std::ofstream outputFile(strOutputLocation.c_str(), std::ios::binary); outputFile.write((const char*)lpResLock, dwSizeRes); outputFile.close(); However, if I add a flush() before the close, it works: std::ofstream outputFile(strOutputLocation.c_str(), std::ios::binary); outputFile.write((const char*)lpResLock, dwSizeRes); outputFile.flush(); outputFile.close(); Does the standard library actually require this, or is it a bug in the Visual

Python negate boolean function

孤者浪人 提交于 2019-11-30 04:06:20
问题 A python boolean function can easily be negated with lambda functions, but it's a bit verbose and hard to read for something so basic, for example: def is_even(n): return n % 2 == 0 odds_under_50 = filter(lambda x: not is_even(x), range(50)) I'm wondering if there is a function to do this in the standard library, which might look like: odds_under_50 = filter(negate(is_even), range(50)) 回答1: As far as I know there is no builtin function for that, or a popular library that does that.

Why does OpenURI treat files under 10kb in size as StringIO?

青春壹個敷衍的年華 提交于 2019-11-30 03:31:50
I fetch images with open-uri from a remote website and persist them on my local server within my Ruby on Rails application. Most of the images were shown without a problem, but some images just didn't show up. After a very long debugging-session I finally found out (thanks to this blogpost ) that the reason for this is that the class Buffer in the open-uri-libary treats files with less than 10kb in size as IO-objects instead of tempfiles. I managed to get around this problem by following the answer from Micah Winkelspecht to this StackOverflow question , where I put the following code within a

What does the “c” mean in cout, cin, cerr and clog?

流过昼夜 提交于 2019-11-30 02:39:43
What does the "c" mean in the cout, cin, cerr and clog names? I would say char but I haven't found anything to confirm it. fredoverflow The "c" stands for "character" because iostreams map values to and from byte (char) representations. [ Bjarne Stroustrup's C++ Style and Technique FAQ ] JRL I originally guessed console , and this link confirmed it. But after seeing the quote from Stroustrup , it seems that's a misconception, and that the c stands for character . One thing in favor of that theory that can serve as an indicator is the fact that for each stream object (cin, cout, cerr, etc.)

Embed Python3 without standard library

 ̄綄美尐妖づ 提交于 2019-11-30 01:44:17
EDIT: I have asked an opposing question here: How to embed Python3 with the standard library A solution for Python2 is provided here: Is it possible to embed python without the standard library? However, Python3 fails on Py_Initialize(); with: Fatal Python error: Py_Initialize: unable to load the file system codec ImportError: No module named 'encodings' This makes sense because py3 source files are utf-8 by default. So it seems that it requires an external binary just in order to parse py3 source files. So what to do? It looks as though I need to locate the encodings binary in my system

Where to find algorithms for standard math functions?

别等时光非礼了梦想. 提交于 2019-11-30 01:34:32
I'm looking to submit a patch to the D programming language standard library that will allow much of std.math to be evaluated at compile time using the compile-time function evaluation facilities of the language. Compile-time function evaluation has several limitations, the most important ones being: You can't use assembly language. You can't call C code or code for which the source is otherwise unavailable. Several std.math functions violate these and compile-time versions need to be written. Where can I get information on good algorithms for computing things such as logarithms, exponents,

When should I use std::bind?

拟墨画扇 提交于 2019-11-29 22:54:20
Every time I need to use std::bind , I end up using a lambda instead. So when should I use std::bind ? I just finished removing it from one codebase, and I found that lambdas were always simpler and clearer than std::bind . Isn't std::bind completely unnecessary? Shouldn't it be deprecated in the future? When should I prefer std::bind to lambda functions? (There has to be a reason that it got into the standard at the same time as lambdas.) I've also noticed that more and more people are familiar with lambdas (so they know what lambdas do). However, a lot fewer people are familiar with std:

What are the differences amongst Python's “__get*__” and “_del*__” methods?

浪尽此生 提交于 2019-11-29 20:12:00
I just started learning Python a few months ago, and I'm trying to understand the differences between the different __get*__ methods: __get__ __getattr__ __getattribute__ __getitem___ And their __del*__ equivalents: __del__ __delattr__ __delete__ __delitem__ What are the differences between these? When should I use one over the other? Is there a specific reason why most of the __get*__ methods have __set*__ equivalents, but there is no __setattribute__ ? Rik Poggi The documentation for every method that you listed is easly reachable from the documentation index . Anyway this may be a little

c++ valgrind shows memory leak in hello world [duplicate]

对着背影说爱祢 提交于 2019-11-29 14:03:52
This question already has an answer here: Valgrind: Memory still reachable with trivial program using <iostream> 3 answers Code of my program is #include <iostream> int main(int argc, const char *argv[]) { std::cout << "hello world!\n"; return 0; } I compiled it with flags -Wpedantic -pedantic-errors -std=c++11 -g -Wall -Wextra Run Valgrind on it and saw something strange, this simple program has memory leak, output of valgrind --leak-check=full --show-leak-kinds=all command is ==4492== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1 ==4492== at 0x4C28C20: malloc (vg_replace

Why do iterators need to be default-constructible

旧街凉风 提交于 2019-11-29 10:40:26
问题 Iterators of the categories forward , bidirectional , and random access need to be default-constructible. Why is this, and why do input and output operators not have to be default-constructible? 回答1: Forward iterators and stronger are required to refer to some external sequence (see [forward.iterators]/6 which says "If a and b are both dereferenceable, then a == b if and only if *a and *b are bound to the same object.") This means they are generally just a lightweight handle onto something