stdvector

Overhead to using std::vector?

不打扰是莪最后的温柔 提交于 2019-12-14 00:16:37
问题 I know that manual dynamic memory allocation is a bad idea in general, but is it sometimes a better solution than using, say, std::vector ? To give a crude example, if I had to store an array of n integers, where n <= 16, say. I could implement it using int* data = new int[n]; //assuming n is set beforehand or using a vector: std::vector<int> data; Is it absolutely always a better idea to use a std::vector or could there be practical situations where manually allocating the dynamic memory

Vector iterator not dereferencable error when trying to push_back()

时光怂恿深爱的人放手 提交于 2019-12-13 22:32:31
问题 I have a problem when my application crashes with this error message from VS: "Debug assertion failed! (...) Expression: vector iterator not dereferencable". The thing is, it happens during using vector's push_back. Here is the code. It is my BigInt library, that I decided to implement as an exercise. The bug is hidden in my TestBigInt class, that I created to (surprisingly) test BigInt. The code is admittedly quite long, but I narrowed the bug to a single piece of that. This is the input I

Storing templated objects in a vector (Storing Class<int>, Class<double> in a single vector)

断了今生、忘了曾经 提交于 2019-12-13 14:33:11
问题 There is a templated class, let it be template<typename T> class A { std::vector<T> data; }; The problem I am facing here is, users can create several types of this class, but I need to track them, best case is I have a reference of these objects in another vector, but that would not work since all types are different. Can you recommend a good design pattern which can encapsulate this. I can store pointers and then typecast it, but its not elegant. I can change the architecture as well, if

strange behaviour of std::vector::resize() with gcc 4.7.0

前提是你 提交于 2019-12-12 17:30:23
问题 I'm still confused about the behaviour of std::vector::resize() . Consider the following code (see also type requirements for std::vector<type>) struct A { A() : X(0) { std::cerr<<" A::A(); this="<<this<<'\n'; } A(A const&) { assert(0); } // is required but doesn't fire in vector::resize int X; }; int main() { std::vector<A> a; a.resize(4); // would not compile without A::A(A const&) or A::A(A&&) } Without A::A(A const&) or A::A(A&&) , the line with a.resize(4); doesn't compile. However, that

Is it safe to use `for(auto& e : cont)`? What is wrong with vector<bool>?

旧城冷巷雨未停 提交于 2019-12-12 14:33:34
问题 I have found that for (auto& e : cont) is sometimes used instead of ordinary for (auto e : cont) (where cont is some container, e.g. std::vector ). I have found two reasons for it so far: Taking a reference should avoid copying the object (faster execution) Making a copy may be forbidden for some classes (e.g. std::thread ) After few tests I can see: for (auto& e : cont) works with any std::vector<T> except std::vector<bool> for (cont::reference e : cont) works with any std::vector<T>

Is the following std::vector code valid?

孤街醉人 提交于 2019-12-12 12:14:49
问题 std::vector<Foo> vec; Foo foo(...); assert(vec.size() == 0); vec.reserve(100); // I've reserved 100 elems vec[50] = foo; // but I haven't initialized any of them // so am I assigning into uninitialized memory? Is the above code safe? 回答1: It's not valid. The vector has no elements, so you cannot access any element of them. You just reserved space for 100 elements (which means that it's guaranteed that no reallocation happens until over 100 elements have been inserted). The fact is that you

Can I reinterpret std::vector<char> as a std::vector<unsigned char> without copying?

痞子三分冷 提交于 2019-12-12 10:58:43
问题 I have a reference to std::vector<char> that I want to use as a parameter to a function which accepts std::vector<unsigned char> . Can I do this without copying? I have following function and it works; however I am not sure if a copy actually takes place - could someone help me understanding this? Is it possible to use std::move to avoid copy or is it already not being copied? static void showDataBlock(bool usefold, bool usecolor, std::vector<char> &chunkdata) { char* buf = chunkdata.data();

C++ std::vector<>::iterator is not a pointer, why?

旧城冷巷雨未停 提交于 2019-12-12 09:29:01
问题 Just a little introduction, with simple words. In C++, iterators are "things" on which you can write at least the dereference operator *it , the increment operator ++it , and for more advanced bidirectional iterators, the decrement --it , and last but not least, for random access iterators we need operator index it[] and possibly addition and subtraction. Such "things" in C++ are objects of types with the according operator overloads, or plain and simple pointers. std::vector<> is a container

Have many vectors sorted together [duplicate]

ε祈祈猫儿з 提交于 2019-12-12 09:26:43
问题 This question already has answers here : Sorting zipped (locked) containers in C++ using boost or the STL (5 answers) Closed 2 years ago . I have three vectors of the same size (~ 1 million items): std::vector<wstring> name; std::vector<int> x; std::vector<int> y; which can be seen as three "columns". How to sort A->Z the vector name : std::sort(name.begin(), name.end()) but having the vectors x and y sorted accordingly? Example: name x y name x y BCD 7 9 ABC 4 3 ZYX 1 4 => BCD 7 9 ABC 4 3

Get the index of a std::vector element given its address

别来无恙 提交于 2019-12-12 08:27:42
问题 Let's say I have a std::vector and I get by some means the address of the n-th element. Is there a simple way (faster than iterating through the vector) to get the index at which the element appears, given the base address of my std::vector? Let's assume I'm sure the element is in the vector. 回答1: Since you know the element is within the vector, and vector guarantees that its storage is contiguous, you could do: index = element_pointer - vector.data(); or index = element_pointer - &vector[0];