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.
Related
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.
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.
For a nested vector, I want to have a function that returns the index of the subvector that contains a certain item. For example, given this nested vector:
vector<vector<int>> vec {{1, 2, 3},
{4, 5, 6},
{7, 8, 9, 10}};
I want to have a function that acts like so:
find_subvector(vec, 5); //returns 1 because V[1] contains the element 5.
All of the items among all of the subvectors are unique.
I do have a solution currently, which is to iterate over all of the subvectors and use std::find():
int find_subvector(const vector<vector<int>>& vec, int num) {
for (int i {}; i < (int)vec.size(); i++)
if (find(vec[i].begin(), vec[i].end(), num) != vec[i].end())
return i;
return -1;
}
But I wanted to know if there was a faster solution, because I will be running this function repeatedly.
I want to check whether given two integers in a specific order are in the same order in a given integer array.
I wonder whether there is an easy way to do this like a built-in CPP method.
If there is no built-in method, suggest me an efficient way to do this as I have a few sets of two integers (not only one set) to check over one array.
given two numbers: 8 3
given array: 2 8 6 1 3 9
output: YES
You could do something like
bool check(std::pair<int, int> numbers = {8, 3},
std::array<int, 6> arr = {2, 8, 6, 1, 3, 9}) {
if (numbers.first != numbers.second)
return std::find(std::find(std::begin(arr), std::end(arr), numbers.first), std::end(arr), numbers.second) == std::end(arr);
return std::count(std::begin(arr), std::end(arr), numbers.first) >= 2;
}
If both numbers are different the inner find searches for the first value. The outer find starts at the position of the first value and searches for the second value.
Else the count is checked.
You could also try:
std::array<int, 6> content = {2, 3, 6, 1, 8, 9};
auto lookup = [content](int a, int b)
{
return std::distance(std::find(content.begin(), content.end(), a), std::find(content.rbegin(), content.rend(), b));
};
lookup(8, 3);
lookup will be positive if 8 comes before 3 and negative otherwise.
Search the entire container to find the first one. Search from the position of the first one to the end of the container to find the second one. If that search succeeds, they're in the expected order. If not, they're not.
int first_value = 8;
int second_value = 3;
std::array<int, 6> values = { 2, 8, 6, 1, 3, 9 };
auto first_pos = std::find(values.begin(), values.end(), first_value);
if (first_pos != values.end())
++first_pos;
auto second_pos = std::find(first_pos, values.end(), second_value);
if (second_pos != values.end())
std::cout << "YES\n";
Use adjacent find. I suppose find_if could also do the job.
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);