standard-library

random_shuffle algorithm - are identical results produced without random generator function?

天大地大妈咪最大 提交于 2019-12-30 08:41:12
问题 If a random generator function is not supplied to the random_shuffle algorithm in the standard library, will successive runs of the program produce the same random sequence if supplied with the same data? For example, if std::random_shuffle(filenames.begin(), filenames.end()); is performed on the same list of filenames from a directory in successive runs of the program, is the random sequence produced the same as that in the prior run? 回答1: 25.2.11 just says that the elements are shuffled

Alternative implementations of Haskell's standard library type classes

北城以北 提交于 2019-12-30 05:48:06
问题 I've seen many people complaining about some of the type classes from the standard library saying things like "Monad should require Functor" or even "Monad should require Applicative", "Applicative should require Pointed", "Num shouldn't require Show", etc, So, I have some questions: Are there arguments for the way the tree of type class dependencies have those "flaws" perceived by the community or is this just the result of how things were done historically? How drastically a change in this

Does std::multiset guarantee insertion order?

…衆ロ難τιáo~ 提交于 2019-12-29 05:57:30
问题 I have a std::multiset which stores elements of class A . I have provided my own implementation of operator< for this class. My question is if I insert two equivalent objects into this multiset is their order guaranteed? For example, first I insert a object a1 into the set and then I insert an equivalent object a2 into this set. Can I expect the a1 to come before a2 when I iterate through the set? If no, is there any way to achieve this using multiset? 回答1: In C++03 you are not guaranteed

C++11 Thread waiting behaviour: std::this_thread::yield() vs. std::this_thread::sleep_for( std::chrono::milliseconds(1) )

牧云@^-^@ 提交于 2019-12-29 03:34:06
问题 I was told when writing Microsoft specific C++ code that writing Sleep(1) is much better than Sleep(0) for spinlocking, due to the fact that Sleep(0) will use more of the CPU time, moreover, it only yields if there is another equal-priority thread waiting to run. However, with the C++11 thread library, there isn't much documentation (at least that I've been able to find) about the effects of std::this_thread::yield() vs. std::this_thread::sleep_for( std::chrono::milliseconds(1) ) ; the second

Getting a machine's external IP address with Python

人盡茶涼 提交于 2019-12-27 16:40:52
问题 Looking for a better way to get a machines current external IP #... Below works, but would rather not rely on an outside site to gather the information ... I am restricted to using standard Python 2.5.1 libraries bundled with Mac OS X 10.5.x import os import urllib2 def check_in(): fqn = os.uname()[1] ext_ip = urllib2.urlopen('http://whatismyip.org').read() print ("Asset: %s " % fqn, "Checking in from IP#: %s " % ext_ip) 回答1: If you are behind a router which obtains the external IP, I'm

Python's standard library - is there a module for balanced binary tree?

冷暖自知 提交于 2019-12-27 12:19:51
问题 Is there a module for AVL or Red-Black or some other type of a balanced binary tree in the standard library of Python? I have tried to find one, but unsuccessfully (I'm relatively new to Python). 回答1: No, there is not a balanced binary tree in the stdlib. However, from your comments, it sounds like you may have some other options: You say that you want a BST instead of a list for O(log n) searches. If searching is all you need and your data are already sorted, the bisect module provides a

Which functions from the standard library must (should) be avoided?

依然范特西╮ 提交于 2019-12-27 11:02:04
问题 I've read on Stack Overflow that some C functions are "obsolete" or "should be avoided". Can you please give me some examples of this kind of function and the reason why? What alternatives to those functions exist? Can we use them safely - any good practices? 回答1: Deprecated Functions Insecure A perfect example of such a function is gets(), because there is no way to tell it how big the destination buffer is. Consequently, any program that reads input using gets() has a buffer overflow

InputMismatchException Error

限于喜欢 提交于 2019-12-25 05:04:07
问题 I am receiving a compile time error saying: No exception of type InputMismatchException can be thrown; an exception type must be a subclass of Throwable InputMismatchException.java As far as I'm aware InputMismatchException is an exception thrown by the Scanner when it receives invalid input, why then is this error preventing me from compiling? import java.util.*; public class InputMismatchException { public static void main(String[] args) { boolean continueInput = true; Scanner input = new

Does const containers have only const iterator?

我只是一个虾纸丫 提交于 2019-12-24 13:15:36
问题 Why do const STL containers only return const_iterator s? For example both std::vector and std::list have the method begin overloaded as: iterator begin(); const_iterator begin() const; const_iterator cbegin() const; I thought I could still modify values of a const vector but not the vector itself. According to the standard library there is no difference between: const std::vector<int> and const std::vector<const int> 回答1: Suppose you have iterator begin() const; instead of const_iterator

Is There a Standard Algorithm to Copy Until?

独自空忆成欢 提交于 2019-12-24 05:52:51
问题 I am using an istream_iterator<char> it , so I cannot iterate over the range in reverse (or iterate over it twice, without a great deal of hastle.) I want to copy until a condition is met. Is there something that would work like this in the standard library: copy_until(it, istream_iterator<char>(), ostream_iterator<char>(cout), [](const unsigned char i){ return isalpha(i); }) If I have to roll something I can I was just hoping for some magic that I haven't been able to figure out. EDIT: The