stdvector

c++ std::vector std::sort infinite loop

痴心易碎 提交于 2020-01-02 07:06:18
问题 I ran across an issue whenever I was trying to sort a vector of objects that was resulting in an infinite loop. I am using a custom compare function that I passed in to the sort function. I was able to fix the issue by returning false when two objects were equal instead of true but I don't fully understand the solution. I think it's because my compare function was violating this rule as outlined on cplusplus.com: Comparison function object that, taking two values of the same type than those

c++ std::vector std::sort infinite loop

微笑、不失礼 提交于 2020-01-02 07:06:14
问题 I ran across an issue whenever I was trying to sort a vector of objects that was resulting in an infinite loop. I am using a custom compare function that I passed in to the sort function. I was able to fix the issue by returning false when two objects were equal instead of true but I don't fully understand the solution. I think it's because my compare function was violating this rule as outlined on cplusplus.com: Comparison function object that, taking two values of the same type than those

Difference between std::vector and std::array initializer lists

为君一笑 提交于 2020-01-02 02:43:09
问题 This C++11 code works fine for me: #include <iostream> #include <vector> #include <array> using namespace std; struct str { int first, last; }; vector<str> fields { {1,2}, {3,4}, {5,6} }; int main() { for (str s : fields) cout << s.first << " " << s.last << endl; } It prints the six expected values. But if I change vector<str> to array<str,3> , gcc gives me this error: "too many initializers for ‘std::array’". If I change the initialization of fields thus: array<str,3> fields { str{1,2}, str

Copy from vector<pointer*> to vector<pointer*> in C++

丶灬走出姿态 提交于 2020-01-01 11:11:09
问题 I create a vector A and want to copy to a vector B in another class by using below method, is it a correct way? The vector A may be destroyed! I searched in google, but not found the good solution and meaningful explanation. Thanks everyone void StateInit(vector<CButton*> listBtn) { _m_pListBtn = listBtn; }; 回答1: Yes and no, you are passing the vector by value: void StateInit(vector<CButton*> listBtn) { _m_pListBtn = listBtn; }; Wich means that listBtn is a copy of vector A (asuming we are

Copy from vector<pointer*> to vector<pointer*> in C++

放肆的年华 提交于 2020-01-01 11:11:05
问题 I create a vector A and want to copy to a vector B in another class by using below method, is it a correct way? The vector A may be destroyed! I searched in google, but not found the good solution and meaningful explanation. Thanks everyone void StateInit(vector<CButton*> listBtn) { _m_pListBtn = listBtn; }; 回答1: Yes and no, you are passing the vector by value: void StateInit(vector<CButton*> listBtn) { _m_pListBtn = listBtn; }; Wich means that listBtn is a copy of vector A (asuming we are

why there is no find for vector in C++

空扰寡人 提交于 2020-01-01 09:18:21
问题 what's the alternative? Should I write by myself? 回答1: There is the std::find() algorithm, which performs a linear search over an iterator range, e.g., std::vector<int> v; // Finds the first element in the vector that has the value 42: // If there is no such value, it == v.end() std::vector<int>::const_iterator it = std::find(v.begin(), v.end(), 42); If your vector is sorted, you can use std::binary_search() to test whether a value is present in the vector, and std::equal_range() to get begin

How can I get the depth of a multidimensional std::vector at compile time?

こ雲淡風輕ζ 提交于 2020-01-01 01:11:11
问题 I have a function that takes a multidimensional std::vector and requires the depth (or the number of dimensions) to be passed in as a template parameter. Instead of hardcoding this value I would like to write a constexpr function that will take the std::vector and return the depth as an unsigned integer value. For example: std::vector<std::vector<std::vector<int>>> v = { { { 0, 1}, { 2, 3 } }, { { 4, 5}, { 6, 7 } }, }; // Returns 3 size_t depth = GetDepth(v); This needs to be done at compile

Concatenating two std::vector — which method is more efficient and how/why?

自作多情 提交于 2019-12-30 10:43:27
问题 Consider the following scenario: std::vector<int> A; std::vector<int> B; std::vector<int> AB; I want AB to have contents of A and then the contents of B in the same order. Approach 1: AB.reserve( A.size() + B.size() ); // preallocate memory AB.insert( AB.end(), A.begin(), A.end() ); AB.insert( AB.end(), B.begin(), B.end() ); Approach 2: std::vector<int> AB ( A.begin(), A.end() ); // calling constructor AB.insert ( AB.end(), B.begin(), B.end() ); Which one of the above methods is more

Concatenating two std::vector — which method is more efficient and how/why?

二次信任 提交于 2019-12-30 10:43:15
问题 Consider the following scenario: std::vector<int> A; std::vector<int> B; std::vector<int> AB; I want AB to have contents of A and then the contents of B in the same order. Approach 1: AB.reserve( A.size() + B.size() ); // preallocate memory AB.insert( AB.end(), A.begin(), A.end() ); AB.insert( AB.end(), B.begin(), B.end() ); Approach 2: std::vector<int> AB ( A.begin(), A.end() ); // calling constructor AB.insert ( AB.end(), B.begin(), B.end() ); Which one of the above methods is more

Why is std::vector contiguous?

末鹿安然 提交于 2019-12-30 05:37:05
问题 Besides the fact that the standard defines it to be contiguous, why is std::vector contiguous? If it runs out of space, it needs to reallocate a new block and copy the old block to the new one before continuing. What if it wasn't contiguous? When the storage fills up, it would just allocate a new block and keep the old block. When accessing through an iterator, it would do simple >, < checks to see which block the index is in and return it. This way it doesnt need to copy the array every time