std::map 丢失了我的数据?

发布于 2024-10-21 14:32:04 字数 1862 浏览 11 评论 0原文

我将一些用 boost::any 包装的数据粘贴到类方法 getValue 中的地图中。一切正常,数据在 getValue 中适当的地图中等。当我离开 getValue 并尝试使用此数据时,它不再位于地图中。

这让我很困惑,我可能忘记在关键位置使用参考,但我找不到它。

相关代码如下:

test.c //我们不想实际记录计时器,如果用户愿意,他可以这样做。 时间值 tmp;

  tmp.tv_sec = 0;
  tmp.tv_usec = 0;

  gettimeofday(&tmp, NULL);
  getValue<timeval>(timerName) = tmp;
  std::cout << tmp.tv_usec << " : " << getSingleton().globalValues.count(key) << std::endl; //Count returns 0 here, for a given key X_X

测试.h

 /* Grab the value of type T found while parsing.  Should use checkValue first.*/
  template<typename T>
  static T& getValue(const char* identifier) {
    //Used to ensure we have a valid value
    T tmp;
    //Used to index into the globalValues map
    std::string key = std::string(identifier);
    std::map<std::string, boost::any>& gmap = getSingleton().globalValues;

    if(checkValue(identifier)) //If we have the option, set it's value
      tmp = getSingleton().vmap[identifier].as<T>();  //vmap is correct, it specifies default values passed in via command line.  

    //We may have whatever is on the commandline, but what if
    //The programmer has made modifications?
    if(!gmap.count(key))  //The programmer hasn't done anything, lets register it then
      gmap[key] = boost::any(tmp);

    std::cout << "gmap " << key << std::endl;        
    std::cout << getSingleton().globalValues.count(key) << std::endl; //count returns 1 here, for a given key.
    return boost::any_cast<T&>(gmap[key]);
  }

...

测试.h

 //Map of global values, stored here instead of in OptionsHierarchy
  //For ease of implementation
  std::map<std::string, boost::any> globalValues;

I'm sticking some data, wrapped with boost::any into a map in a class method getValue. Everything works fine, the data is in the map as appropriate etc etc in getValue. The second I leave getValue and try to use this data, it's no longer in the map.

This has me stumped, I'm probably forgetting to use a reference at a key spot, but I can't find it.

The relevant code is as follows:

test.c
//We don't want to actually document the timer, the user can do that if he wants to.
timeval tmp;

  tmp.tv_sec = 0;
  tmp.tv_usec = 0;

  gettimeofday(&tmp, NULL);
  getValue<timeval>(timerName) = tmp;
  std::cout << tmp.tv_usec << " : " << getSingleton().globalValues.count(key) << std::endl; //Count returns 0 here, for a given key X_X

test.h

 /* Grab the value of type T found while parsing.  Should use checkValue first.*/
  template<typename T>
  static T& getValue(const char* identifier) {
    //Used to ensure we have a valid value
    T tmp;
    //Used to index into the globalValues map
    std::string key = std::string(identifier);
    std::map<std::string, boost::any>& gmap = getSingleton().globalValues;

    if(checkValue(identifier)) //If we have the option, set it's value
      tmp = getSingleton().vmap[identifier].as<T>();  //vmap is correct, it specifies default values passed in via command line.  

    //We may have whatever is on the commandline, but what if
    //The programmer has made modifications?
    if(!gmap.count(key))  //The programmer hasn't done anything, lets register it then
      gmap[key] = boost::any(tmp);

    std::cout << "gmap " << key << std::endl;        
    std::cout << getSingleton().globalValues.count(key) << std::endl; //count returns 1 here, for a given key.
    return boost::any_cast<T&>(gmap[key]);
  }

...

test.h

 //Map of global values, stored here instead of in OptionsHierarchy
  //For ease of implementation
  std::map<std::string, boost::any> globalValues;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杯敬自由 2024-10-28 14:32:04

在这些行中,您使用 vmap 而不是 gmap。是这样吗?

if(checkValue(identifier)) //If we have the option, set it's value
  tmp = getSingleton().vmap[identifier].as<T>();

我还注意到您正在使用 count 来检查某个项目是否存在。一般来说,除非您确实需要计数,否则这将导致比 find 做更多的工作,尽管对于 std::map 来说,它可能足够智能来优化 count< /code> 进入查找。由于您希望在它不存在时进行插入,而在它存在时不执行任何操作,因此只需使用 insert 即可,因为这正是 insert 将为您提供的行为。

On these lines you use vmap instead of gmap. Is that right?

if(checkValue(identifier)) //If we have the option, set it's value
  tmp = getSingleton().vmap[identifier].as<T>();

Also I noticed that you're using count to check for existence of an item. Generally speaking unless you actually need the count, that will result in doing more work than find although for std::map it may be smart enough to optimize count into find. Since you want to insert if it doesn't exist and no-op when it does, just use insert as that's exactly the behavior insert will give you.

苄①跕圉湢 2024-10-28 14:32:04

我修复了这个问题,注意我如何将 getValue 中的 key 定义为 std::string(identifier)。在 test.c 中,我将键定义为 sanitizeString(..) ,显然它返回了不同的键。当然,你们看不到,所以我的错。

I fixed this, note how I define key in getValue as being std::string(identifier). In test.c I was defining key as sanitizeString(..) which returned a different key, obviously. Of course you guys couldn't see that so my bad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文