Best way to extract a subvector from a vector?
vector<T>::const_iterator first = myVec.begin() + 100000;
vector<T>::const_iterator last = myVec.begin() + 101000;
vector<T> newVec(first, last);
It's an O(N) operation to construct the new vector, but there isn't really a better way.
Extract a subvector from a vector with iterator
If you want to assign a subrange to an existing std::vector
you can use the assign()
member:
NewVec.assign(Original.begin(), Original.begin() + 5);
Of course, this assumes that Original
has at least 5 elements.
Creating a new C++ subvector?
One of std::vector
's constructor accepts a range:
std::vector<int> v;
// Populate v.
for (int i = 1; i <= 10; i++) v.push_back(i);
// Construct v1 from subrange in v.
std::vector<int> v1(v.begin() + 4, v.end() - 2);
How to extract sub-vectors of different datatypes from vector<char>?
You need to copy the bytes into objects of the right type
std::vector<char> data = /* get from network */;
Eigen::Matrix4d trajectory;
std::vector<uint8_t> rgb_image_data(rgb_image_size);
std::vector<uint8_t> gray_image_data(gray_image_size);
std::memcpy(&trajectory, data.data() + trajectory_offset, sizeof(Eigen::Matrix4d));
std::memcpy(rgb_image_data.data(), data.data() + rgb_image_offset, rgb_image_size);
std::memcpy(gray_image_data.data(), data.data() + gray_image_offset, gray_image_size);
That is assuming that the source copied in a whole Eigen::Matrix4d
, rather than the double[4][4]
it contains.
Find value in vector and extract subvector from that position
You tried to copy to somewhere which is not allocated.
You can use a constructor of std::vector
that takes two InputIterator
s and creates copy of the range.
Try this:
vector<int> vec = {10, 20, 30, 40, 50, 60};
std::vector<int>::iterator it;
it = find (vec.begin(), vec.end(), 30);
vector<int> newvec(it, vec.end());
Extracting Subvector from Vector Register in LLVM IR
shufflevector
will accomplish the same as above (provided you are only interest in %out.8
) and LLVM will replace it with a simple register name change (e.g., if %out.1
is ymm0
, %out.8
would be xmm0
).
Single line to replace eight:
%out.8 = shufflevector <8 x float> %out.0, <8 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
Related Topics
How to Implement an Autonomous 'Self' Member Type in C++
G++ Undefined Reference to Typeinfo
C++ Dynamic Shared Library on Linux
Why Use Std::Bind Over Lambdas in C++14
Random Number Generation in C++11: How to Generate, How Does It Work
Why Do C and C++ Support Memberwise Assignment of Arrays Within Structs, But Not Generally
Is There Any Use For Unique_Ptr With Array
Removing Item from Vector, While in C++11 Range 'For' Loop
Smart Pointers (Boost) Explained
Order of Calling Constructors/Destructors in Inheritance
When Vectors Are Allocated, Do They Use Memory on the Heap or the Stack
If (Cin ≫≫ X) - Why Can You Use That Condition
Why Does Pointer Decay Take Priority Over a Deduced Template
How to Sum Up Elements of a C++ Vector
Can Inner Classes Access Private Variables
How to Read a Complete Line from the User Using Cin