How to find a list that only contains a certain field and no other fields? eg:
[a, b, c, d]
[a, b, c]
[a, b]
find the list containing only a and b: [a, b]
How to find a list that only contains a certain field and no other fields? eg:
[a, b, c, d]
[a, b, c]
[a, b]
find the list containing only a and b: [a, b]
Here I have implemented what you need. The input is 2d vector, which contains the input that you have mentioned as three lists. The check vector contains the list which you want to check if exists in the input.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
vector<vector<char>> input = {
{'a', 'b', 'c', 'd'},
{'a', 'b', 'c'},
{'a', 'b'}
};
bool broke = false;
vector<char> check = {'a', 'b'};
sort(check.begin(), check.end());
for(int j=0; j<input.size(); ++j){
auto x = input[j];
if(x.size()!=check.size())continue;
sort(x.begin(), x.end());
for(int i=0; i<min(x.size(),check.size()); ++i){
if(check[i]!=x[i]){
broke = true;
break;
}
}
if(!broke){
cout<<j+1<<" number list is equal to required list\n";
}
broke = false;
}
return 0;
}
This code accepts dynamic input so you can play around with input and check values to understand code. I have implemented ways suggested by Cwift in previous answer.
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<char> list[] = { { 'a', 'b', 'c', 'd' },
{ 'a', 'b', 'c' },
{ 'a', 'b' }
};
for (int i = 0; i < list->size(); i++)
{
if (list[i].size() == 2 &&
find(list[i].begin(), list[i].end(), 'a') != list[i].end() &&
find(list[i].begin(), list[i].end(), 'b') != list[i].end())
{
cout << "Index: " << i << endl;
return 0;
}
}
cout << "No required list." << endl;
return 0;
}
So there is more then one solution to this task. A possible solution could be the following.
if (list.length() == 2 && list[0] == a && list[1] == b)
{
}