Delete Row in 2D Vector C++ - c++

So I have the example vector initialized as so:
vector<vector<int> > v = {{1, 2, 3},{4, 5, 6},{7, 8, 9}}
I want to totally delete the row, {4, 5, 6} such that v[1][0] references 7 and the final vector is v = {{1, 2, 3},{7, 8, 9}}
v[1].clear() does not accomplish this, and v[1].erase(v[1].begin(),v[1].begin()+3) doesnt seem to either unless I am just an idiot.
Thank you in advance!

You should erase whole nested vector, not just items inside.
If you writing v[1].erase(v[1].begin(),v[1].begin()+3), you got {{1,2,3},{},{7,8,9}}.
Just write what Albin Paul said: v.erase(v.begin() + 1) and then your vector will be v = {{1,2,3},{7,8,9}}.

Adding onto Vslav, if the vector you want to delete is conveniently at the end of your 2d matrix, then just do v.popback(). It is simpler but only works on the final element. You could also do some research on remove() function.

Related

Is there any standard variadic function for erasing multiple elements in a vector?

Take this vector:
std::vector<int> v = {1, 2, 3, 4, 5};
Let's say I want to remove some elements of a vector at some arbitrary indices: 0, 1, and 3. It's tedious to have to write something like this:
v.erase(v.begin());
v.erase(v.begin());
v.erase(v.begin() + 1);
Is there any standard function that takes in an arbitrary number of indices to erase from a vector? Something like this: v.erase(0, 1, 3);
Yes and no.
There's nothing that deals with indices. There's also nothing that deals with arbitrary elements.
But you can erase multiple items that form a contiguous range at once. So you can coalesce your first two calls to erase into one (and probably about double the speed in the process).
// erase the first two elements
v.erase(v.begin(), v.begin() + 2);
If you want to erase first three element so for this you can run.
std::vector<int> v = {1, 2, 3, 4, 5};
v.erase(v.begin(),v.begin()+3);
or,
v.erase(v.begin(),v.end()-2);

Using switch case with multidimensional array in C++

