这是非常begineer的做法,但我想用我从折叠和STL了解到
的方法的说明:
1.Created向量的列表(用于输入目的可以是无论如何周围)
2.Kept主向量v将存储主折叠矢量
3.used STL包括折叠之前继续检查如果序列是本
组输入
std::vector<int> x ={1,2,3};
std::vector<int> y ={7,8,9};
std::vector<int> z ={1,2,3};
std::vector<int> a ={1,2,3};
std::vector<int> v5 = {11,1,1,1}; //as mentioned in question
std::vector<int> v6 = {1,1,1,1}; //as mentioned in question
方法
#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
template <typename T>
void Concat(std::vector<T>& v, const std::vector<T>& v2)
{
v.insert(v.end(), v2.begin(), v2.end());
}
template <typename T>
void Concat(std::vector<T>& v, const T& value)
{
v.push_back(value);
}
template<typename T, typename... Args>
void push_back_vec(std::vector<T>& v, Args&&... args)
{
(Concat(v, args), ...);
}
int main()
{
std::vector<int> v;
std::list<std::vector<int> > m ;
std::vector<int> x ={1,2,3};
std::vector<int> y ={7,8,9};
std::vector<int> z ={1,2,3};
std::vector<int> a ={1,2,3};
std::vector<int> v5 = {11,1,1,1};
std::vector<int> v6 = {1,1,1,1};
m.push_back(x);
m.push_back(y);
m.push_back(z);
m.push_back(a);
m.push_back(v5);
m.push_back(v6);
for (std::list<std::vector<int> >::iterator it1 = m.begin(); it1 != m.end(); ++it1)
{
if (std::includes(v.begin(), v.end(), (*it1).begin(), (*it1).end()))
{
std::cout<<"Already present"<<std::endl;
}
else
{
push_back_vec(v,(*it1));
}
}
for (int i : v) std::cout << i << ' ';
}
输出
Already present
Already present
1 2 3 7 8 9 11 1 1 1 1 1 1 1 Program ended with exit code: 0
我知道可以有很大的改进,它可以在某个角落的情况下失败。这仅仅是尝试随意批评和帮助我提高
转换的向量,以逗号分隔的字符串不应该是非常繁琐的。这似乎是解决这个问题的最简单方法。 – Barmar
我觉得一个哈希函数将有助于 –
你考虑[布隆过滤器(https://en.wikipedia.org/wiki/Bloom_filter)? –