stl

Custom arguments to std::set comparison function

本小妞迷上赌 提交于 2021-02-08 12:01:22
问题 I know how to pass a regular comparison class or function to set::set<>. I am writing some test code and I want to emulate some C libraries using STL's std::set and I want to be able to pass a C callback to the comparison object so a different comparison takes place. I have the following theoretical code: struct MyClass { int a; }; typedef bool (*user_callback_t)(void *, void *); class MyComparison { private: user_callback_t cb = nullptr; public: MyComparison(user_callback_t cb): cb(cb) { }

thread safety in std::map

南楼画角 提交于 2021-02-08 10:18:29
问题 Is it safe to use std map without a lock in multi-thread environment? Where It is guaranteed that two threads never be manipulating the same entry in the map. There is already a question on this but I am particularly interested in the case where multiple threads are accessing different entries in the map. particularly unordered maps. 回答1: It is safe as long as none of the threads are modifying the map. It is also safe if threads are modifying different elements of the map (provided the

Use std::vector<double> to access data managed by std::unique_ptr<double[2]>

谁说我不能喝 提交于 2021-02-08 05:17:36
问题 I have a complex class, that holds a big block of double[2] -type data managed by a smart pointer like: std::unique_ptr<double[2]> m_data; I cannot change the type of the data structure. I am using a library that gives me a function with the following signature: bool func_in_lib(std::vector<double>& data, double& res) . I cannot change the signature of this function. I want to pass the data managed by the unique_ptr to the function expecting a vector<double>& without breaking the connection

read from a file while another process is writing to it using std::fstream [duplicate]

余生颓废 提交于 2021-02-08 03:48:00
问题 This question already has answers here : How to read a growing text file in C++? (4 answers) Closed 6 years ago . I need to read from a file line by line, it's done by std::getline. The problem another process is appending data to it all the time, then I need to read the new lines. For example, the file contains 10 lines at first, my program read the 10 line, then my program should wait. A while later another process append 5 lines to the file, then my program read the 5 lines. I tried this

Adding struct containing not copyable/moveable object to std::map

混江龙づ霸主 提交于 2021-02-07 20:13:52
问题 I have this MCVE: #include <atomic> #include <map> #include <string> struct foo { int intValue; std::atomic_bool bar; foo( int intValue ) : intValue( intValue ) {}; }; std::map<std::string, foo> myMap; int main() { myMap.emplace( "0", 1234 ); } It does not compile because std::atomic is neither copyable nor movable. My question: How can I add a class containing a not copyable/moveable object to a std::map container? 回答1: What about myMap.emplace(std::piecewise_construct, std::forward_as_tuple

std::map find not working in C++ [duplicate]

青春壹個敷衍的年華 提交于 2021-02-07 19:26:15
问题 This question already has an answer here : Error trying to find const char* key from std::map (1 answer) Closed 4 years ago . I have created a hash map and an iterator using the below lines: std::map<const char*,vaLueClass *> myCache; std::map<const char*,vaLueClass *>::iterator myCacheIterator; Then I insert into this map using the below line: myCache[anotherObject->getStringKey()] = new vaLueClass(anotherObj1->getIntAttr(), anotherObj1-->getIntAttr()); Then whenever I tried to search if an

Why does std::stof not throw when passed an argument it cannot convert?

六月ゝ 毕业季﹏ 提交于 2021-02-07 18:22:33
问题 I'm working on a project where I want to accept an input of the form {float}{single-letter-identifier} , for example 15.6E , or 10W . To do this, I thought that I could take the input string, strip off the last letter and then check to see if a conversion could be performed to float, using std::stof . This would be nested in a try-catch block, and allow me to notify the user of invalid input. The open-standard of the STL here (page 653) states that std::stof throws: invalid_argument if wcstod

Why does std::stof not throw when passed an argument it cannot convert?

眉间皱痕 提交于 2021-02-07 18:21:05
问题 I'm working on a project where I want to accept an input of the form {float}{single-letter-identifier} , for example 15.6E , or 10W . To do this, I thought that I could take the input string, strip off the last letter and then check to see if a conversion could be performed to float, using std::stof . This would be nested in a try-catch block, and allow me to notify the user of invalid input. The open-standard of the STL here (page 653) states that std::stof throws: invalid_argument if wcstod

Why can't I delete last element of vector

家住魔仙堡 提交于 2021-02-07 14:26:51
问题 I have stl vector consisting of several elements. I need to iterate through this vector and delete elements which meets some criteria. So I wrote this code for (int j = imageDataVector.size()-1; j >= 0; j--) { if(imageDataVector[i] < threshold) imageDataVector.erase(imageDataVector.end() - j); } This code works fine for almost all cases, however if all elements of vector meets the criteria I get an error: vector erase iterator outside the range This error occurs if I have only one element

Why can't I delete last element of vector

和自甴很熟 提交于 2021-02-07 14:24:27
问题 I have stl vector consisting of several elements. I need to iterate through this vector and delete elements which meets some criteria. So I wrote this code for (int j = imageDataVector.size()-1; j >= 0; j--) { if(imageDataVector[i] < threshold) imageDataVector.erase(imageDataVector.end() - j); } This code works fine for almost all cases, however if all elements of vector meets the criteria I get an error: vector erase iterator outside the range This error occurs if I have only one element