I want to start out by saying that I am really new to C++ and have not been able to find an answer to this question yet. It is for a school project and my teacher has not been very helpful.
I wanted to know how to use a switch statement with a multidimensional array. For example, say we have this array:
int arr[3][5] = {{1, 3, 1, 1, 2}, {1, 2, 4, 5, 6}, {6, 3, 4, 3, 5}};
How would I use a switch statement in order to locate the individual rows and check for certain criteria within them?
Edit: I think it is best if I clarify a bit. I want to check the conditions of the elements in each row, not just the individual elements themselves.
So I may check if there are 1's in the first subarray, check for 3's in the second subarray, and check for 4's in the last subarray.
Short answer; you wouldn't. swtich is not the right tool for the job. You are searching for some criteria right? In the absence of a specific criteria, lets say that you are interested in if the sub array contains a 1.
We can formulate this with a function:
bool contains_a_1(int arr[5]) {
return std::any_of( // This is an algorithm that will check if any of them
// match the predicate below
std::begin(arr), std::end(arr), // This is the range to check over
[](int v) { return v == 1; } // This is a simple lambda function that tells the
// algorithm what we are looking for.
);
Now we have a function that checks our condition. So now we need to check it, but how? If we want to check if any of the arrays in our array meet this condition, we have already seen a thing that can help us with this, std::any_of:
int arr[3][5] = {{1, 3, 1, 1, 2}, {1, 2, 4, 5, 6}, {6, 3, 4, 3, 5}};
bool there_is_a_one_somewhere = std::any_of(std::begin(arr), std::end(arr), contains_a_1);
Or maybe you want this bool for each value in the array:
std::array<bool, 3> contains_1;
std::transform(std::begin(arr), std::end(arr), std::begin(contains_1), contains_a_1);
Or maybe you want to do something else? Well, there is probably an algorithm for that! You just need to find the right one here in <algorithm>.
So I may check if there are 1's in the first subarray, check for 3's in the second subarray, and check for 4's in the last subarray.
You can use for loops to iterate over the rows and std::find to check whether the subarray contains a particular element or not as shown below:
#include <iostream>
#include <algorithm>
int main()
{
int arr[3][5] = {{1, 3, 1, 1, 2}, {1, 2, 4, 5, 6}, {6, 3, 4, 3, 5}};
//iterate over rows
for(auto &row: arr)
{
bool exists = std::find(std::begin(row), std::end(row), 1) != std::end(row); //check if 1 is present in the current row
std::cout<<"element "<<(exists?"is present":"not present")<<std::endl;
}
}
In the above code, we have checked whether 1 is present in the subarray or not. Similarly you can check for other values like 3 or 4.

What is more efficient? vector.assign vs vector.erase

When a vector exists and tries to erase the back of that vector.
Is it efficient to use an 'vector.assign' in terms of time complexity? Or is it efficient to use 'vector.erase'?
Please let me know the time complexity in each case.
[For example]
vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// 1. use assign
v.assign(v.begin(), v.begin() + 5);
// 2. use erase
v.erase(v.begin() + 5, v.end());
I would like to use vectors that consist only of elements from the beginning section of vectors to a certain index.
It's most efficient to use resize, because that's what that function is for.
For what its worth, self-assignment is not allowed for containers.

mapping 1D array with two points

I want to store random snapshots of a a 1D array arr using a mapping of 2 points say a,b to utilise in a dynamic program. like say
P1 => (4,5) = [1,1,0,2,1]
P2 => (10,13) = [5,6,3,4,3]
P3 => (15,23) = [11,13,9,12,14]
so on.....
Later I need to add them to array A having also n elements like arr.
like say A = [1,1,1,1,1]
so now after P1 i have A as : [2,2,1,3,2] and so on till P ends.
I am not sure how to map these points P1,P2 ... with the array arr and then later sum it with array A.
I tried using a 3D array to store those points and then the array.But it seems bulky and not utilised in an efficient way.
Any help is appreciated.
You can use a
std::map<std::pair<int, int>, std::vector<int>> m;
then you can use it as m[{10, 20}].push_back(42);.
The data in the question for example could be stored with
m[{4, 5}] = {1, 1, 0, 2, 1};
m[{10, 13}] = {5, 6, 3, 4, 3};
m[{15, 23}] = {11, 13, 9, 12, 14};
According to this answer posted by GManNickG https://stackoverflow.com/a/2197015/7943474, un unordered_map will use more memory but in your case it can be much faster since you are not going to add/delete points once you initialize your set of data.
In this case you should consider:
#include <unordered_map>
std::unordered_map<std::pair<int, int>, std::vector<int>> uMap;
and then insert your element as:
std::pair<int, int> point(4, 5);
std::vector<int> arr;
arr.push_back(1);
arr.push_back(1);
and so on..
uMap.insert(point, arr);
To find and element in the uMap, you can use the find() method:
std::unordered_map<std::pair<int, int>, std::vector<int>>::const_iterator it = uMap.find(point);
and then update data with
it->second[0] + A[0];
it->second[1] + A[1];
and so on..
For more reference see http://www.cplusplus.com/reference/unordered_map/unordered_map/

C++\CLI Sorting a 2-D List based on the value of the first index?

How to sort a 2-D List based on the value of the first index of the sub-list?
Say I have a 2-D List "lst" that contains the following values:
List<List<int>^>^:
{
{3, 1, 3},
{2, 5, 6},
{1, 4, 3}
}
After the sort, "lst[0]" should be "{1, 4, 3}" and "lst[0][0]" should be "1". How to do that? I am using C++\CLI, not C#, unfortunately. Thanks in advance.
I'm not great with C++/CLI, so take this with a grain of salt.
I think https://stackoverflow.com/a/15974889/56778 gives the basic idea. Modified for your lists, it would be something like:
static int AnonymousMethod1(List<int>^ p1, List<int>^ p2)
{
return p1[0].CompareTo(p2[0]);
}
Comparison<List<int>^> ^ comparisonDelegate = gcnew Comparison<List<int>^>(&AnonymousMethod1);
lstTest->Sort(comparisonDelegate);