- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有三个 LPCWSTR
字符串变量,分别称为 A
、B
、C
。
我从另一个函数分配它们,如果出现问题,有时会返回 nullptr
。像这样:
A = MyFunc();
B = MyFunc();
C = MyFunc();
现在,对于带有这些变量的一些东西,我需要检查这些变量中是否只有一个不是 nullptr
(只分配了一个变量)。
我自己尝试这样做:
if ((A == nullptr) && (B == nullptr) && (C <> nullptr)) {}
欢迎任何关于如何做到这一点的想法。
最佳答案
很容易做到:
int numSet = 0;
A = MyFunc(); if (A != nullptr) numSet++;
B = MyFunc(); if (B != nullptr) numSet++;
C = MyFunc(); if (C != nullptr) numSet++;
if (numSet == 1) // only one is set
您还可以使用辅助函数封装该行为:
LPCWSTR MyFuncWithCount(int &countSetProperly) {
LPCWSTR retVal = MyFunc();
if (retVal != nullptr) countSetProperly++;
return retVal;
}
int numSet = 0;
A = MyFuncWithCount(numSet);
B = MyFuncWithCount(numSet);
C = MyFuncWithCount(numSet);
if (numSet == 1) // only one is set
下一步将使用 基于范围的 for 循环和 花括号初始化列表,按照以下完整程序:
#include <iostream>
#include <vector>
typedef void * LPCWSTR; // Couldn't be bothered including Windows stuff :-)
int main() {
// Only set two for test purposes.
LPCWSTR A = nullptr, B = nullptr, C = nullptr;
LPCWSTR D = &A, E = nullptr, F = &A;
int numSet = 0;
for (const auto &pointer: {A, B, C, D, E, F})
if (pointer != nullptr)
numSet++;
std::cout << "Count is " << numSet << std::endl;
}
或者您可以通过使用 lambda 函数来拥抱现代 C++ 的所有荣耀,如下所示:
#include <iostream>
#include <vector>
typedef void * LPCWSTR; // Couldn't be bothered including Windows stuff :-)
int main() {
// Only set two for test purposes.
LPCWSTR A = nullptr, B = nullptr, C = nullptr;
LPCWSTR D = &A, E = nullptr, F = &A;
int numSet = 0;
[&numSet](const std::vector<LPCWSTR> &pointers) {
for (const auto &pointer: pointers)
if (pointer != nullptr)
numSet++;
} (std::vector<LPCWSTR>{A,B,C,D,E,F});
std::cout << "Count is " << numSet << std::endl;
}
对于您的特定情况,这可能有点矫枉过正:-)
关于c++ - 检查 C++ 中是否只有一个字符串变量不是 nullptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45498201/
我正在尝试为我的学校作业制作信息亭程序。当我编译它时,它不会发出任何错误信号。但是当我尝试初始化我的队列列表时,它说我不能使用 nullptr...我不明白我的代码有什么问题。 typedef str
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭 4 年前。 Improve
这个问题在这里已经有了答案: Placement of the asterisk in pointer declarations (14 个答案) 关闭 4 年前。 在博客和论坛上,我注意到人们使用
为什么std::string str(nullptr)和 std::string str=nullptr是错的?有人可以详细解释原因吗? 最佳答案 这样的例子编译是因为存在一个 std::string
这个定义有效: const auto &b{nullptr}; 虽然失败: auto *b{nullptr}; 我尝试在 Visual C++、GCC 和 Clang 中编译它。他们都提示“无法推断类
刚刚看了一篇关于C++0x标准的文章:http://www.softwarequalityconnection.com/2011/06/the-biggest-changes-in-c11-and-w
我已将节点指针初始化为 nullptr 并将其作为引用传递给辅助函数。在辅助函数内,我将之前为 nullptr 的指针设置为等于 new Pointer。然而,函数结束后,它又被设置为nullptr。
有谁知道为什么分配type* = 0无效,而分配type* = nullptr无效呢?在这两种情况下typedef void type。谢谢 #include #include template
我正在尝试为类(class)项目创建链表。我的节点类有一个指向链接节点的指针和另一个指向专门书籍类的指针。 class Node{ private: Book *data; Node
我在destroyStudent()函数中删除指针aStudent,然后我将aStudent设置为空指针。但是,运行该函数后,aStudent 不再设置为nullptr,所以我必须再次将其设置为nul
我正在尝试制作一个基本的 HashMap。在将元素插入索引之前,我正在检查它是否存在于索引中。当我插入第一个元素时,它说该位置已经存在一个元素。我已经通过调试器,我的所有值都符合预期,map[hash
我正在尝试做类似的事情: if (foo) return SET_ERROR_AND_RETURN_NULL(ERROR_HERE); 使用... #define SET_ERROR_AND
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
我有一个看起来像这样的结构: struct Wolf { Wolf *dog; Wolf *puppy; }; 我写过: Wolf *alphawolf = new Wolf; 当我尝
我正在使用带有 Visual Studio 2012 的 C++(11)。 我使用定制的包装器类创建窗口。 CUIWindow* winA = new CUIWindow ( NULL, TEXT("
我为它编写了一个 PriorityQueue 类和一个迭代器类。但我不明白为什么这些行编译? PriorityQueue pq; auto z =pq.begin(); z=nullptr
我正在学习 enable_if 的用法我偶然发现了以下代码。 template ::value, T>::type* = nullpt
我有一个函数func(int a, char b, void *ptr)。第三个参数保留供内部使用,它应该是当前版本的 nullptr。有没有办法在函数原型(prototype)而不是定义中强制执行此
所以当我在学校编译它时 nullptr 错误没有出现,我想我可以通过在编译时添加一行来修复它,有没有另一种方法来摆脱它,另外两个错误我不明白为什么我要得到它们。有人可以至少解释一下 nullptr 错
这个问题在这里已经有了答案: Checking if this is null (8 个答案) 关闭 4 年前。 我想像这样替换代码: if ((obj != nullptr) && obj->is
我是一名优秀的程序员,十分优秀!