gpt4 book ai didi

c++ - 了解来自 tensorflow c++ API 的代码

转载 作者:行者123 更新时间:2023-11-28 02:06:09 24 4
gpt4 key购买 nike

我是 c++ 的新手,我试图了解这段代码的工作原理,我能够理解 includeguard 的工作原理。以及如何使用嵌套命名空间 gtl 和 tensorflow。

我想了解这段代码是如何工作的

// 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++ map,但我仍然无法理解这一点。我从未见过这里如何使用关键字模板。我非常努力地想看看它是如何工作的,但也许我的基础知识还不清楚,所以我无法理解。能否请您举一个使用它的例子和一个简短的解释?

最佳答案

让我们分解一下(我用 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;
}

现在开始 - 一个简单的函数,它接受两个参数并返回一个整数。那template <class Collection>意味着 FindOrNull是一个模板 函数,它可以接受(任何)类型的参数 Collection除非它没有一些必需的方法/属性(例如 Collection::find )。


模板化函数的一个简单示例:

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

这个函数添加两个...东西:两个整数,double小号,float小号,std::string s,无论什么可以加在一起

关于c++ - 了解来自 tensorflow c++ API 的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37365914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com