stl

How to define a multiset using a function pointer?

强颜欢笑 提交于 2021-02-07 08:51:22
问题 I'm stuck when doing an exercise from C++ Primer 5th Edition ,which goes like Exercise 11.11: Redefine bookstore without using decltype. Below is the relevant codes in this book: multiset<Sales_data, decltype(compareIsbn)*> bookstore(compareIsbn); The code for class Sales_data is a little bit verbose to post here,so I wrote a simpler one and defined the multiset in the same style, as shown below. It compiled without any error. class A { int lenth; public: int getLenth() const {return lenth;}

Will std::vectors inside another vector reallocate when the first vector reallocates?

Deadly 提交于 2021-02-07 04:52:20
问题 I have a vector std::vector<std::vector<ContactPairs>> m_contactPairs; If I call m_contactPairs.push_back() or any other function that will resize the outermost vector, will the elements inside that vector have to reallocate (the inner elements in this case being std::vector<ContactPairs> ), or will the inner vectors just do a shallow copy and keep pointing at the same memory they already own? I'm using Visual Studio 2010, which is prior C++11, but has some of the functionality as extensions

Why might std::vector be faster than a raw dynamically allocated array?

不羁的心 提交于 2021-02-06 13:54:57
问题 The result of a discussion with a colleague I ended up writing benchmarks to test std::vector vs raw dynamically allocated arrays, and ended up with a surprise. My tests are as follows: #include "testconsts.h" // defines NUM_INTS across all tests #include <vector> int main() { const int numInts = NUM_INTS; std::vector<int> intVector( numInts ); int * const intArray = new int[ numInts ]; ++intVector[0]; // force access to affect optimization ++intArray[0]; // force access to affect

Why might std::vector be faster than a raw dynamically allocated array?

狂风中的少年 提交于 2021-02-06 13:52:35
问题 The result of a discussion with a colleague I ended up writing benchmarks to test std::vector vs raw dynamically allocated arrays, and ended up with a surprise. My tests are as follows: #include "testconsts.h" // defines NUM_INTS across all tests #include <vector> int main() { const int numInts = NUM_INTS; std::vector<int> intVector( numInts ); int * const intArray = new int[ numInts ]; ++intVector[0]; // force access to affect optimization ++intArray[0]; // force access to affect

Thread-safety of writing a std::vector vs plain array

本小妞迷上赌 提交于 2021-02-06 09:49:27
问题 I've read on Stackoverflow that none of the STL containers are thread-safe for writing . But what does that mean in practice? Does it mean I should store writable data in plain arrays? I expect concurrent calls to std::vector::push_back(element) could lead to inconsistent data structures becaue it might entail resizing the vector. But what about a case like this, where resizing is not involved: using an array: int data[n]; // initialize values here... #pragma omp parallel for for (int i = 0;

Concatenate two huge files in C++

与世无争的帅哥 提交于 2021-02-06 08:59:25
问题 I have two std::ofstream text files of a hundred plus megs each and I want to concatenate them. Using fstreams to store the data to create a single file usually ends up with an out of memory error because the size is too big. Is there any way of merging them faster than O(n)? File 1 (160MB): 0 1 3 5 7 9 11 13 ... ... 9187653 9187655 9187657 9187659 File 2 (120MB): a b c d e f g h i j a b c d e f g h j i a b c d e f g i h j a b c d e f g i j h ... ... j i h g f e d c b a Merged (380MB): 0 1 3

Is there a nice way to assign std::minmax(a, b) to std::tie(a, b)?

天涯浪子 提交于 2021-02-06 06:29:11
问题 std::tie(a, b) = std::minmax(a, b); I think this is intuitive code. Clean and understandable. Too bad it doesn't work as intended, as std::minmax templates for const& . If therefore the values are swapped inside the std::pair<const&, const&> than one assignement will overwrite the other value: auto[a, b] = std::make_pair(7, 5); std::tie(a, b) = std::minmax(a, b); std::cout << "a: " << a << ", b: " << b << '\n'; a: 5, b: 5 The expected output here is a: 5, b: 7 . I think this is important as

Why doesn't C++ require a “new” statement to initialize std::vector?

筅森魡賤 提交于 2021-02-05 20:10:22
问题 /* bar.h */ class bar{ /* standard stuff omitted */ std::vector<my_obj*> foo; }; /* bar.cpp */ bar::bar(){ // foo = new std::vector<my_obj*>(); <-- why don't I need this line?? foo.push_back(new my_obj()); } Why does this code work even though we didn't assign foo a new instance of std::vector ? 回答1: Because C++ is not C#/Java. std::vector<my_obj*> foo; This is a definition of an object , not a reference as in C#/Java. An object is a living instance of a type. new std::vector<my_obj*>() This

Why doesn't C++ require a “new” statement to initialize std::vector?

倾然丶 夕夏残阳落幕 提交于 2021-02-05 20:05:50
问题 /* bar.h */ class bar{ /* standard stuff omitted */ std::vector<my_obj*> foo; }; /* bar.cpp */ bar::bar(){ // foo = new std::vector<my_obj*>(); <-- why don't I need this line?? foo.push_back(new my_obj()); } Why does this code work even though we didn't assign foo a new instance of std::vector ? 回答1: Because C++ is not C#/Java. std::vector<my_obj*> foo; This is a definition of an object , not a reference as in C#/Java. An object is a living instance of a type. new std::vector<my_obj*>() This

Why doesn't C++ require a “new” statement to initialize std::vector?

烈酒焚心 提交于 2021-02-05 19:55:44
问题 /* bar.h */ class bar{ /* standard stuff omitted */ std::vector<my_obj*> foo; }; /* bar.cpp */ bar::bar(){ // foo = new std::vector<my_obj*>(); <-- why don't I need this line?? foo.push_back(new my_obj()); } Why does this code work even though we didn't assign foo a new instance of std::vector ? 回答1: Because C++ is not C#/Java. std::vector<my_obj*> foo; This is a definition of an object , not a reference as in C#/Java. An object is a living instance of a type. new std::vector<my_obj*>() This