2016-08-25 13 views
-1

std :: set_intersectionに関する質問が必要です。私はリストにそれを使用していると私は配列とリストのstd :: set_intersection

std::set_intersection(list1.begin(),list1.end(),list2.begin(),list2.end(), std::back_inserter(Get_intersect)); 

を使用していた場合にうまく機能している。しかし、私は、配列とそれを使ったことがありません。ここで配列を取るのはどうですか?私は議論で配列を取って、交差点を実行したい。 syntax error : ']'

std::set_intersection(a[].begin(),a[].end(),list2.begin(),list2.end(), std::back_inserter(Get_intersect)); 

このエラーを取得します。この[]a[]から削除すると、配列を開始および終了できません。

+0

これは明らかに 'set_intersection'とは関係ありません。実際にはポインタを渡すことができます。イテレータを配列から開始および終了する方法を調べるだけで済みます。 – juanchopanza

+0

@juanchopanzaはい私は、同じ関数がリストとうまくいっているので、set_intersectionに何の問題もないことを知っています。配列には問題のみがあります。 – noman

+0

なぜあなたのタイトルと質問は 'set_intersection'に焦点を当てていますか? – juanchopanza

答えて

0

std::beginstd::endを使用します。

std::set_intersection(
    std::begin(a), std::end(a), 
    list2.begin(), list2.end(), 
    std::back_inserter(Get_intersect) 
); 

aが設定されていないので、あなたがset_intersection documentationを参照して、作業するstd::set_intersectionための前提条件である(それがソートされていることを自分で世話をする必要があることに注意が

全例:。

あなたは、実際の配列(およびないポインタ)を持っている場合
#include <algorithm> 
#include <iostream> 
#include <iterator> // for std::begin and std::end 
#include <set> 
#include <vector> 

void printVector(std::vector<int> vec) 
{ 
    for (std::vector<int>::const_iterator i = vec.begin(); i != vec.end(); ++i) 
    { 
     std::cout << *i << ' '; 
    } 
    std::cout << std::endl; 
} 

int main(int argc, char ** argv) 
{ 
    int a[5] = { 1, 2, 3, 4, 5 }; 
    std::vector<int> list2; 
    list2.push_back(2); 
    list2.push_back(4); 
    std::vector<int> intersection; 
    std::set_intersection(
     std::begin(a), std::end(a), 
     list2.begin(), list2.end(), 
     std::back_inserter(intersection) 
    ); 
    printVector(intersection); 
} 
+0

ありがとうございました:) – noman

0

あなたは、配列のための「イテレータ」を取得するためにstd::beginstd::endを使用することができます。

基本的にはポインタの最初と最後(プラス1)にポインタを与えています。

int* pointer_to_array = ...; 
size_t num_elements = ...; 

some_function_taking_iterators(
    pointer_to_array, // This is the begin "iterator" 
    pointer_to_array + num_elements // This is the end "iterator" 
); 
+0

@jaochimありがとうございます:) – noman