小编Dar*_*row的帖子

在特定条件下跳过std :: for_each中的迭代

我正在开发一个需要迭代一定范围的程序。我想知道是否可以continue在基于范围的for循环中使用时使用。

工作方式:

std::vector<std::string> v = {"foo", "bar", "baz", "foobar"};
for (auto s : v)
{
    if (*s.front() == 'b')
        continue;
    std::cout << s << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

不工作:

std::vector<std::string> v = {"foo", "bar", "baz", "foobar"};
std::for_each(v.begin(), v.end(), [](const std::string& s) {
    if (*s.front() == 'b')
        continue;
    std::cout << s << std::endl;
});
Run Code Online (Sandbox Code Playgroud)

c++

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

for_each没有返回(布尔值)值

我有一个程序来验证作为字符串输入的IPv4地址是否是有效的点分四位表示法.

我面临的问题是,一旦检测到错误,我就无法返回(退出)功能.根据cppreference文档for_each返回UnaryFunction.我尝试使用any_of和all_of,但是他们要求我在我的lambda函数中使用一个循环(基于范围的循环),我试图避免.我错过了什么或者无法在for_each中返回值.

vector<string> ipExplode;
string ip;
bool    inValidIp = false;
cout << "Server IP : ";
cin >> ip;
trim(ip);
ipExplode = explode(ip, '.');
if(not for_each(ipExplode.begin(), ipExplode.end(), [](const string& str) -> bool{
    int32_t ipNum;
    if(regex_search(str, regex("\\D+")))
        return false;
    try
    {
        ipNum = stoi(str);
        if(ipNum < 0 or ipNum > 255)
            return false;
    }
    catch (std::exception& ex)
    {
        return false;
    }
}))
    return false;
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

在 Raspberry Pi 4 (8 GB) 上安装 Flutter

我正在使用以下文章在 Raspberry Pi 4 中安装 Flutter https://snapcraft.io/install/flutter/raspbian#install

我收到以下错误

pi@raspberrypi:~ $ sudo snap install flutter --classic
error: snap "flutter" is not available on stable for this architecture (armhf) but exists on other architectures (amd64, arm64).
Run Code Online (Sandbox Code Playgroud)

我尝试添加arm64架构但无法添加。

pi@raspberrypi:~ $ sudo dpkg --print-architecture
armhf
pi@raspberrypi:~ $ sudo dpkg --add-architecture arm64
pi@raspberrypi:~ $ sudo dpkg --print-architecture
armhf
Run Code Online (Sandbox Code Playgroud)

尝试添加架构后,我再次尝试安装 Flutter

pi@raspberrypi:~ $ sudo snap install flutter --classic
    error: snap "flutter" is not available on stable for this architecture (armhf) but exists on other architectures (amd64, …
Run Code Online (Sandbox Code Playgroud)

raspberry-pi flutter raspberry-pi4

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

在哪里定义 _SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING 或 _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS 宏?

我正在使用 Boost.Asio 开发 TCP/IP 程序,但出现错误:

note: see declaration of 'std::allocator<void>' 1>d:\hardware\libraries\visual studio 2017\x64\include\boost\asio\use_future.hpp(137): error C4996: 'std::allocator<void>': warning STL4009: std::allocator<void> is deprecated in C++17. You can define
_SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING or _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS to acknowledge that you have received this warning.
Run Code Online (Sandbox Code Playgroud)

阅读错误后,我明白我已经定义了

_SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING

或者

_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
Run Code Online (Sandbox Code Playgroud)

我在 Configuration Properties -> C/C++ -> General -> Additional #using Directories 中定义了一次,但错误仍然存​​在。

然后我在包含任何头文件之前 #defined 主文件中的宏,但错误仍然存​​在。

然后我在我的每个编译的牧羊人中声明了宏,但错误仍然存​​在。

我什至尝试了 2 种不同的增强包。一个安装了 vcpkg 包管理器和一个预填充二进制文件。

MSVC 14.1 x64 增强:1.68 (x64)

c++ boost boost-asio

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

返回std :: nullopt作为非恒定引用

如果我想返回std :: nullopt作为非恒定引用,请形成学术观点。我将如何做同样的事情。

没什么背景,今天当我在处理返回std :: optional>引用的代码时,却忘记了使返回常量,并得到了错误。

Error (active)    E0434   a reference of type "std::optional<std::vector<std::any, std::allocator<std::any>>> &" (not const-qualified) cannot be initialized with a value of type "const std::nullopt_t"
Socket.IO   D:\Hardware\Windows\Visual Studio\Socket.IO\Socket.IO\main.cpp  46  

Error C2440   'return': cannot convert from 'const std::nullopt_t' to 'std::optional<std::vector<std::any,std::allocator<_Ty>>> &'
Socket.IO   d:\hardware\windows\visual studio\socket.io\socket.io\main.cpp  46
Run Code Online (Sandbox Code Playgroud)

只是想知道是否有人想使用std :: optional返回一个非常量引用,他将如何做。

使用平台:Windows 10 Pro x64

开发环境:Visual Studios 15.9.9

std::vector<int>> a;
std::optional<std::vector<int>>& test(int b)
{
    a.clear();
    a.push_back(b);
    if(b)
         return a;
    else
         return std::nullopt;
}
Run Code Online (Sandbox Code Playgroud)

c++ std c++17 stdoptional

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