standard-library

Why does the reverse() function in the Swift standard library return ReverseRandomAccessCollection?

懵懂的女人 提交于 2019-11-26 20:39:17
问题 Now that I've learned Swift (to a reasonable level) I'm trying to get to grips with the standard library, but in truth it's mainly ελληνικά to me! So a specific question: I have an array of strings and I can call reverse() on it. let arr = ["Mykonos", "Rhodes", "Naxos"].reverse() Now naively I thought I'd get back a type of Array from this. (Ruby for example has a similar method that you pass an array and get back an array) But arr is now actually of type ReverseRandomAccessCollection<Array

Reading a text file backwards in C

点点圈 提交于 2019-11-26 19:06:34
What's the best way to read a file backwards in C? I know at first you may be thinking that this is no use whatsoever, but most logs etc. append the most recent data at the end of the file. I want to read in text from the file backwards, buffering it into lines - that is abc def ghi should read ghi , def , abc in lines. So far I have tried: #include <stdio.h> #include <stdlib.h> void read_file(FILE *fileptr) { char currentchar = '\0'; int size = 0; while( currentchar != '\n' ) { currentchar = fgetc(fileptr); printf("%c\n", currentchar); fseek(fileptr, -2, SEEK_CUR); if( currentchar == '\n') {

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

不羁的心 提交于 2019-11-26 18:47:53
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). 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 binary search algorithm for lists. Mike DeSimone recommended sets and dicts and you explained why lists are too

Why is it OK to return vector from function?

孤街浪徒 提交于 2019-11-26 18:21:17
Please consider this code. I have seen this type of code several times. words is a local vector. How is it possible to return it from a function? Can we guarantee it will not die? std::vector<std::string> read_file(const std::string& path) { std::ifstream file("E:\\names.txt"); if (!file.is_open()) { std::cerr << "Unable to open file" << "\n"; std::exit(-1); } std::vector<string> words;//this vector will be returned std::string token; while (std::getline(file, token, ',')) { words.push_back(token); } return words; } Can we guarantee it will not die? As long there is no reference returned, it's

What is std::promise?

孤者浪人 提交于 2019-11-26 18:02:23
I'm fairly familiar with C++11's std::thread , std::async and std::future components (e.g. see this answer ), which are straight-forward. However, I cannot quite grasp what std::promise is, what it does and in which situations it is best used. The standard document itself doesn't contain a whole lot of information beyond its class synopsis, and neither does just::thread . Could someone please give a brief, succinct example of a situation where an std::promise is needed and where it is the most idiomatic solution? Jonathan Wakely In the words of [futures.state] a std::future is an asynchronous

How to access a standard-library module in Python when there is a local module with the same name?

丶灬走出姿态 提交于 2019-11-26 16:40:46
How can a standard-library module (say math) be accessed when a file prog.py is placed in the same directory as a local module with the same name (math.py)? I'm asking this question because I would like to create a package uncertainties that one can use as import uncertainties from uncertainties.math import * Thus, there is a local math module inside the uncertainties directory. The problem is that I want to access the standard library math module from uncertainties/__init__.py. I prefer not to rename uncertainties.math because this module is precisely intended to replace functions from the

How to flatten a list to a list without coercion?

徘徊边缘 提交于 2019-11-26 16:09:25
I am trying to achieve the functionality similar to unlist, with the exception that types are not coerced to a vector, but the list with preserved types is returned instead. For instance: flatten(list(NA, list("TRUE", list(FALSE), 0L)) should return list(NA, "TRUE", FALSE, 0L) instead of c(NA, "TRUE", "FALSE", "0") which would be returned by unlist(list(list(NA, list("TRUE", list(FALSE), 0L)) . As it is seen from the example above, the flattening should be recursive. Is there a function in standard R library which achieves this, or at least some other function which can be used to easily and

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

北慕城南 提交于 2019-11-26 15:37:56
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? 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 vulnerability . For similar reasons, one should use strncpy() in place of strcpy() and strncat() in place of

Concatenating strings doesn&#39;t work as expected [closed]

我怕爱的太早我们不能终老 提交于 2019-11-26 15:27:04
问题 I know it is a common issue, but looking for references and other material I don't find a clear answer to this question. Consider the following code: #include <string> // ... // in a method std::string a = "Hello "; std::string b = "World"; std::string c = a + b; The compiler tells me it cannot find an overloaded operator for char[dim] . Does it mean that in the string there is not a + operator? But in several examples there is a situation like this one. If this is not the correct way to

How to check if a file exists in Go?

不羁的心 提交于 2019-11-26 14:57:54
问题 Go's standard library does not have a function solely intended to check if a file exists or not (like Python's os.path.exists). What is the idiomatic way to do it? 回答1: To check if a file doesn't exist, equivalent to Python's if not os.path.exists(filename) : if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) { // path/to/whatever does not exist } To check if a file exists, equivalent to Python's if os.path.exists(filename) : Edited: per recent comments if _, err := os.Stat("/path