How can we do slicing of an array or list like we do in python…both for int and character type array
Slicing of a array
hello @prerak_semwal
there are various ways.
like u can use copy function
or range constructors
or inbuilt slice function.
example->
// Function to slice a given vector
// from range X to Y
vector<int> slicing(vector<int>& arr,
int X, int Y)
{
// Starting and Ending iterators
auto start = arr.begin() + X;
auto end = arr.begin() + Y + 1;
// To store the sliced vector
vector<int> result(Y - X + 1);
// Copy vector using copy function()
copy(start, end, result.begin());
// Return the final sliced vector
return result;
}
pls refer this for examples of other methods -> link
@aman212yadav
I haven’t learned about vectors is there any other way … talking w.r.t character arrays ,int arrays, string objects (in string library)
there are function like substr that u can use to slice ur c++ string.
for char array read about functions like strncpy ,memcpy function.
for int array u need to deal it manually , i dont have any function in my mind.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.