C++ 指向存储在地图容器中的数据的指针

C++ 指向存储在地图容器中的数据的指针,c++,pointers,stl,map,C++,Pointers,Stl,Map,我有两张地图,其中一张包含数据 比如说 struct DATA { int A1; int A2; }; typedef map<int,DATA> DataList; DataList myData; struct数据{ int A1; int A2; }; typedef映射数据列表; 数据列表myData; 我还尝试了以下一些方法: typedef映射数据列表指针 表格 (注意:抄送:我真的不明白这句话: map是一个表。列列表和指向myData上存在的数据的指针

我有两张地图,其中一张包含数据

比如说

struct DATA {
  int A1;
  int A2;
};
typedef map<int,DATA> DataList;
DataList myData;
struct数据{
int A1;
int A2;
};
typedef映射数据列表;
数据列表myData;
我还尝试了以下一些方法:
typedef映射数据列表指针
表格

(注意:抄送:我真的不明白这句话:

map是一个表。列列表和指向
myData
上存在的数据的指针

或者上面的表格在做什么 )

但我想知道如何找到指向地图中存在的数据的指针。我怎么能做到?
谢谢herzl。

这将获得指向地图中数据值的指针(如果存在)

   DataList::iterator it = myData.find( 42 );
   DATA * pData = NULL;       
   if ( it != myData.end() ) {
          pData = &((*it).second);
   }

您可能需要2个重载:

DATA * lookup( DataList & theMap, int key )
{
   DataList::iterator iter = theMap.find( key );
   if( iter != theMap.end() )
   {
      return &iter->second;
   }
   else
     return NULL;
}

当然,这意味着复制代码。因此,他可能是const cast的一个很好的候选人

DATA * lookup( DataList & theMap, int key )
{
   return const_cast<DATA *>(lookup( const_cast<const DataList&>(theMap), key));
}
DATA*查找(DataList&theMap,int键)
{
返回const_cast(查找(const_cast(theMap),key));
}
您可以将其作为通用模板:

template< typename Key, typename Value >
const Value * mapValueLookup( const std::map<Key, Value>& theMap, Key key )
{
   typename std::map<Key, Value>::const_iterator iter = theMap.find(key);
   if( iter != theMap.end() )
   {
      return &iter->second;
   }
   else
     return NULL;
}

template< typename Key, typename Value >
Value * mapValueLookup( std::map<Key, Value>& theMap, Key key )
{
   return const_cast<Value*>( mapValueLookup
       ( const_cast<const std::map<Key, Value> &>(theMap),
       key ) );
}
模板
常量值*mapValueLookup(常量标准::映射和主题映射,键)
{
typename std::map::const_迭代器iter=theMap.find(键);
如果(iter!=theMap.end())
{
返回&iter->second;
}
其他的
返回NULL;
}
模板
值*mapValueLookup(标准::映射和主题映射,键)
{
返回常量转换(mapValueLookup
(施工图),
键);
}
根据您的定义(非常合适),正如前面指出的,您的映射是由
std::pair
元素组成的,它们以某种方式串在一起(红黑树,通常用于有序
std::map
)。迭代器为您提供该映射中的位置,其行为类似于指向元素的指针。特别是,取消引用有效的将使
std::pair
与您在
.first
中的密钥和数据在
.second
中配对。但是,您没有使用
nullptr
,而是使用
map.end()
作为“无有效内容”迭代器值。因此
map.find(key)
为您提供一个迭代器,该迭代器保持
对的位置。如果找到,则第一个
等于
key
,否则为迭代器值
map.end()

前面的文章提供了很好的解决方案。我认为最简单的通用解决方案如下所示:

// return a pointer to the map's mapped_type value associated with the key;
// if the key is not found, return nullptr.
template <class Map, typename Key>
auto mapLookup(Map&& map, Key&& key)
{
  auto iter = map.find(key);
  return iter != map.end() ? &iter->second : nullptr;
}
你能行

DATA* pointer1 = mapLookup(myData, 1);
assert (pointer1 != nullptr); // we expect to find myData
assert (pointer1 == &myData[1]); // it should be the same as indexed access
assert (pointer1->A1 == 2); // and should find the values we inserted
assert (pointer1->A2 == 3);

DATA* pointer2 = mapLookup(myData, 999); // a key we haven't used
assert (pointer2 == nullptr); // so we could not find it

遗憾的是,此功能在标准(非多重)贴图类型中无法立即使用。C++20添加了一个
bool contains(const Key&Key)const
方法,这很好,但是如果它返回了一个指向该值的指针(或者
std::optional>
?),它将允许相同的功能(您可以像
bool
一样使用
测试指针的空值)以及提供对数据的即时访问(如果数据在那里)。

您能给出一个具体的示例说明您想要做什么吗?我的数据存在于一个类中,我不想将其复制到其他作用域。我只想发送指向数据的表结构。@closer我认为其中有一个合理的问题,只是需要一些编辑。
DataList myData;
//...
myData[1] = DATA{ 2, 3 };
//...
DATA* pointer1 = mapLookup(myData, 1);
assert (pointer1 != nullptr); // we expect to find myData
assert (pointer1 == &myData[1]); // it should be the same as indexed access
assert (pointer1->A1 == 2); // and should find the values we inserted
assert (pointer1->A2 == 3);

DATA* pointer2 = mapLookup(myData, 999); // a key we haven't used
assert (pointer2 == nullptr); // so we could not find it