|
お世話になっております。DD.でございます。
現在επιστημη様のHP等を徘徊しておりまして。
そこにVisual C++ 4.0とStandard C++ Library v1.2で書かれたという
(その頃にはプログラムのプの字も知りませんでしたが^^;)
STLのサンプルが乗っています。
入力された単語の出現頻度を求めるものです。
失礼してリンクを貼らせて頂きます<(_ _)>↓こちらなのですが、
http://www02.so-net.ne.jp/~epi-/html/stl/wordcount.html
そしてこれをVC++2005BETA2:XPの環境でお試ししようとしております。
以下ソースを抜粋:
#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <algorithm>
typedef std::string Key;
struct Count
{
int n_;
Count() : n_( 0 ) {}
void operator ++(int) { n_++; }
operator int() const { return n_; }
};
typedef std::map< Key, Count, std::less< Key > > Map;
typedef std::istream_iterator< Key, std::ptrdiff_t > str_in;
void print_pair( const std::map< std::string, Count,
std::less< std::string > >::value_type &p )
{
std::cout << p.first << ':' << p.second << std::endl;
}
struct count_word
{
std::map< std::string, Count,
std::less< std::string > > &map_;
count_word( std::map< std::string, Count,
std::less< std::string > > &m ) : map_( m ) {}
void operator () ( const std::map< std::string, Count, std::less< std::string > >::key_type &k )
{
map_[k]++;
}
};
int main()
{
std::map< std::string, Count,
std::less< std::string > > wordmap;
std::for_each( std::istream_iterator< std::string, std::ptrdiff_t >( std::cin ), std::istream_iterator< std::string, std::ptrdiff_t >(),
count_word( wordmap ) );
std::for_each( wordmap.begin(), wordmap.end(), print_pair );
return 0;
}
typedef してある部分も展開してしまってまして申し訳ないです。
(自分なりに判別つきやすいように...)
以上のコードに対して下記エラーがでてしまいます。
wordcount.cpp(38) : error C2440: '<function-style- : 'std::istream' から 'std::istream_iterator<_Ty,_Elem>' に変換できません。
main 内の最初の for_each に対してです。
なぜ istream_iterator に対して、istream との変換エラーがでてくるのでしょうか?
istream_iteratorの仕様が変わったのかな?とか思ってみたりするのですが
なぜこのようなエラーになるのか原因を教えて頂ければと思います。
ストリーム系イテレータに関して未だ知識が乏しく、
勉強させて頂きたいと思います。
|