std

C++ std list sort with custom comparator that depends on an member variable for the object instance

独自空忆成欢 提交于 2019-12-19 08:50:15
问题 Class: Class: private: ... vector<string> words; vector< list<int> > vints; public: myFunction(...) I am calling a sort on non-empty list in another member function: void myClass::myFunction (...) { ... if (!vints[i].empty()) vints[i].sort(sortFunc); ... } My sorting function: bool myClass::sortFunc(const int& i, const int& j) { return (words[i] < words[j]); } The Error: error: no matching function for call to ‘std::list<int, std::allocator<int> >::sort(<unresolved overloaded function type>)’

creating an ostream

我的未来我决定 提交于 2019-12-19 08:05:22
问题 I am trying to create a c++ ostream for educational reasons. My test will be creating an ostream that acts like a ofstream would except instead of writing to a file it would write to a deque or vector container. 回答1: As it is for education, as you say, i will show you how i would do such a thingy. Otherwise, stringstream is really the way to go. Sounds like you want to create a streambuf implementation that then writes to a vector / deque. Something like this (copying from another answer of

Sort a std::list<myclass*> with myclass::operator<(myclass &other)

旧时模样 提交于 2019-12-19 06:18:26
问题 I have a std::list<myclass*> and in my class I have myclass::operator<(myclass &other) defined. I use the std::list.sort() function, but it does not change anything in that list. Maybe it just sorts the pointers? How can I sort the actual items in that list? 回答1: You are sorting the pointer values, not the myclass values. You have to write your own predicate to compare pointers by dereference: template <typename T> bool PComp(const T * const & a, const T * const & b) { return *a < *b; } std:

Sort a std::list<myclass*> with myclass::operator<(myclass &other)

混江龙づ霸主 提交于 2019-12-19 06:18:01
问题 I have a std::list<myclass*> and in my class I have myclass::operator<(myclass &other) defined. I use the std::list.sort() function, but it does not change anything in that list. Maybe it just sorts the pointers? How can I sort the actual items in that list? 回答1: You are sorting the pointer values, not the myclass values. You have to write your own predicate to compare pointers by dereference: template <typename T> bool PComp(const T * const & a, const T * const & b) { return *a < *b; } std:

Is std::regex thread safe?

谁说胖子不能爱 提交于 2019-12-19 05:46:05
问题 Related to Is a static boost::wregex instance thread-safe? but for the standarized version. Can I call regex_search from several threads with the same regex object? 回答1: Claiming that std::regex is thread-safe in every respect is a pretty bold statement. The C++11 standard does not make such guarantees for the regex library. However, looking at the prototype of std::regex_search shows that it it takes the basic_regex object as a const argument. This means that it is protected by the standard

Is it defined to provide an empty range to C++ standard algorithms?

孤街浪徒 提交于 2019-12-19 04:10:33
问题 Following on from my previous question, can we prove that the standard allows us to pass an empty range to a standard algorithm? Paragraph 24.1/7 defines an "empty range" as the range [i,i) (where i is valid), and i would appear to be "reachable" from itself, but I'm not sure that this qualifies as a proof. In particular, we run into trouble when looking at the sorting functions. For example, std::sort : Complexity: O(N log(N)) (where N == last - first ) comparisons Since log(0) is generally

Is it defined to provide an empty range to C++ standard algorithms?

北慕城南 提交于 2019-12-19 04:10:07
问题 Following on from my previous question, can we prove that the standard allows us to pass an empty range to a standard algorithm? Paragraph 24.1/7 defines an "empty range" as the range [i,i) (where i is valid), and i would appear to be "reachable" from itself, but I'm not sure that this qualifies as a proof. In particular, we run into trouble when looking at the sorting functions. For example, std::sort : Complexity: O(N log(N)) (where N == last - first ) comparisons Since log(0) is generally

Could a smart compiler do all the things std::move does without it being part of the language?

天大地大妈咪最大 提交于 2019-12-19 04:02:15
问题 This is a bit theoretical question, but although I have some basic understanding of the std::move Im still not certain if it provides some additional functionality to the language that theoretically couldnt be achieved with supersmart compilers. I know that code like : { std::string s1="STL"; std::string s2(std::move(s1)); std::cout << s1 <<std::endl; } is a new semantic behavior not just performance sugar. :D But tbh I guess nobody will use var x after doing std::move(x). Also for movable

C++ compile error constructing object with rvalue std::string

情到浓时终转凉″ 提交于 2019-12-18 18:54:29
问题 I'm faced with a compile error that I don't even know how to describe! It completely baffles me. The situation : Code tries to create an object on the stack with an rvalue std::string that is initialized with a char*. The code : #include <iostream> #include <string> class Foo { public: Foo(double d) : mD(d) { } Foo(const std::string& str) { try { mD = std::stod(str); } catch (...) { throw; } } Foo(const Foo& other) : mD(other.mD) { } virtual ~Foo() {} protected: double mD; }; class Bar {

Efficiently reading two comma-separated floats in brackets from a string without being affected by the global locale

南楼画角 提交于 2019-12-18 15:54:45
问题 I am a developer of a library and our old code uses sscanf() and sprintf() to read/write a variety of internal types from/to strings. We have had issues with users who used our library and had a locale that was different from the one we based our XML files on ("C" locale). In our case this resulted in incorrect values parsed from those XML files and those submitted as strings in run-time. The locale may be changed by a user directly but can also be changed without the knowledge of the user.