...ing logging 4.0

はてなブログに移行しました。D言語の話とかいろいろ。

クロスリファレンス作成プログラム

http://mixi.jp/view_diary.pl?id=121472047&owner_id=842935より.
お題はJavaなんだけどC++じゃダメですか?(^^;

コンマとかピリオドとかアポストロフィーとか(・・・)って何? 食えるの?
ちゅーか,これじゃメモリ使いまくりで効率悪いですか,そうですか.
もっとエレガントに書けるという猛者はぜひ教えてください.

あ,ソースをHTMLに変換してくれるページを見つけたので貼っておきます.
http://www9.plala.or.jp/sgwr-t/c_sub/ConvHTML.html

ソース:

001 #include <iostream>
002 #include <string>
003 #include <map>
004 #include <fstream>
005 #include <sstream>
006 #include <list>
007 using namespace std;
008
009 typedef map<string, list<int> > CrossReference;
010 typedef list<string> WordList;
011
012 int main(int argc, char* argv[])
013 {
014 CrossReference ref;
015 WordList words;
016 ifstream file(argv[1]);// 第一コマンドライン引数のファイル名でオープン
017 int lineNumber = 0;
018 while( !file.eof() )// ファイルをEOFまで読み込み
019 {
020 string lineBuffer;
021 getline( file, lineBuffer);// 一行読み込み
022 istringstream ss( lineBuffer );
023 string wordBuffer;
024 while( ss >> wordBuffer )// 空白区切りでトークン分割
025 {
026 ref[wordBuffer].push_back( lineNumber );// 単語をキーとした連想配列に行番号を追加
027 words.push_back( wordBuffer );// 単語一覧に単語追加
028 }
029 lineNumber++;
030 }
031 file.close();// お行儀
032 words.sort(); // これと
033 words.unique();// これで単語の重複除去
034
035 for(WordList::iterator iter = words.begin(); iter != words.end(); iter++)
036 {
037 ref[*iter].sort();// これと
038 ref[*iter].unique();// これで行番号の重複除去
039 CrossReference::mapped_type::iterator iter_line = ref[*iter].begin();
040 cout << *iter << "\t";
041 while( iter_line != ref[*iter].end() )// 単語に関連付けられた行番号の数だけループ
042 {
043 cout << *iter_line + 1 << ", ";// 行番号は1から始めます
044 iter_line++;
045 }
046 cout << endl;
047 }
048
049 return 0;
050 }


テキストファイル:

At Microsoft Research, we have an insatiable curiosity and
the desire to create new technology that will help define
the computing experience. Whether inspired by a suggestion
from a customer or simply the search for a better way, we're
driven to innovate and push the state of the art in computer
science as far as our imaginations can reach. To that end,
we collaborate with universities, submit papers for peer review,
and partner with product groups to bring our research to you.
Read on to discover what we're doing to improve Microsoft
products in the next two to ten years.

出力結果:

At	1, 
Microsoft 1, 9,
Read 9,
Research, 1,
To 6,
Whether 3,
a 3, 4,
an 1,
and 1, 5, 8,
art 5,
as 6,
better 4,
bring 8,
by 3,
can 6,
collaborate 7,
computer 5,
computing 3,
create 2,
curiosity 1,
customer 4,
define 2,
desire 2,
discover 9,
doing 9,
driven 5,
end, 6,
experience. 3,
far 6,
for 4, 7,
from 4,
groups 8,
have 1,
help 2,
imaginations 6,
improve 9,
in 5, 10,
innovate 5,
insatiable 1,
inspired 3,
new 2,
next 10,
of 5,
on 9,
or 4,
our 6, 8,
papers 7,
partner 8,
peer 7,
product 8,
products 10,
push 5,
reach. 6,
research 8,
review, 7,
science 6,
search 4,
simply 4,
state 5,
submit 7,
suggestion 3,
technology 2,
ten 10,
that 2, 6,
the 2, 3, 4, 5, 10,
to 2, 5, 8, 9, 10,
two 10,
universities, 7,
way, 4,
we 1, 7,
we're 4, 9,
what 9,
will 2,
with 7, 8,
years. 10,
you. 8,