小编NO_*_*AME的帖子

从矢量中删除元素

我想使用erase方法从向量中清除元素.但是这里的问题是元素不能保证在向量中只出现一次.它可能存在多次,我需要清除所有这些.我的代码是这样的:

void erase(std::vector<int>& myNumbers_in, int number_in)
{
    std::vector<int>::iterator iter = myNumbers_in.begin();
    std::vector<int>::iterator endIter = myNumbers_in.end();
    for(; iter != endIter; ++iter)
    {
        if(*iter == number_in)
        {
            myNumbers_in.erase(iter);
        }
    }
}

int main(int argc, char* argv[])
{
    std::vector<int> myNmbers;
    for(int i = 0; i < 2; ++i)
    {
        myNmbers.push_back(i);
        myNmbers.push_back(i);
    }

    erase(myNmbers, 1);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这段代码显然崩溃了,因为我在迭代它时改变了向量的末尾.实现这一目标的最佳方法是什么?也就是说有没有办法做到这一点,而无需多次迭代矢量或创建一个矢量的副本?

c++ stl vector erase

94
推荐指数
3
解决办法
13万
查看次数

intmax_t 和 uintmax_t 是否保证大小相同?

我需要知道是否intmax_t始终“相同类型”,除非uintmax_t使用二进制补码而不是无符号值。

或者用正式的术语来说,下面的代码是否总是在符合标准的编译器中编译?

#include <cstdint>

// The important assertion:
static_assert(sizeof(std::uintmax_t) == sizeof(std::intmax_t));
// Less important assertions:
static_assert(UINTMAX_MAX == static_cast<std::uintmax_t>(INTMAX_MAX) * 2 + 1);
static_assert(-static_cast<std::intmax_t>(UINTMAX_MAX / 2) - 1 == INTMAX_MIN);
Run Code Online (Sandbox Code Playgroud)

我对 C++17 特别感兴趣。

我知道 C++20 是强制执行二进制补码的标准的第一个版本,但变量的大小对我来说比表示形式更重要。

c++ language-lawyer c++17

27
推荐指数
2
解决办法
2064
查看次数

将主机名解析为IP地址的便携方式

我需要在shell脚本中将主机名解析为IP地址.代码必须至少工作中Cygwin,UbuntuOpenWrt(busybox).可以假设每个主机只有一个IP地址.

例:

编辑: nslookup似乎是一个很好的解决方案,但它的输出是非常不可预测的,很难过滤.这是我的计算机上的结果命令(Cygwin):

>nslookup google.com
Unauthorized answer:
Serwer:  UnKnown
Address:  fdc9:d7b9:6c62::1

Name:   google.com
Addresses:  2a00:1450:401b:800::200e
          216.58.209.78
Run Code Online (Sandbox Code Playgroud)

dns shell sh

10
推荐指数
2
解决办法
5726
查看次数

约束允许在函数参数上使用static_assert吗?

当前,即使对它的所有调用确实为,也无法用于static_assert验证constexpr函数的参数constexpr。这是有道理的,因为在某些其他模块尝试调用该函数的情况下,编译器仍必须为此函数创建一个非constexpr实例。可悲的是,即使函数是static或位于匿名名称空间中,也是如此。

然而,C ++ 20将引入一个新的关键字consteval,类似于constexpr但不允许以非constexpr的方式调用函数。在这种情况下,编译器可以确定在编译时始终知道函数参数。因此,从理论上讲,应该有可能使用进行验证static_assert

问题是:标准允许吗?


例:

#include <iostream>

consteval char operator""_bchar(const char text[], const size_t length)
{
    static_assert(length == 8, "Binary char has to have 8 digits!"); // <-- This is currently not possible.
    uint8_t byte = 0;
    for (size_t i = 0; i != length; ++i)
    {
        byte <<= 1;
        byte |= text[i] == '1' ? 0b00000001 : 0b00000000;
    }
    return byte;
}

int main() …
Run Code Online (Sandbox Code Playgroud)

c++ static-assert c++20 consteval

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

Java 8 - 在lambda中抛出多个泛型检查异常

在我正在工作的项目中,我找到了一个类,它在一些精心设计的异常处理中包装了它的超类的所有方法.它看起来类似于:

public void method1() throws ExceptionA {
    String exceptionString = "";
    try {
        super.method1();
    } catch (ExceptionA e) {
         exceptionString = // <convert the exception to string in an elaborate way>
         throw e;
    } finally {
         // <an elaborate logger call which uses value of exceptionString>
    }
}

public void method2() throws ExceptionB, ExceptionC {
    String exceptionString = "";
    try {
        super.method2();
    } catch (ExceptionB | ExceptionC e) {
         exceptionString = // <convert the exception to string in elaborate way> …
Run Code Online (Sandbox Code Playgroud)

java lambda exception-handling java-8

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

在constexpr构造函数中复制数组

我用constexpr复制构造函数编写了一个类.(这是一个结构,使其更简单.)其中一个字段是一个数组.我也要复制它.

struct Foo
{
    static constexpr int SIZE = 4;
    constexpr Foo() = default;
    constexpr Foo(const Foo &foo) :
            arr{foo.arr[0], foo.arr[1], foo.arr[2], foo.arr[3]},
            bar(foo.bar+1) {}
    int arr[SIZE] = {0, 0, 0, 0};
    int bar = 0;
};
Run Code Online (Sandbox Code Playgroud)

我的版本有效,但不可扩展.如果我改变SIZE,我必须修改构造函数.另外,代码看起来很难看.

是否有更好的方法在构造函数中复制数组?构造函数必须是constexpr.

c++ arrays copy-constructor constexpr c++11

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

让编译器假设所有情况都在switch中处理而没有默认值

让我们从一些代码开始吧.这是我程序的极简化版本.

#include <stdint.h>

volatile uint16_t dummyColorRecepient;

void updateColor(const uint8_t iteration)
{
    uint16_t colorData;
    switch(iteration)
    {
    case 0:
        colorData = 123;
        break;
    case 1:
        colorData = 234;
        break;
    case 2:
        colorData = 345;
        break;
    }
    dummyColorRecepient = colorData;
}

// dummy main function
int main()
{
    uint8_t iteration = 0;
    while (true)
    {
        updateColor(iteration);
        if (++iteration == 3)
            iteration = 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

该程序编译警告:

./test.cpp: In function ‘void updateColor(uint8_t)’:
./test.cpp:20:25: warning: ‘colorData’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     dummyColorRecepient …
Run Code Online (Sandbox Code Playgroud)

c++ g++ switch-statement

6
推荐指数
2
解决办法
210
查看次数

如果应用了Java插件,Gradle将无法在合成版本中找到zip工件

我有一个创建zip工件的Gradle项目。我通过定义了工件artifacts.add('default', zipTask)。我通过将该项目添加到另一个项目,includeBuild并使用zip作为依赖项(dependencies { myConfiguration 'org.example:testA:+@zip' })。到目前为止,一切都很好。有用。

当我将插件添加java到第一个项目时,问题开始。由于某些原因,它阻止Gradle查找zip工件。错误是:

Execution failed for task ':doubleZipTask'.
> Could not resolve all files for configuration ':myConfiguration'.
   > Could not find testA.zip (project :testA).
Run Code Online (Sandbox Code Playgroud)

为什么?如何解决?

完整的例子:

项目 testA

settings.gradle

rootProject.name = 'testA'
Run Code Online (Sandbox Code Playgroud)

build.gradle

plugins {
    id 'base'
    // Uncomment the line below to break the zip artifact
    //id 'java'
}

group = 'org.test'
version = '0.0.0.1_test'

task zipTask(type: Zip) {
    from './settings.gradle' // just so the …
Run Code Online (Sandbox Code Playgroud)

java artifact gradle build.gradle

6
推荐指数
2
解决办法
350
查看次数

std::filesystem:: 与不存在文件等效的版本

我的程序应该创建两个具有用户指定路径的文件。我需要知道路径是否通向同一位置,以便在开始更改文件系统之前以错误结束。

由于路径来自用户,因此它们应该是非规范且奇怪的。例如,它们可能是./dir1/subdir/file,并且符号链接dir2/subdir/../subdir/file在哪里并且还不存在。预期的结果仍然是,它们是等价的。dir2dir1subdirtrue

std::filesystem::equivalent仅适用于已存在的文件。有没有类似的功能,没有这个限制?

c++ c++17 std-filesystem

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

如何在 switch 语句中使用 spaceship 运算符

新的<=>运算符使编写代码更加方便,并且如果比较算法不平凡,它可以节省一些性能,因为它不需要重复两次才能获得完整的排序。

或者至少当我了解到这一点时我是这么认为的。然而,当我尝试在实践中使用它时,在switch声明中,它不起作用。

此代码无法编译:

#include <iostream>

void compare_values(int x, int y)
{
    switch (x <=> y)
    {
    case std::strong_ordering::less:
        std::cout << "is less\n";
        break;
    case std::strong_ordering::greater:
        std::cout << "is greater\n";
        break;
    case std::strong_ordering::equal:
        std::cout << "is equal\n";
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

编译器显示错误,提示返回的值<=>不能在 a 中使用switch

<source>: In function 'void compare_values(int, int)':
<source>:5:15: error: switch quantity not an integer
    5 |     switch (x <=> y)
      |             ~~^~~~~
Compiler returned: 1
Run Code Online (Sandbox Code Playgroud)

活生生的例子

我猜想在 switch 中使用 …

c++ switch-statement spaceship-operator c++20

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