3

簡単なことです。しかし、私は最後の 1 時間を費やし、理解できませんでした。

次のコードをコンパイルすると:

#include <iostream>
#include <sort.h>
#define array_len(arr) ( sizeof(arr) / sizeof (*arr) )

using namespace std;

template<typename ITER>
void printIt_works(ITER b, ITER e) {
    for_each(b, e, [](int it) { cout << it; } ); // putting int explicitly would work
                                                 // but it's not generic
}

template<typename ITER>
void printIt_doesnt_work(ITER b, ITER e) {
    for_each(b, e, [](ITER it) { cout << *it; } );
}

int main() {
    int a[] = {5, 2, 4, 6, 1, 3};

    printIt_doesnt_work(a, a+array_len(a)); // how to make this work in a generic way.

    //merge_sort(a, a+array_len(a));
    selection_sort(a, 6);
    insertion_sort_decending(a, 6);
    insertion_sort(a, 6);
    return 0;
}

私が得るコンパイルエラーは次のとおりです。

In file included from d:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/algorithm:63:0,
                 from D:\Workspaces\CodeBlocks\Test\main.cpp:4:
d:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_algo.h: In function '_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = int*, _Funct = printIt_doesnt_work(ITER, ITER) [with ITER = int*]::<lambda(int*)>]':
D:\Workspaces\CodeBlocks\Test\main.cpp:17:5:   instantiated from 'void printIt_doesnt_work(ITER, ITER) [with ITER = int*]'
D:\Workspaces\CodeBlocks\Test\main.cpp:23:42:   instantiated from here
d:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_algo.h:4185:2: error: invalid conversion from 'int' to 'int*'
d:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_algo.h:4185:2: error:   initializing argument 1 of 'printIt_doesnt_work(ITER, ITER) [with ITER = int*]::<lambda(int*)>'

d:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_algo.h:4185 は、for_each3 番目のパラメーターとして渡された関数を呼び出す場所です。__f(*__first);

私は問題を理解しています。私のラムダ関数は期待するように宣言されてint*いますが、のテンプレートインスタンスfor_eachint. 私はそれをある意味で解決する方法を知りませんgeneric

もちろん、型を明示的にすることで回避できますが、それは一般的ではありません。

for_each(b, e, [](int it) { cout << it; } );
4

2 に答える 2

6

1 つのオプションは、newdecltypeキーワードを使用して、反復される対象のタイプを判別することです。

template<typename ITER>
void printIt_works(ITER b, ITER e) {
    for_each(b, e, [](decltype(*b) it) { cout << it; } );
}

これにより、引数の型が反復されるものの型であるラムダが作成されます。これはまさにあなたが望むものです。

decltypeコンパイラで利用できない場合は、より大きなiterator_traitsタイプを使用してこれを行うことができます。

template<typename ITER>
void printIt_works(ITER b, ITER e) {
    for_each(b, e, [](typename std::iterator_traits<ITER>::reference it) { cout << it; } );
}

しかし、それは本当に醜いです。

お役に立てれば!

于 2012-03-14T21:57:09.003 に答える
6

ラムダ関数を使用する代わりに、次を使用できますostream_iterator

#include <iterator>
#include <algorithm>

template<typename Iterator>
void printIt(const Iterator& begin, const Iterator& end) {
  typedef typename std::iterator_traits<Iterator>::value_type value_type;
  std::copy(begin, end, std::ostream_iterator<value_type>(cout));
}
于 2012-03-14T22:46:50.037 に答える