Templateとか習ったけどTemplate便利でいいなー
でも思いついたコードが通らなくてずっと首傾げてた。
#include <iostream> #include <vector> #include <algorithm> #include <iterator> template <class T> print(vector<T> &v){ vector<T>::iterator itr; std::cout << "start --> "; for(itr = v.begin(); itr != v.end(); itr++){ std::cout << *itr << " --> "; } std::cout << "end" << std::endl; }
もらったvectorの内部に対してイテレータを使って出力していく形式。
いけると思ったんだけどコンパイルエラーが出る。
何でも2行目のvector
TwitterのTLに流しておいたら何人かの方からいけるはずと言われたんだけど動かない。
別のコードとしてはcpp_akiraさんからこんなのいただいた。
#include <iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std; template <class T> print(vector<T> &v){ for (vector<T>::const_iterator it = v.begin(), last = v.end(); it != last; ++it) { cout << *it << endl; } vector<T>::iterator itr;
これは無事に動いたけど何で上のコードは動かないかなー
クラスの継承もなんか変な感じで面白かった。
多言語訪問はたまにはやってみるもんだなと思う。
あとC++の例外処理は例外処理の役目を果たしてるのか疑問だらけw
そんなわけでC++入門篇は終わり。
今度は8月の頭に応用編を受けるつもり。
業務ではC++なんか使わないけどな!