我在 C++ 中尝试了今天的 leetcode 挑战。你必须在二叉树中找到表亲。这是我的代码。
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
class Solution {
public:
bool isCousins(TreeNode* root, int x, int y) {
this->x = x;
this->y = y;
return visit(overloaded{
[](const bool& r) {
return r;
},
[](const optional<int>& r) {
return false;
}
}, helper(root));
}
private:
int x;
int y;
variant<const bool, optional<int>> helper(TreeNode* node) {
if (node == nullptr) {
return variant<const bool, optional<int>>((optional<int>()));
} …Run Code Online (Sandbox Code Playgroud) 我能找到的每个参考资料都表明我可以使用 char* cName = "Some Text",但我的 VS2019 抱怨。
我在 Stackoverflow 上看到了很多使用这种语法的例子,很多 youtube 视频也演示了它的用法。
谁能解释为什么我看到我的错误(见下图)
我知道我们在 C 中以这种方式更改 const 变量值(通过将其更改为非常量)。
#include<stdio.h>
#include<stdlib.h>
int main()
{
const int var = 10;
int *ptr = &var;
*ptr = 5;
printf("var = %d\n", var);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C++ 中的类似事情(使用指针)给了我一个编译错误
如何更新 C++ 中的 const 变量值?
我遇到了一个问题,而试图分配struct与const&成员的地图。
struct test {
const int& number;
test(const int& cnumber) : number(cnumber) {}
test(const test&) = default;
test& operator=(const test&) = default;
};
Run Code Online (Sandbox Code Playgroud)
int main () {
std::map<std::string, test> testmap;
testmap["asd"] = test(2);
}
Run Code Online (Sandbox Code Playgroud)
运行此代码会导致错误 C2280 'test &test::operator =(const test &)': attempting to reference a deleted function
谁能向我解释这里的问题是什么?这是一个最小的可重现示例。在我的实际项目中,数据要大得多,因此按值传递是不明智的。
我想用 实现构造函数std::initializer_list,我试过:
#include <initializer_list>
template <class T>
class Vec
{
public:
typedef T *iterator;
typedef T *const const_iterator;
typedef T value_type;
Vec(std::initializer_list<T> init)
{
create(init);
}
...
private:
iterator data;
iterator avail;
iterator limit;
void create(std::initializer_list<T> init);
...
}
template<class T> void Vec<T>::create(std::initializer_list<T> init)
{
data = init.begin();
limit = avail = init.end();
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用它时:
#include "vec2.hpp"
int main()
{
Vec<int> v({ 1, 2, 3, 4 });
}
Run Code Online (Sandbox Code Playgroud)
我很惊讶std::initializer_list::begin()和end()迭代器是const iterator,不可写iterator(就像其他所有容器一样, …
我是 C++ 的新手,遇到了const关键字。我在网上查了一下,读到主要用途是防止在整个程序中更改值。我在下面有这两个片段
const int size = 56;
Run Code Online (Sandbox Code Playgroud)
与使用相比
int size = 56;
Run Code Online (Sandbox Code Playgroud)
为什么我应该使用一个?使用它背后的想法是什么。
我想知道,C 编译器(即 GCC 或 GHS)是否有一个标志来检查输入参数是否被修改?
我的意思是,如果我有以下功能,x并且y如果我不将const它们添加到它们中,则可能会在该功能内进行修改(就像现在发生的那样)。所以我想知道编译器是否可以检测到这种情况,以及是否存在这样做的标志。
int16_t myFunc(int16_t* x ,int16_t* y, bool* e)
{
int16_t z = 0;
z = *x + *y;
*x = 16; // this is wrong on purpose and I would like to detect it
if ( z < 0)
{
*e = true;
}
return z;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个结构(或类,无关紧要),其构造函数将值分配给结构的常量参数。即我不想在创建 Point 对象后更改它的任何变量。
以下代码显然不起作用,因为构造函数试图更改常量的值。
struct point
{
const int x;
const int y;
point(int _x = 0, int _y = 0)
{
x = _x;
y = _y;
}
};
point myPoint = point(5, 10);
std::cout << myPoint.x << myPoint.y << std::endl;
Run Code Online (Sandbox Code Playgroud) 我Point在头文件中定义了一个结构类,如下所示 -
namespace global_planner {
class GlobalPlanner : public nav_core::BaseGlobalPlanner {
struct Point {
__uint32_t x, y;
bool operator==(const Point &p1 ) {
return ((p1.x == x) && (p1.y == y));
}
bool operator<(const Point &p1 ) const {
return ((p1.x < x) || (p1.x == x && p1.y < y) ) ;
}
};
public:
///
private:
////
};
};
Run Code Online (Sandbox Code Playgroud)
在我的源文件(名为global_planner.cpp)中,我有一个名为的函数,generate_straight_path定义如下 -
bool GlobalPlanner::generate_straight_path(const Point &p1, const Point &p2){
if(costmap_ros_->getCost(p1.x, p1.y) == costmap_2d::LETHAL_OBSTACLE) …Run Code Online (Sandbox Code Playgroud) 当您要创建类的新 const 实例时,是否需要定义 const 函数?
不知何故,当我尝试从 const 类实例访问它们时,我的编译器找不到“常规”(不是 const)函数。