我想使用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)
这段代码显然崩溃了,因为我在迭代它时改变了向量的末尾.实现这一目标的最佳方法是什么?也就是说有没有办法做到这一点,而无需多次迭代矢量或创建一个矢量的副本?
我需要知道是否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 是强制执行二进制补码的标准的第一个版本,但变量的大小对我来说比表示形式更重要。
我需要在shell脚本中将主机名解析为IP地址.代码必须至少工作中Cygwin,Ubuntu和OpenWrt(busybox).可以假设每个主机只有一个IP地址.
例:
输入
google.com
Run Code Online (Sandbox Code Playgroud)产量
216.58.209.46
Run Code Online (Sandbox Code Playgroud)编辑:
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) 当前,即使对它的所有调用确实为,也无法用于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) 在我正在工作的项目中,我找到了一个类,它在一些精心设计的异常处理中包装了它的超类的所有方法.它看起来类似于:
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) 我用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.
让我们从一些代码开始吧.这是我程序的极简化版本.
#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) 我有一个创建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)
为什么?如何解决?
testAsettings.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) 我的程序应该创建两个具有用户指定路径的文件。我需要知道路径是否通向同一位置,以便在开始更改文件系统之前以错误结束。
由于路径来自用户,因此它们应该是非规范且奇怪的。例如,它们可能是./dir1/subdir/file,并且符号链接dir2/subdir/../subdir/file在哪里并且还不存在。预期的结果仍然是,它们是等价的。dir2dir1subdirtrue
std::filesystem::equivalent仅适用于已存在的文件。有没有类似的功能,没有这个限制?
新的<=>运算符使编写代码更加方便,并且如果比较算法不平凡,它可以节省一些性能,因为它不需要重复两次才能获得完整的排序。
或者至少当我了解到这一点时我是这么认为的。然而,当我尝试在实践中使用它时,在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 中使用 …