std

std::ofstream, check if file exists before writing

六月ゝ 毕业季﹏ 提交于 2019-12-17 15:34:16
问题 I am implementing file saving functionality within a Qt application using C++. I am looking for a way to check to see if the selected file already exists before writing to it, so that I can prompt a warning to the user. I am using an std::ofstream and I am not looking for a Boost solution. 回答1: This is one of my favorite tuck-away functions I keep on hand for multiple uses. #include <sys/stat.h> // Function: fileExists /** Check if a file exists @param[in] filename - the name of the file to

Compiler does not deduce template parameters (map std::vector -> std::vector)

对着背影说爱祢 提交于 2019-12-17 13:53:52
问题 I have the following template. template<typename T, typename U> std::vector<U> map(const std::vector<T> &v, std::function<U(const T&)> f) { std::vector<U> res; res.reserve(v.size()); std::transform(std::begin(v), std::end(v), std::end(res), f); return res; } When I use it in my code I have the specify the template parameters. Why is the compiler not able to deduce this for me? How do I have to change my template definition to make this work? vector<int> numbers = { 1, 3, 5 }; // vector<string

How to make sure that std::random_shuffle always produces a different result?

蹲街弑〆低调 提交于 2019-12-17 12:47:11
问题 Is there some function, similar to srand() , that I need to call to make sure that std::random_shuffle() always produces different results? i.e. if I call it several times with the same data, I want the order to be different every time. How can I make sure of that? 回答1: std::random_shuffle has two forms. One that takes 2 arguments (begin/end iterators), and one that takes 3 (begin/end iterator and a random generator). The first form uses std::rand() , so you would use std::srand() to seed it

How can I get a list of all the Python standard library modules

▼魔方 西西 提交于 2019-12-17 10:43:21
问题 I want something like sys.builtin_module_names except for the standard library. Other things that didn't work: sys.modules - only shows modules that have already been loaded sys.prefix - a path that would include non-standard library modules EDIT: and doesn't seem to work inside a virtualenv. The reason I want this list is so that I can pass it to the --ignore-module or --ignore-dir command line options of trace http://docs.python.org/library/trace.html So ultimately, I want to know how to

std::vector resize downward

大城市里の小女人 提交于 2019-12-17 09:35:42
问题 The C++ standard seems to make no statement regarding side-effects on capacity by either resize(n) , with n < size() , or clear() . It does make a statement about amortized cost of push_back and pop_back - O(1) I can envision an implementation that does the usual sort of capacity changes ala CLRS Algorithms (e.g. double when enlarging, halve when decreasing size to < capacity()/4 ). (Cormen Lieserson Rivest Stein) Does anyone have a reference for any implementation restrictions? 回答1: Calling

std::lexical_cast - is there such a thing?

空扰寡人 提交于 2019-12-17 08:34:26
问题 Does the C++ Standard Library define this function, or do I have to resort to Boost? I searched the web and couldn't find anything except Boost, but I thought I'd better ask here. 回答1: Only partially. C++11 <string> has std::to_string for the built-in types: [n3290: 21.5/7]: string to_string(int val); string to_string(unsigned val); string to_string(long val); string to_string(unsigned long val); string to_string(long long val); string to_string(unsigned long long val); string to_string(float

Trouble reading a line using fscanf()

做~自己de王妃 提交于 2019-12-17 07:32:08
问题 I'm trying to read a line using the following code: while(fscanf(f, "%[^\n\r]s", cLine) != EOF ) { /* do something with cLine */ } But somehow I get only the first line every time. Is this a bad way to read a line? What should I fix to make it work as expected? 回答1: It's almost always a bad idea to use the fscanf() function as it can leave your file pointer in an unknown location on failure. I prefer to use fgets() to get each line in and then sscanf() that. You can then continue to examine

What is the fastest way to change a key of an element inside std::map

懵懂的女人 提交于 2019-12-17 06:27:31
问题 I understand the reasons why one can't just do this (rebalancing and stuff): iterator i = m.find(33); if (i != m.end()) i->first = 22; But so far the only way (I know about) to change the key is to remove the node from the tree alltogether and then insert the value back with a different key: iterator i = m.find(33); if (i != m.end()) { value = i->second; m.erase(i); m[22] = value; } This seems rather inefficient to me for more reasons: traverses the tree three times (+ balance) instead of

Why would I ever use push_back instead of emplace_back?

给你一囗甜甜゛ 提交于 2019-12-17 06:20:37
问题 C++11 vectors have the new function emplace_back . Unlike push_back , which relies on compiler optimizations to avoid copies, emplace_back uses perfect forwarding to send the arguments directly to the constructor to create an object in-place. It seems to me that emplace_back does everything push_back can do, but some of the time it will do it better (but never worse). What reason do I have to use push_back ? 回答1: I have thought about this question quite a bit over the past four years. I have

std::shared_ptr thread safety

一笑奈何 提交于 2019-12-17 04:15:19
问题 I've read that "Multiple threads can simultaneously read and write different shared_ptr objects, even when the objects are copies that share ownership." (MSDN: Thread Safety in the Standard C++ Library) Does that mean that changing shared_ptr object is safe ? For an instance, is the next code considered safe: shared_ptr<myClass> global = make_shared<myClass>(); ... //In thread 1 shared_ptr<myClass> private = global; ... //In thread 2 global = make_shared<myClass>(); ... Can I be sure in that