stl

C++ STL container ::clear ::swap

﹥>﹥吖頭↗ 提交于 2019-12-22 11:28:00
问题 What's the fastest way to "clear" a large STL container? In my application, I need to deal with large size std::map , e.g., 10000 elements. I have tested the following 3 methods to clear a std::map . Create a new container every time I need it. Calling map::clear() method. Calling map::swap() method. It seems that ::swap() gives the best result. Can anyone explain why this is the case, please? Is it safe to say that using map::swap() method is the proper way to "clear" a std::map? Is it the

Why the destructors are called twice for this functor?

a 夏天 提交于 2019-12-22 11:23:39
问题 When I run the following program, the destructor is called twice and I'm trying to understand why? #include <iostream> #include <vector> #include <algorithm> class sample { public: sample() { std::cout << "Constructor " << std::endl; } ~sample() { std::cout << "Destructor" << std::endl; } void operator()(int i) { std::cout << i << " , " << std::endl; } }; int main() { std::vector<int> iNumbers; for ( int i = 0 ; i < 5 ; ++i) iNumbers.push_back(i); std::for_each(iNumbers.begin() , iNumbers.end

Heap corruption when deleting a string

只愿长相守 提交于 2019-12-22 11:22:49
问题 Here is my code: std::string readString() { int strLen = Read<int>(); char* rawString = new char[strLen]; Read(rawString, strLen); rawString[strLen] = '\0'; std::string retVal(rawString); delete [] rawString; return retVal; } The first line reads the length of the string. The second line creates a new char array (c-string) with the string length The third line reads the string (its reading it from a file) The 4th line adds a NULL to the end. The 5th line creates an std::string out of the c

Heap corruption when deleting a string

左心房为你撑大大i 提交于 2019-12-22 11:20:27
问题 Here is my code: std::string readString() { int strLen = Read<int>(); char* rawString = new char[strLen]; Read(rawString, strLen); rawString[strLen] = '\0'; std::string retVal(rawString); delete [] rawString; return retVal; } The first line reads the length of the string. The second line creates a new char array (c-string) with the string length The third line reads the string (its reading it from a file) The 4th line adds a NULL to the end. The 5th line creates an std::string out of the c

Which sorting algorithm is used by Microsoft's STL::list::sort()?

为君一笑 提交于 2019-12-22 10:57:10
问题 Note: I accidentally posted this question without specifying which STL implementation I was using, and I felt it can't really be updated since it would render most of its answers obsolete. So, the correct question goes - which sorting algorithm is used in the below code, assuming I'm using the STL library of Microsoft Visual C++?: list<int> mylist; // ..insert a million values mylist.sort(); 回答1: Just so you don't have to rely on second hand information, the the sort code is right in the list

STL __merge_without_buffer algorithm?

≯℡__Kan透↙ 提交于 2019-12-22 10:39:13
问题 Where can I get a decent high-level description of the algorithm used in __merge_without_buffer() in the C++ STL? I'm trying to reimplement this code in the D programming language, with some enhancements. I can't seem to grok what it's doing at the algorithmic level from just reading the STL source code because there are too many low-level details obscuring it. Also, I don't want to just blindly translate the code because then, if it didn't work I wouldn't know why, and I wouldn't be able to

Parallel reads from STL containers

亡梦爱人 提交于 2019-12-22 10:37:36
问题 It is safe to read a STL container from multiple parallel threads. However, the performance is terrible. Why? I create a small object that stores some data in a multiset. This makes the constructors fairly expensive ( about 5 usecs on my machine. ) I store hundreds of thousands of the small objects in a large multiset. Processing these objects is an independent business, so I split the work between threads running on a multi-core machine. Each thread reads the objects it needs from the large

Which is preferable for each, in or for_each?

点点圈 提交于 2019-12-22 10:29:48
问题 When using Visual Studio, I can write a container traversal in at least the following three ways. Which way is preferable? Assuming: vector<CString> strings1; Method 1 (using the for_each algorithm with a lambda: for_each(strings1.begin(), strings1.end(), [](CString s){ _tprintf(_T("%s"), s); } Method 2 (using for each, in , microsoft specific): for each(auto s in strings1) { _tprintf(_T("%s"), s); } Method 3 (treat the vector with array syntax): for (int i=0; i<v.size(); ++i) { _tprintf(_T("

multiple keys for a map in C++

情到浓时终转凉″ 提交于 2019-12-22 10:24:45
问题 I have a table where the entries are something like this Row - Column1 - Column2 - Column3 Column4 1 0X0A 1 2 A 2 0X0B 2 2 B 3 0x0C 3 2 C Now i want to use map in such that i can use column 1 or Column 2 as the key to get the row. What kind of map i should use to achieve this? (Note- Table is just for explanation and not the exact requirement) I thought of using multimap, but that is not going to solve the prob 回答1: try multi-index containers from boost. 回答2: Define a class similar to pair

Why does the `equal` works for const char* in C++?

我与影子孤独终老i 提交于 2019-12-22 10:01:09
问题 The codes are like this, it outputs 1 : int main(int argc, char *argv[]) { vector <const char*> vec = {"nima","123"}; vector <const char*> vec2 = {"nima","123"}; auto result = equal(vec.cbegin(), vec.cend(), vec2.cbegin()); cout << result << endl; return 0; } I knew that I can test whether two c-style string is equal only by using strcmp (because char* is not an object as I understood). But here equal is a function from <algorithm> . Does it overload the == operator so that it can test the