std

how are map iterators invalidated when erasing elements? [duplicate]

扶醉桌前 提交于 2019-12-06 01:11:26
问题 This question already has answers here : Iterator invalidation rules (5 answers) Closed 10 months ago . when and how are iterators invalidated in a map when using the erase method ? for example : std :: map < int , int > aMap ; aMap [ 33 ] = 1 ; aMap [ 42 ] = 10000 ; aMap [ 69 ] = 100 ; aMap [ 666 ] = -1 ; std :: map < int , int > :: iterator itEnd = aMap.lower_bound ( 50 ) ; for ( std :: map < int , int > :: iterator it = aMap.begin ( ) ; it != itEnd ; // no-op ) { aMap.erase ( it ++ ) ; }

Limiting fps with std::chrono

心不动则不痛 提交于 2019-12-06 00:43:49
std::chrono::system_clock::time_point m_BeginFrame = std::chrono::system_clock::now(); std::chrono::system_clock::time_point m_EndFrame = std::chrono::system_clock::now(); std::chrono::nanoseconds m_WorkTime = std::chrono::nanoseconds::zero(); std::chrono::nanoseconds m_WaitTime = std::chrono::nanoseconds::zero(); auto invFpsLimit = std::chrono::nanoseconds(1e9 / fpsLimit()); // main loop while (!glfwWindowShouldClose(m_pScreen->glfwWindow())) { m_WaitTime = m_BeginFrame - m_EndFrame; m_EndFrame = std::chrono::system_clock::now(); m_WorkTime = m_EndFrame - m_BeginFrame; // if need sleep if (m

std::string as a key in std::map using a compare operator

元气小坏坏 提交于 2019-12-06 00:23:42
I'm trying to use a std::string as a key in a std::map however, i'm unable to find() correctly. My code is somewhat complicated and large so this is a small program that demonstrates the problem I'm having. If someone could tell me why this doesn't work, i'd be very grateful. Thanks. #include <stdio.h> #include <string> #include <map> struct comparer { public: bool operator()(const std::string x, const std::string y) { return x.compare(y)==0; } }; int main(int argc, char *argv[]) { std::map<std::string, int, comparer> numbers; numbers.insert(std::pair<std::string,int>("One",1)); numbers.insert

C++11 std::forward a pointer

耗尽温柔 提交于 2019-12-05 22:43:15
问题 I have a Signal class in my application that provides classes with an option to expose events (same as in .NET). The class works and all is well. Yesterday I saw this SO question (and its answer) and was familiarized with std::forward . I decided to try to use it in my code so I changed every std::function<void(Args...)> to std::function<void(Args&&...)> and in the raise function (the operator() ) I used the same logic I saw in the above link so now the function takes Args&&...args and the

Convert vector<std::string> to vector<double>

こ雲淡風輕ζ 提交于 2019-12-05 22:30:55
问题 I have a string vector like {"1.2","3.4","0.5","200.7"} . I would like to convert each element into double and store it in a vector<double> . Like so {1.2,3.4,0.5,200.7} What would be the best way to do this? I know of the std::stod(string, size); But I am hoping for a better way to do this. I was looking for something like: vector<double> doubleVector = convertStringVectortoDoubleVector(myStringVector); There doesn't seem to be anything like that; so what is the next best thing? EDIT: Here's

c++ vector source code

╄→гoц情女王★ 提交于 2019-12-05 21:22:23
问题 I am trying to get the vector source code to see how the standard std or stl vector is implemented. This is for learning purpose. Now the question is where can i find the source code. Even source code of other C++ Container also helpful. 回答1: There is no 'standard' vector - the standard defines behaviour and interface (and some implementation details, such as contiguous storage) but the code is a matter for compiler writers to determine. Your compiler should have its own <vector> header file,

Using qsort() with class pointers

青春壹個敷衍的年華 提交于 2019-12-05 18:07:12
I am using the in-built function qsort() to sort a vector of class item pointers. class item { int value; vector<char> c; ... ... }; //Declaration of vector vector<item*> items; //Function Call qsort(&items, items.size(), sizeof(item*), value_sort); int value_sort(const void* a, const void* b) { item* pa = *(item**) a; item* pb = *(item**) b; if (pb->value < pa->value) return 1; else if (pa->value < pb->value) return -1; return 0; } In the debugger mode, pointers neither pa nor pb point to a valid location. Set of all data members of the class items pointed by either pa or pb contain garbage

Convert values to values inside a range in c++ , optimized using boost or std

為{幸葍}努か 提交于 2019-12-05 17:36:32
I want to validate all the elemnent of an array. If an element is under a value, swap by a min value and if it is above a value, swap by a max value. But I don´t know how I can do it optimized. For do it I go above all elements, element by element but it is not optimized, and it spend a lot of cpu time in very large arrays. This is an example of my code: #include <iostream> #include <math.h> const int MAX = 10; int main () { float minVal = 2.0; float maxVal = 11.0; float vElem[] = {-111111.0/0.0, 10.0, 90.0, 8.0, -7.0, -0.6, 5.0, 4.0, 33.0, 222222222.0/0}; for(int i=0; i<MAX; i++){ if(isinf

Is it possible to bind() *this to class member function to make a callback to C API

自古美人都是妖i 提交于 2019-12-05 17:31:26
Is there a way to use boost or std bind() so I could use a result as a callback in C API? Here's sample code I use: #include <boost/function.hpp> #include <boost/bind/bind.hpp> typedef void (*CallbackType)(); void CStyleFunction(CallbackType functionPointer) { functionPointer(); } class Class_w_callback { public: Class_w_callback() { //This would not work CStyleFunction(boost::bind(&Class_w_callback::Callback, this)); } void Callback(){std::cout<<"I got here!\n";}; }; Thanks! No, there is no way to do that. The problem is that a C function pointer is fundamentally nothing more than an

Removing everything after character (and also character)

旧时模样 提交于 2019-12-05 17:13:35
问题 I have a string like this: std::string string1 = "xjdfhfakdjs%54k34k.-jk34"; I need to get only ""xjdfhfakdjs", but the string is dynamic, not hardcoded so I don't know what is it, the length etc. so I wanted to remove everything after %, and also the % char. How could I do this? 回答1: std::string mystr = string1.substr(0, string1.find("%", 0)); I believe that will work. 回答2: std::string the_prefix_you_want = string1.substr(0, string1.find("%")); See: http://www.cplusplus.com/reference/string