【问题标题】:Is there a NULL equivalent for pairs in C++?C++ 中的对是否有 NULL 等价物?
【发布时间】:2015-09-24 10:54:07
【问题描述】:

如果我在 C++ 中有一个未分配的对,我想用什么来代替 NULL?

例如,假设我有如下(伪)代码:

pair<int,int> bestPair; //Global variable

updateBestPair(vector<int> a, vector<int> b) {

    bestPair = NULL;

    for (/* loop through a and b */) {
        if (/* pair(a,b) is better than bestPair and better than some baseline */)
            bestPair = make_pair(a,b);
    }

    if (bestPair != NULL) //Found an acceptable best pair
        function(bestPair);
    else
        cout<<"No acceptable pairs found"<<endl;
}

【问题讨论】:

    标签: c++ std-pair


    【解决方案1】:

    C++ 中是否有 NULL 等价物?

    没有。

    如果我在 C++ 中有一个未分配的对,我想用什么来代替 NULL?

    这里有几个选项:

    • 可以使用指向pair的指针,可以设置为NULL;这可能不是最好的解决方案(因为您显然不需要指针)

    • 您可以使用boost::optional&lt;std::pair&lt;int,int&gt;&gt;

    • 您可以(并且可能应该)重写代码以不使用全局变量。

    • 您可以重组您的控制流程以避免将检查有效对作为单独的步骤:

      pair<int,int> bestPair; //Global variable
      
      updateBestPair(vector<int> a, vector<int> b) {
      
          // not needed
          // bestPair = NULL;
      
          //loop through a and b
          if (/* pair(a,b) is better than bestPair and ... */)
          {
              bestPair = make_pair(a,b);
              function(bestPair);
          }
          else
              cout<<"No acceptable pairs found"<<endl;
      }
      
    • 您可以选择一个人为的值来表示“无效对值”:

      // use as constant, wherever you used NULL before
      const auto invalid_pair = std::make_pair(
          std::numeric_limits<int>::max(),
          std::numeric_limits<int>::max());
      
    • 您可以使用布尔标志:

      pair<int,int> bestPair; //Global variable
      
      updateBestPair(vector<int> a, vector<int> b) {
      
          bool initialized = false;
      
          //loop through a and b
          if (/* pair(a,b) is better than bestPair and ... */)
          {
              bestPair = make_pair(a,b);
              initialized = true;
          }
      
          if(initialized)
              function(bestPair);
          else
              cout<<"No acceptable pairs found"<<endl;
      }
      
    • 您可以使用自定义解决方案(是否类似于 boost::optional wrapper)

    【讨论】:

    • 您的第一个代码建议使bestPair 处于“无效状态”,没有表示该事实的变量。鉴于bestPair 是一个全局变量,它很可能在其他地方使用;因此,这个建议不太可能是一种可行的方法。其余的都很好:)
    • 我假设 bestPair 在调用 updateBestPair 之前有一个有效值,因为函数被命名为 updateBestPair,而不是 initBestPair (我的错 - 我没有理由假设)。
    • 谢谢,这些都是好主意!我认为使用 boost::optional 将是最干净的方法,并且使用布尔标志将是最简单的(尽管以后也最容易搞砸)。我不能对这个特定的应用程序使用人工值,因为任何对值都是可以接受的,尽管这是我通常喜欢的方法。是的,不使用全局变量是最好的,但在这种情况下,这将需要重写整个框架,所以每当我进行重大重组时,它就会出现在待办事项列表中!
    • @utnapistim:我想这很公平,但我希望一个名为 updateBestPair 的函数没有更新 bestPair 以某种方式告诉我:P 而我没有不仅仅意味着控制台消息。
    • @user1634426,boost::optional 的实现实际上是给定类型的自动存储变量的存储,以及一个 bool 标志来指定该值是否已初始化(以及保留它们的代码同步)。
    【解决方案2】:

    没有。 C++ 对象不能被“NULLed”。

    (即使是对象的指针也不能“NULL”!这很令人困惑,因为它们的值可能被设置为空指针值,过去我们有时使用名为 NULL 的宏获得;但是,这与将指针本身“归零”不同。呃,无论如何……)

    我推荐boost::optional,或者重新考虑拥有一个可以“具有有用价值”或“没有有用价值”的全局变量的想法。如果它没有有用的价值,它存在的意义何在?

    【讨论】:

    • 哇,boost::optional 包看起来正是我需要的!谢谢!至于为什么我首先需要这样的全局变量——我正在做一些粒子物理分析,我需要读取大量的碰撞事件。有时我会找到一个相关的电子碰撞参数,有时我会遇到没有足够好的电子的碰撞。但是,在第二种情况下,我不能只参加下一个活动。我还得做分析,只是在我没有找到好的电子对的地方做标记。
    • @user1634426:好的。那么,在过去,我会按照马吕斯的建议去做; boost::optional 是实现这一目标的更快/原子方式。
    【解决方案3】:

    不,这是不可能的。您可以使用附加变量来指示配对的有效性(即您有配对)。

    【讨论】:

    • 谢谢,这听起来是个好主意。我更喜欢不必为一个目的使用两个变量的方法(这两个变量很容易失去同步),但我想有时这是最好的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    相关资源
    最近更新 更多