http://www.un.org/Depts/DGACM/index_french.htm t 系指<代码>std:set——它简单是指一套逻辑;一组内容。 如果对这两种收集进行分类,你可以简单地读到<条码>_intersection。 这两辆集装箱到第三集装箱。
vector<int> common;
set_intersection(v.begin(), v.end(), s.begin(), s.end(), back_inserter(common));
EDIT:
下面是上述例子。 这使用C++11 lambdas,但如果你没有C++11或能够使用斜体,你可以自行使用ctor子。 指出缺乏明确的住所。
#include <set>
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
#include <iostream>
using namespace std;
static const int numbers[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181};
static const size_t num_numbers = sizeof(numbers)/sizeof(numbers[0]);
int main()
{
/*** GET THE SET ****/
set<int> s(begin(numbers), end(numbers));
//copy(&numbers[0], &numbers[num_numbers], inserter(s, s.begin()));
/*** GET THE NUMBERS TO LOOK FOR **/
int first = 5, last = 10;
vector<int> targets;
generate_n(back_inserter(targets), last-first, [&first]() -> int {
return first++;
});
/*** FIND THE INTERSECTION ***/
vector<int> common;
set_intersection(s.begin(), s.end(), targets.begin(), targets.end(), back_inserter(common));
/*** DUMP RESULTS ***/
cout << "Intersecton of:
";
copy(s.begin(), s.end(), ostream_iterator<int>(cout," "));
cout << "
with:
";
copy(targets.begin(), targets.end(), ostream_iterator<int>(cout," "));
cout << "
= = = = = = = =
";
copy(common.begin(), common.end(), ostream_iterator<int>(cout," "));
cout << "
";
}
产出是:
Intersecton of:
0 1 2 3 5 8 13 21 34
55 89 144 233 377 610 987 1597 2584 4181
with:
5 6 7 8 9
= = = = = = = =
5 8