Posts

Showing posts with the label Vector

Build HashSet From A Vector In Rust

Answer : Because the operation does not need to consume the vector¹, I think it should not consume it. That only leads to extra copying somewhere else in the program: use std::collections::HashSet; use std::iter::FromIterator; fn hashset(data: &[u8]) -> HashSet<u8> { HashSet::from_iter(data.iter().cloned()) } Call it like hashset(&v) where v is a Vec<u8> or other thing that coerces to a slice. There are of course more ways to write this, to be generic and all that, but this answer sticks to just introducing the thing I wanted to focus on. ¹This is based on that the element type u8 is Copy , i.e. it does not have ownership semantics. The following should work nicely; it fulfills your requirements: use std::collections::HashSet; use std::iter::FromIterator; fn vec_to_set(vec: Vec<u8>) -> HashSet<u8> { HashSet::from_iter(vec) } from_iter() works on types implementing IntoIterator , so a Vec argument is sufficient. Ad...

C++ Erase Vector Element By Value Rather Than By Position?

Answer : How about std::remove() instead: #include <algorithm> ... vec.erase(std::remove(vec.begin(), vec.end(), 8), vec.end()); This combination is also known as the erase-remove idiom. You can use std::find to get an iterator to a value: #include <algorithm> std::vector<int>::iterator position = std::find(myVector.begin(), myVector.end(), 8); if (position != myVector.end()) // == myVector.end() means the element was not found myVector.erase(position); You can not do that directly. You need to use std::remove algorithm to move the element to be erased to the end of the vector and then use erase function. Something like: myVector.erase(std::remove(myVector.begin(), myVector.end(), 8), myVec.end()); . See this erasing elements from vector for more details.

Advantages Of Using Arrays Instead Of Std::vector?

Answer : In general, I strongly prefer using a vector over an array for non-trivial work; however, there are some advantages of arrays: Arrays are slightly more compact: the size is implicit. Arrays are non-resizable; sometimes this is desirable. Arrays don't require parsing extra STL headers (compile time). It can be easier to interact with straight-C code with an array (e.g. if C is allocating and C++ is using). Fixed-size arrays can be embedded directly into a struct or object, which can improve memory locality and reducing the number of heap allocations needed. Because C++03 has no vector literals. Using arrays can sometime produce more succinct code. Compared to array initialization: char arr[4] = {'A', 'B', 'C', 'D'}; vector initialization can look somewhat verbose std::vector<char> v; v.push_back('A'); v.push_back('B'); ... I'd go for std::array available in C++0x instead of plain arrays which can...

Calculating A 2D Vector's Cross Product

Answer : Implementation 1 returns the magnitude of the vector that would result from a regular 3D cross product of the input vectors, taking their Z values implicitly as 0 (i.e. treating the 2D space as a plane in the 3D space). The 3D cross product will be perpendicular to that plane, and thus have 0 X & Y components (thus the scalar returned is the Z value of the 3D cross product vector). Note that the magnitude of the vector resulting from 3D cross product is also equal to the area of the parallelogram between the two vectors, which gives Implementation 1 another purpose. In addition, this area is signed and can be used to determine whether rotating from V1 to V2 moves in an counter clockwise or clockwise direction. It should also be noted that implementation 1 is the determinant of the 2x2 matrix built from these two vectors. Implementation 2 returns a vector perpendicular to the input vector still in the same 2D plane. Not a cross product in the classical sense but c...

Android Mirror Vector Drawable

Image
Answer : For those who Use ImageView or TextView or EditText Scale works perfectly. Use android:scaleX="-1" //To flip horizontally or android:scaleY="-1" //To flip vertically OR Try android:rotationX="180" // for horizontal android:rotationY="180" // for vertical OR Simply rotation="180" for vertical android:rotation="180" // for vertical Edit: Additional If you want to flip/mirror icons/drawable when changing language RTL/LTR ("Right To Left"/"Left To Right"), there is a nice way of doing so in android vector drawable just check the ckeckbox Enable auto mirroring for RTL layout . => Right Click on drawable folder => New => Vector Asset => Select drawable => check the Checkbox . I am using AndroidStudio 3.0.1 in Windows 10 . well, there is no need to create another vector image, you can do it with one single vector image just make sure you do the following st...

Choice Between Vector::resize() And Vector::reserve()

Answer : The two functions do vastly different things! The resize() method (and passing argument to constructor is equivalent to that) will insert or delete appropriate number of elements to the vector to make it given size (it has optional second argument to specify their value). It will affect the size() , iteration will go over all those elements, push_back will insert after them and you can directly access them using the operator[] . The reserve() method only allocates memory, but leaves it uninitialized. It only affects capacity() , but size() will be unchanged. There is no value for the objects, because nothing is added to the vector. If you then insert the elements, no reallocation will happen, because it was done in advance, but that's the only effect. So it depends on what you want. If you want an array of 1000 default items, use resize() . If you want an array to which you expect to insert 1000 items and want to avoid a couple of allocations, use reserve() . ...