从tensorflow c+理解代码+;美国石油学会 我是C++新手,我想了解这段代码是如何工作的,我能理解Cebug防御程序是如何工作的。以及如何使用嵌套命名空间gtl和tensorflow
我想了解这段代码是如何工作的从tensorflow c+理解代码+;美国石油学会 我是C++新手,我想了解这段代码是如何工作的,我能理解Cebug防御程序是如何工作的。以及如何使用嵌套命名空间gtl和tensorflow,c++,tensorflow,C++,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
// 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[])
{
}
//此文件提供用于STL映射类数据的实用程序函数
//结构,例如std::map和hash_map。有些函数也可以与
//集合,例如ContainsKey()。
#ifndef TENSORFLOW_LIB_GTL_MAP_UTIL_H_
#定义TENSORFLOW_LIB_GTL_MAP_UTIL_H_
#包括
#包括
#包括
#包括
#包括
名称空间张量流{
名称空间gtl{
//返回与给定键关联的常量值的指针,如果
//存在,否则为空。
模板
常量typename集合::值类型::第二个类型*FindOrNull(
常量集合和集合,
常量typename集合::值类型::第一个类型和键){
typename集合::常量迭代器it=Collection.find(键);
if(it==collection.end()){
返回0;
}
返回&it->second;
}
//同上,但返回指向非常量值的指针。
模板
typename集合::值\类型::第二个\类型*FindOrNull(
集合&Collection,//NOLINT
常量typename集合::值类型::第一个类型和键){
typename集合::迭代器it=Collection.find(键);
if(it==collection.end()){
返回0;
}
返回&it->second;
}
//返回与给定键关联的指针值。如果找不到,
//返回NULL。该函数设计用于将密钥映射到
//指针。
//
//此函数不区分缺少的密钥和映射的密钥
//设置为空值。
模板
typename集合::值类型::第二种类型FindPtrOrNull(
常量集合和集合,
常量typename集合::值类型::第一个类型和键){
typename集合::常量迭代器it=Collection.find(键);
if(it==collection.end()){
返回typename集合::值类型::第二个类型();
}
返回->秒;
}
//返回与给定键关联的值的常量引用,如果
//存在,否则返回对提供的默认值的常量引用。
//
//警告:如果临时对象作为默认“值”传递
//此函数将返回对该临时对象的引用,
//将在声明末尾销毁。一个常见的
//示例:如果您有一个带有字符串值的映射,并且您传递了一个char*
//作为默认“值”,请立即使用返回的值
//或者将其存储在字符串中(不是字符串&)。
模板
const typename集合::值类型::第二类型&FindWithDefault(
常量集合和集合,
常量typename集合::值类型::第一个类型和键,
常量类型名称集合::值类型::第二个类型和值){
typename集合::常量迭代器it=Collection.find(键);
if(it==collection.end()){
返回值;
}
返回->秒;
}
//当且仅当
//给定的键在集合中不存在。如果该键以前
//集合中存在,该值未更改。如果
//已插入键值对;如果该键已存在,则返回false。
模板
bool InsertIfNotPresent(集合*常量集合,
常量typename集合::值(类型和vt){
返回集合->插入(vt)。第二;
}
//与上面相同,只是键和值是分开传递的。
模板
布尔插入不存在(
集合*常量集合,
常量typename集合::值类型::第一个类型和键,
常量类型名称集合::值类型::第二个类型和值){
返回InsertIfNotPresent(集合,
typename集合::值\类型(键,值));
}
//在集合中查找给定的键值对并插入键值
//如果尚未存在,则进行配对。返回对关联值的引用
//用钥匙。
模板
typename集合::值\类型::第二个\类型和LookupOrInsert(
集合*const集合,const typename集合::value\u type&vt){
返回集合->插入(vt).第一次->第二次;
}
//与上面相同,只是键值是单独传递的。
模板
typename集合::值\类型::第二个\类型和LookupOrInsert(
集合*常量集合,
常量typename集合::值类型::第一个类型和键,
常量类型名称集合::值类型::第二个类型和值){
返回LookupOrInsert(集合,
typename集合::值\类型(键,值));
}
}//名称空间gtl
}//命名空间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;
}
//存在,否则为NULL。
模板
常量typename集合::值类型::第二个类型*FindOrNull(
常量集合和集合,
常量typename集合::值类型::第一个类型和键){
typename集合::常量迭代器it=Collection.find(键);
if(it==collection.end()){
返回0;
}
返回&it->second;
}
<>我使用了C++地图,但我还是不能理解。
我从未见过这里如何使用关键字模板。 我一直在努力想知道这是怎么回事,但可能我的基本知识不清楚,所以我无法理解这一点。你能给出一个例子和一个简短的解释吗?让我们把它分解一下(我用
int
、std::string
和常量迭代器
(可能不是实际的类型)替换了一些复杂的类型):
此函数添加了两个。。。一些东西:两个整数,double
s,float
s,std::string
s,可以加在一起的任何东西。让我们把它分解一下(我用int
替换了一些复杂的类型,std::string
和const\u迭代器
(可能不是实际的类型)):
这个功能
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 <typename T, typename Y>
auto add(const T &a, const Y &b) -> decltype(a + b) {
return a + b;
}