小编asm*_*mmo的帖子

为什么是'if(1 == 1 == 1 == 1 == 1);' TRUE 但'if(-1 == -1 == -1 == -1 == -1);' 错误的?

if (1 == 1 == 1 == 1 == 1)
    std::cout << "right";
Run Code Online (Sandbox Code Playgroud)

上面的代码显示“正确”。

if (-1 == -1)
    std::cout << "right";
Run Code Online (Sandbox Code Playgroud)

上面的代码也显示“正确”。

if (-1 == -1 == -1)
    std::cout << "right";
Run Code Online (Sandbox Code Playgroud)

上面的代码什么也没显示。(这是因为if statement我猜这不是真的?)

我想知道为什么会发生这种奇怪的事情。

因为-1等于-1并且无论我重复多少次(据我所知),该语句始终为真。

c++ if-statement short-circuiting

2
推荐指数
1
解决办法
195
查看次数

如何在不使用命名空间 chrono 的情况下初始化 std::chrono::duration 常量?

我想使用这个代码

static constexpr auto set_time = 1s;
Run Code Online (Sandbox Code Playgroud)

但我不想使用using namespace chrono;

错误 C3688 无效的文字后缀“s”;找不到文字运算符或文字运算符“operator”“s”的模式

c++ literals c++-chrono c++20

2
推荐指数
1
解决办法
2558
查看次数

C++:当用作逻辑值时如何返回对象的设置状态

我有一些可以设置或不设置的类:

Class obj;
Run Code Online (Sandbox Code Playgroud)

我希望它的值在逻辑中使用时返回是否已设置:

if ( obj )
    obj.Clear();

if ( !obj )
    obj.Set( "foo" );
Run Code Online (Sandbox Code Playgroud)

我想添加到 bool 的隐式转换,但我想知道 int 是否是必要的,或者是否有更好的方法来解决这个问题。

c++ oop operator-overloading c++11

2
推荐指数
1
解决办法
77
查看次数

decltype() 不适用于正在编译的类模板

我有以下代码片段有问题。我认为编译器无法推导出类型,因为values在编译之前不会知道元素的类型。那正确吗?

vector values{1, 2, 3, 4, 5, 6};
vector<decltype(values[0])> {values};
ostream_iterator<int> printer{cout," "};
copy(ints.crbegin(),ints.crend(),printer);
Run Code Online (Sandbox Code Playgroud)

c++ templates c++17

1
推荐指数
1
解决办法
45
查看次数

How can I use va_arg in a loop without knowing how many optional arguments there are in C++?

I want to write a function which takes at least two integer and returns the sum of all integer passed to the function:

int sumOfAtLeastTwoIntegers(int a, int b, ...){
   
    int sum = a+b;
    va_list ptr;
    va_start(ptr,b);
    for(){
        sum += va_arg(ptr, int)
    }

    va_end(ptr);
    return sum;
}
Run Code Online (Sandbox Code Playgroud)

I want to know how the expression in the for loop has to look like such that the loop continues until all optional arguments were added to the sum. How would I achieve this …

c++ templates variadic-functions variadic-templates

1
推荐指数
2
解决办法
116
查看次数

如何使用 remove_if 从向量中删除元素

我有以下代码:我在删除函数中遇到编译错误。我想从向量中删除元素与xinput 的值匹配的元素x

class A
{
   int x,y;
   public:
   init(int a, int b)
   {
     x = a; y= b;
   }
   int getX(){return x;}
}
class B
{
   public:
   void add (int a, int b)
   {
     A a1;
     a1.init(a,b);
     MyVector.push_back(a1);
   }
   void remove(int x)
   {
      MyVector.erase(remove_if(MyVector.begin(), MyVector.end(),                                         
                  [&vec](int x){return (vec.getX() == x);}), MyVector.end());

   }
   vector<A> MyVector;
}
Run Code Online (Sandbox Code Playgroud)

c++ algorithm iterator stl vector

1
推荐指数
1
解决办法
78
查看次数

向量的自动参考?

i以下代码不会改变for 循环中的内容:

class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        if (target == 0) {return vector<vector<int>>{{}};} 
        else if (!candidates.size() || target < 0) {return vector<vector<int>>();}
        else {
            vector<vector<int>> with = combinationSum(candidates, target - candidates[0]); 
            vector<int> new_vector(candidates.begin() + 1, candidates.end());
            vector<vector<int>> without = combinationSum(new_vector, target);
            for (auto i : with) {i.push_back(candidates[0]);}

            with.insert(with.end(), without.begin(), without.end());
            return with;
                                        
        }
    }
};

Run Code Online (Sandbox Code Playgroud)

但是,如果我将其更改为auto& i : with ...,它就会起作用。有理由吗?我认为只有当您使用指向的对象或者您不希望变量(在本例中i)在本地范围内更改时,引用才相关。

c++ iterator stl reference range-based-loop

1
推荐指数
1
解决办法
1131
查看次数

如何克隆其中包含结构项的向量(rust)?

如何克隆带有结构项的向量Rust

我已经尝试过.to_vec(),但似乎我不能,因为我正在使用结构。

struct Abc {
    id: u32,
    name: String
}

let mut vec1: Vec<Abc> = vec![];

let item1 = Abc {
    id: 1,
    name: String::from("AlgoQ")
}

vec1.push(item1)

let vec2 = vec1.to_vec();
Run Code Online (Sandbox Code Playgroud)

错误:

the trait bound `blabla::Abc: Clone` is not satisfied
the trait `Clone` is not implemented for `blabla::Abc`rustc(E0277)
Run Code Online (Sandbox Code Playgroud)

clone vector traits rust

1
推荐指数
1
解决办法
3877
查看次数

为什么 main 函数和其他函数中的数组大小不同?

我对 C++ 并不陌生,但今天我发现 main 函数和其他函数中数组的大小不同。这是为什么?我想这与指针有关。

#include<bits/stdc++.h>
using namespace std;

void func(int arr[]){
    cout<<"func size: "<<sizeof(arr)<<"\n";
}

int main(){
    int arr[5];
    cout<<sizeof(arr)<<"\n";
    func(arr);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

您可以测试此代码以查看差异。

c++ arrays function

0
推荐指数
1
解决办法
139
查看次数

我可以创建一个只能动态分配的结构吗?

在 C++ 中,我可以创建一个只能动态分配的结构吗?我想强制执行一种方法来确保程序员不能静态分配这个结构,因为这样做会导致悬空指针,因为我需要在其范围之外使用指针的内容。

c++ oop struct pointers dangling-pointer

-1
推荐指数
1
解决办法
80
查看次数

有没有办法在 C++ 中显式调用可选参数?

当我创建一个函数时,例如:

int addThree(int x=1, int y=1, int z=1)

我想调用该函数,使其使用 x 和 z 的默认参数,而不是 y。

一些尝试已经addThree(5,,5)addThree(5,NULL,5),但都没有有效地工作。

c++ function optional-parameters optional-arguments

-1
推荐指数
1
解决办法
85
查看次数