掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
関数オブジェクト呼び出し方 (ID:72694)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
お世話になります。 現在、C++を勉強しているのですが、関数オブジェクトの呼び出し方について 教えて頂けないでしょうか。 下記のサンプルコードの★でrandom_shuffleの第3引数に関数オブジェクト を指定すると思っているのですが、なぜRandomクラスのインスタンスrを指定 しても関数オブジェクトを呼ぶことができるのでしょうか。 r ではなく Random() ではないのでしょうか。 どうかご教授をお願いできないでしょうか。 よろしくお願いします。 #include <iostream> #include <algorithm> #include <vector> #include <cstdlib> #include <ctime> class Random { public: // コンストラクタ Random() { srand(static_cast<unsigned int>( time(NULL) )); } // 関数オブジェクト unsigned int operator() (unsigned int max) { double tmp = static_cast<double>( rand() ) / static_cast<double>(RAND_MAX); return static_cast<unsigned int>( tmp * max ); } }; int main() { using namespace std; vector<int> nums; int i; for(i = 0; i < 10; i++) nums.push_back(i); Random r; random_shuffle( nums.begin(), nums.end(), r); // ★ for(i = 0; i < 10; i++) cout << nums[i] << endl; return 0; }
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.