STL vector: resize() and assign()
assign sets the size to n and all element values to 0.0, whereas resize sets the size to n and only new element values to 0.0. If v is empty beforehand, they’re the same, but assign is probably clearer.
assign sets the size to n and all element values to 0.0, whereas resize sets the size to n and only new element values to 0.0. If v is empty beforehand, they’re the same, but assign is probably clearer.
In practice, at the CPU level, there are instructions which can atomically update an int, and a good compiler will use these for std::atomic<int>. In contrast, there are are no instructions which can atomically update a vector of ints (for any architecture I am aware of), so there has got to be a mutex of … Read more
Solution 1 Flattern image in Sketch and use this site to convert SVG to xml for Android Solution 2 I use nonZero instead of evenOdd and open it in Sketch to reverse Order after reverse it will change pathData and remove android:fillType and everything work fine on Android 21+. Solution 3 PNG TLDR After some … Read more
There’s no built-in Deque container, but there are several implementations available. Here’s a good one from Stephen Cleary. This provides O(1) operations to index and also to insert at the beginning and append at the end. The C# equivalent to Vector is List<T>. Indexed access is O(1), but insertion or removal is O(N) (other than … Read more
Do the arguments in the quotation hold? Measure and you will know. Are you constrained in memory? Can you figure out the correct size up front? It will be more efficient to reserve than it will be to shrink after the fact. In general I am inclined to agree on the premise that most uses … Read more
You should prefer std::vector over std::string. In common cases both solutions can be almost equivalent, but std::strings are designed specifically for strings and string manipulation and that is not your intended use.
EDIT: as of C++17, the standard library now includes the class template std::variant, which is quite similar to pre-existing solutions in boost. variant is a type-safe alternative to unions that allows multiple types to be joined using an “or” relationship, e.g., an std::variant<type1, type2, typ3> contains either a type1 OR a type2 OR a type3. … Read more
You don’t want to push_front on a vector ever. Adding an element to the front means moving every single other element in the vector one element back: O(n) copying. Terrible performance.
The capacity of a vector cannot be controlled by the constructors – there is no applicable overload. The C++ standard doesn’t give any guarantee about the capacity of a default-constructed vector vector<Foo> bar;. However all well-known implementations use 0 as the default capacity. This is something you can rely on, as allocating memory at that … Read more