c++中includes判断一个序列是否包含另一个序列
#include<iostream>#include<vector>#include<algorithm>#include<string>usingnamespacest...
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
void Output(const string& val)
{
cout << val << endl;
}
void main()
{
vector<string> strVect1;
vector<string> strVect2;
strVect1.push_back("Sunday");
strVect1.push_back("Monday");
strVect1.push_back("Over");
strVect1.push_back("Wednesday");
strVect2.push_back("Monday");
strVect2.push_back("Sunday");
strVect2.push_back("Over");
strVect2.push_back("Saturday");
sort(strVect1.begin(),strVect1.end());
sort(strVect2.begin(),strVect2.end());
cout << "Vect1 :" << endl;
for_each(strVect1.begin(),strVect1.end(),Output);
cout << endl;
cout << "Vect2 :" << endl;
for_each(strVect2.begin(),strVect2.end(),Output);
cout << endl;
bool result=includes(strVect1.begin(),strVect1.end(),
strVect2.begin()+1,strVect2.begin()+2);//strVect2.begin()+1和strVect2.begin()+2表示容器中的设么区间呢?是第一个元素和第二个元素?还是第二个元素和第三个元素呢?还有就是对includes的用法不是太清楚,能不能再写个实例
详细讲解一下!谢啦! if(result)
cout << "result : OK" << endl;
else
cout <<"result : ERROR"<<endl;
} 展开
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
void Output(const string& val)
{
cout << val << endl;
}
void main()
{
vector<string> strVect1;
vector<string> strVect2;
strVect1.push_back("Sunday");
strVect1.push_back("Monday");
strVect1.push_back("Over");
strVect1.push_back("Wednesday");
strVect2.push_back("Monday");
strVect2.push_back("Sunday");
strVect2.push_back("Over");
strVect2.push_back("Saturday");
sort(strVect1.begin(),strVect1.end());
sort(strVect2.begin(),strVect2.end());
cout << "Vect1 :" << endl;
for_each(strVect1.begin(),strVect1.end(),Output);
cout << endl;
cout << "Vect2 :" << endl;
for_each(strVect2.begin(),strVect2.end(),Output);
cout << endl;
bool result=includes(strVect1.begin(),strVect1.end(),
strVect2.begin()+1,strVect2.begin()+2);//strVect2.begin()+1和strVect2.begin()+2表示容器中的设么区间呢?是第一个元素和第二个元素?还是第二个元素和第三个元素呢?还有就是对includes的用法不是太清楚,能不能再写个实例
详细讲解一下!谢啦! if(result)
cout << "result : OK" << endl;
else
cout <<"result : ERROR"<<endl;
} 展开
1个回答
展开全部
includes 算法有两个形式
template<class InputIterator1, class InputIterator2>
bool includes(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2
);
template<class InputIterator1, class InputIterator2, class BinaryPredicate>
bool includes(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
BinaryPredicate _Comp
);
判断_First2 和 _Last2 是否为 _First1 和 _Last1 的子区间,两者都是半开半闭区间,
strVect2.begin()+1,strVect2.begin()+2 表示第二个元素
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
vector<int> coll;
for(int i=0;i!=9;++i)
coll.push_back(i);
vector<int> ivec;
for(int i=1;i!=3;++i)
ivec.push_back(i);
if(includes(coll.begin(),coll.end(),
ivec.begin(),ivec.end()))
cout<<"all elements of ivec are also in coll."<<endl;
else cout<<"not all elements of ivec are also in coll."<<endl;
template<class InputIterator1, class InputIterator2>
bool includes(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2
);
template<class InputIterator1, class InputIterator2, class BinaryPredicate>
bool includes(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
BinaryPredicate _Comp
);
判断_First2 和 _Last2 是否为 _First1 和 _Last1 的子区间,两者都是半开半闭区间,
strVect2.begin()+1,strVect2.begin()+2 表示第二个元素
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
vector<int> coll;
for(int i=0;i!=9;++i)
coll.push_back(i);
vector<int> ivec;
for(int i=1;i!=3;++i)
ivec.push_back(i);
if(includes(coll.begin(),coll.end(),
ivec.begin(),ivec.end()))
cout<<"all elements of ivec are also in coll."<<endl;
else cout<<"not all elements of ivec are also in coll."<<endl;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询