2016-05-21 7 views
0

私はC++を初めて使い、このコードがどのように機能するかを理解しようとしていますが、includeguardの仕組みを理解することができます。また、ネストされた名前空間gtlとテンソルフローを使用する方法。私はこのコードはtensorflowのコードを理解するC++ API

// This file provides utility functions for use with STL map-like data 
// structures, such as std::map and hash_map. Some functions will also work with 
// sets, such as ContainsKey(). 

#ifndef TENSORFLOW_LIB_GTL_MAP_UTIL_H_ 
#define TENSORFLOW_LIB_GTL_MAP_UTIL_H_ 
#include <stddef.h> 
#include <iterator> 
#include <memory> 
#include <string> 
#include <utility> 

namespace tensorflow { 
namespace gtl { 

// Returns a pointer to the const value associated with the given key if it 
// exists, or NULL otherwise. 
template <class Collection> 
const typename Collection::value_type::second_type* FindOrNull(
    const Collection& collection, 
    const typename Collection::value_type::first_type& key) { 
    typename Collection::const_iterator it = collection.find(key); 
    if (it == collection.end()) { 
    return 0; 
    } 
    return &it->second; 
} 

// Same as above but returns a pointer to the non-const value. 
template <class Collection> 
typename Collection::value_type::second_type* FindOrNull(
    Collection& collection, // NOLINT 
    const typename Collection::value_type::first_type& key) { 
    typename Collection::iterator it = collection.find(key); 
    if (it == collection.end()) { 
    return 0; 
    } 
    return &it->second; 
} 

// Returns the pointer value associated with the given key. If none is found, 
// NULL is returned. The function is designed to be used with a map of keys to 
// pointers. 
// 
// This function does not distinguish between a missing key and a key mapped 
// to a NULL value. 
template <class Collection> 
typename Collection::value_type::second_type FindPtrOrNull(
    const Collection& collection, 
    const typename Collection::value_type::first_type& key) { 
    typename Collection::const_iterator it = collection.find(key); 
    if (it == collection.end()) { 
    return typename Collection::value_type::second_type(); 
    } 
    return it->second; 
} 

// Returns a const reference to the value associated with the given key if it 
// exists, otherwise returns a const reference to the provided default value. 
// 
// WARNING: If a temporary object is passed as the default "value," 
// this function will return a reference to that temporary object, 
// which will be destroyed at the end of the statement. A common 
// example: if you have a map with string values, and you pass a char* 
// as the default "value," either use the returned value immediately 
// or store it in a string (not string&). 
template <class Collection> 
const typename Collection::value_type::second_type& FindWithDefault(
    const Collection& collection, 
    const typename Collection::value_type::first_type& key, 
    const typename Collection::value_type::second_type& value) { 
    typename Collection::const_iterator it = collection.find(key); 
    if (it == collection.end()) { 
    return value; 
    } 
    return it->second; 
} 

// Inserts the given key and value into the given collection if and only if the 
// given key did NOT already exist in the collection. If the key previously 
// existed in the collection, the value is not changed. Returns true if the 
// key-value pair was inserted; returns false if the key was already present. 
template <class Collection> 
bool InsertIfNotPresent(Collection* const collection, 
         const typename Collection::value_type& vt) { 
    return collection->insert(vt).second; 
} 

// Same as above except the key and value are passed separately. 
template <class Collection> 
bool InsertIfNotPresent(
    Collection* const collection, 
    const typename Collection::value_type::first_type& key, 
    const typename Collection::value_type::second_type& value) { 
    return InsertIfNotPresent(collection, 
          typename Collection::value_type(key, value)); 
} 

// Looks up a given key and value pair in a collection and inserts the key-value 
// pair if it's not already present. Returns a reference to the value associated 
// with the key. 
template <class Collection> 
typename Collection::value_type::second_type& LookupOrInsert(
    Collection* const collection, const typename Collection::value_type& vt) { 
    return collection->insert(vt).first->second; 
} 

// Same as above except the key-value are passed separately. 
template <class Collection> 
typename Collection::value_type::second_type& LookupOrInsert(
    Collection* const collection, 
    const typename Collection::value_type::first_type& key, 
    const typename Collection::value_type::second_type& value) { 
    return LookupOrInsert(collection, 
         typename Collection::value_type(key, value)); 
} 

} // namespace gtl 
} // namespace tensorflow 

#endif // TENSORFLOW_LIB_GTL_MAP_UTIL_H_ 


int main(int argc, char const *argv[]) 
{ 



} 

をどのように動作するかを理解したい

私は本当にこの部分がどのように機能するかを理解したいです。

// exists, or NULL otherwise. 
template <class Collection> 
const typename Collection::value_type::second_type* FindOrNull(
    const Collection& collection, 
    const typename Collection::value_type::first_type& key) { 
    typename Collection::const_iterator it = collection.find(key); 
    if (it == collection.end()) { 
    return 0; 
    } 
    return &it->second; 
} 

私はC++マップを使用しているが、私はまだこれを理解することはできません。 ここではキーワードテンプレートの使い方を見たことがありません。 私はこれがどのように機能するか見極めようと努力していますが、私の基本は分かりませんので、これを理解することはできません。これを使用した例と簡単な説明を教えてください。

+0

「ここでキーワードテンプレートがどのように使用されているかわかりません。」キーワード 'template'を他の用途に使用したことがありますか?あなたがここにいるのは、普通の、基本的な、毎日のキーワード 'テンプレート'の使用です。 C++であなたの好きな本を入手し、テンプレートについて読んでください。 – AnT

+0

申し訳ありませんが、私は最近C++を学び始めました。 – ArafatK

答えて

2

(私は実際の型ではないかもしれないintstd::stringconst_iteratorといくつかの複雑なタイプを()に置き換えてきた)ことを打破するのをしてみましょう:

template <class Collection> 
const int FindOrNull(
    const Collection& collection, 
    const std::string& key) { 
    const_iterator it = collection.find(key); 

    if (it == collection.end()) 
    return 0; 

    return &it->second; 
} 

だからここにあなたが行く - 取る単純な関数を2つのパラメータを返し、整数を返します。 template <class Collection>は、FindOrNullテンプレートの機能であることを意味し、必要なメソッド/プロパティ(例:Collection::find)を持たない限り、任意のタイプのパラメータCollectionを取ることができます。


テンプレート化機能の簡単な例:二つの整数、double秒、float秒、std::string秒、どんなことができます。

template <typename T, typename Y> 
auto add(const T &a, const Y &b) -> decltype(a + b) { 
    return a + b; 
} 

この機能は...何かを 2が追加されます一緒に追加

+0

ありがとうございました。私はまず物事を理解しようとします。コメントにいくつかフォローアップの質問をしても大丈夫ですか? – ArafatK

+0

@ArafatK、彼らはこの質問に非常に密接に関連していて、これのサブセットと考えることができます。しかし、コメントの中で「特集した」質問をすることは奨励されていません。 – ForceBru

+0

私はコードを理解することができ、スコープ解決演算子がマップに対してここでどのように使われているかを理解することができます。私はちょうど質問があります。キーワードconstとtypenameの使い方をここで説明できますか? – ArafatK