小编康桓瑋*_*康桓瑋的帖子

使用 range::views::chunk_by 时可以引用当前子范围吗?

我得到了一个随机整数向量v,并希望按照以下标准分成子范围:

  1. 子范围中的整数应该是连续的并且加 1(易于检查)
  2. 子范围的大小不应大于 4(这很难)
auto result = v | ranges::views::chunk_by([](int a, int b) {
 return a + 1 == b /* && size of current chunk < 4 */;
});
Run Code Online (Sandbox Code Playgroud)

有谁知道如何做到这一点?有没有仍然使用ranges库的替代方法?

c++ std-ranges

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

为什么概念要求没有生效

在以下代码中,它在 -std=c++23 标志下编译。为什么概念要求 In 参数不应被引用触发?

#include <concepts>
#include <type_traits>
#include <functional>

template <typename T>
concept NotRef = requires { !std::is_reference_v<T>; };

template <typename In, typename Out>
    requires NotRef<In> // requires that In should not be reference
class SwitchType
{
    using ConstIn = std::add_const_t<In>;
public:
    SwitchType(const In& in)
        : in_{in}
    { }
    ConstIn & in_;
};

int main()
{
    int a{9};
    // Expected behavior: 'requirement not satifsfied'
    // Actural behavior: compiles under c++ 23

    SwitchType<int&, int> mytype{a};
}
Run Code Online (Sandbox Code Playgroud)

c++ c++-concepts c++23

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

来自 time_t 的 std::filesystem::file_time_type - 如何?

我想std::filesystem::file_time_type从 a创建 a std::time_t,但不知道如何做。

例子:

time_t t = 1337;
std::filesystem::file_time_type ft = ...; //how?
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望它能够与 c++17 一起使用,但我也会采用 c++20 解决方案。

c++ c++17 c++20

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

带有自动说明符和 static_cast 的基于范围的 for 循环

想象我有一个std::vectorof std::string,我想将它们转换std::stringstd::string_view基于范围的 for 循环:

auto v = std::vector<std::string>{"abc", "def", "ghi"};
for (std::string_view sv : v) {
    // do something with string_view
}
Run Code Online (Sandbox Code Playgroud)

上面的代码是完全有效的,但我想保留说明auto符来做到这一点,如何static_cast在一行基于范围的 for 循环中进行这样的操作?好像 C++20ranges可以以简洁的方式做到这一点,有人可以举个例子吗?

for (auto sv : v | static_cast<std::string_view>) {
    // do something with std::string_view
} 
Run Code Online (Sandbox Code Playgroud)

c++ for-loop auto c++20

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

在 C++ 中排序时映射会产生巨大的错误

我是使用地图的新手,当我尝试使用下面的代码对其进行排序时

#include <iostream>
#include<map>
#include<algorithm>
using namespace std;

int main()
{
    map<int,int> mp;
    mp[2]++;
    mp[5]++;
    mp[1]++;
    sort(mp.begin(), mp.end());
}

Run Code Online (Sandbox Code Playgroud)

抛出一个错误:

In file included from /usr/include/c++/6/algorithm:62:0,
                 from main.cpp:11:
/usr/include/c++/6/bits/stl_algo.h: In instantiation of ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::_Rb_tree_iterator<std::pair<const int, int> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’:
/usr/include/c++/6/bits/stl_algo.h:4707:18:   required from ‘void std::sort(_RAIter, _RAIter) [with _RAIter = std::_Rb_tree_iterator<std::pair<const int, int> >]’
<span class="error_line" onclick="ide.gotoLine('main.cpp',21)">main.cpp:21:30</span>:   required from here
/usr/include/c++/6/bits/stl_algo.h:1966:22: error: no match for ‘operator-’ (operand types are ‘std::_Rb_tree_iterator >’ and ‘std::_Rb_tree_iterator >’)
     std::__lg(__last - …
Run Code Online (Sandbox Code Playgroud)

c++

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

为什么gcc解析第一个return子句后不能推导出返回值类型?

为什么此代码无效?

auto f() {
  if (true) return 0;
  return {};
}
Run Code Online (Sandbox Code Playgroud)

解析后0,我认为gcc应该知道函数的返回类型fint,但它仍然解释{}initializer_list解析最终返回子句时,为什么?

c++ return initializer-list type-deduction c++20

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

为什么 constexpr const 作用域变量不是隐式静态的?

(接着这个问题:)

void foo() {
    constexpr const auto my_lambda = [](int z) { return z+1; };
}
Run Code Online (Sandbox Code Playgroud)

显然,my_lambda“不是静态的”。除了没有正式定义之外,在什么意义上它不是静态的?为什么它不应该是隐式静态的,看看它似乎符合定义

c++ lambda static constexpr storage-duration

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

std::ranges 可以与 std::list 一起使用吗

我注意到,许多算法可以与范围一起使用,允许使用自定义类型的成员,而不需要 lambda 函数。所以,我很好奇是否std::ranges可以有效地使用std::list<>. 我认识到这std::list<>不是此类算法的首选数据结构,但对于其他问题,它更适合我的目的。

c++ c++20 std-ranges

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

当“require 子句”被括号包围时,为什么还要用括号包围模板参数?

当 被括号包围时,为什么要用括号包围模板参数require clause

template(typename This, typename Receiver)
    (requires same_as<remove_cvref_t<This>, type> AND
      receiver<Receiver> AND
      constructible_from<std::tuple<Values...>, member_t<This, std::tuple<Values...>>>)
friend auto tag_invoke(tag_t<connect>, This&& that, Receiver&& r)
    noexcept(std::is_nothrow_constructible_v<std::tuple<Values...>, member_t<This, std::tuple<Values...>>>)
    -> operation<Receiver, Values...> {
  return {static_cast<This&&>(that).values_, static_cast<Receiver&&>(r)};
}
Run Code Online (Sandbox Code Playgroud)

来自libunifex

c++ c++-concepts c++20

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

如何将 int 转换为 string 然后与 std::ranges::views 连接?

#include <iostream>
#include <numeric>
#include <ranges>
#include <vector>
#include <string>
#include <string_view>

int main() {
    auto str = (
        std::views::iota(1)
        | std::ranges::views::take(5)
        | std::ranges::views::transform([](int x) -> std::string_view {
            return std::to_string(x) + "|";
        })
        | std::ranges::views::join
    );

    for (const char ch : str) {
        std::cout << ch;
    }

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

我是 cpp 函数式编程的新手。我想生成前五个自然数并将它们转换为字符串,然后连接它们并打印它。

如果我使用std::stringlambda 的返回类型进行转换,它会在编译时引发许多错误。我想我应该把它改成std::string_view. 我这样改了,编译没有编译错误。但是,如果我std::string_view在那里使用,lambda 函数仅返回字符串的引用,并且当 lambda 结束时,堆栈内存上的翻译字符串将被从内存中删除。因此,该程序不会打印任何内容。

我该如何修复它?

c++ c++20 std-ranges

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

如何从 constexpr 函数中调用未标记为 constexpr 的第三方库中的函数?

我想做的正是标题所说的。我有一些带有 API 的第三方代码。

评估函数所需的信息都应该在编译时可用以评估该函数。但第三方似乎并没有给它贴上constexpr悲伤的标签。此外,我没有完整实现的源代码,只有很多的标头。看起来它可能使用共享对象来评估调用,因为在深入研究 API 的头文件后,很多东西似乎都被标记为外部。由于这些调用并不是constexpr每次我尝试调用它以用于static_assert()编译时都会失败。

如何使该函数在编译时运行并在constexpr函数中使用结果来解决static_assert()条件?所有输入在编译时都是已知的。不需要外部数据。

c++ constexpr c++20

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

Short int 和 int 使用相同的代码给出不同的结果,有人可以解释为什么吗?

#include <iostream>
int main()
{
    unsigned int x{ 2 };
    int y{-3};
    std::cout<<x+y;

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

这段代码给了我答案 4294967295。

#include <iostream>
int main()
{
    unsigned short x{ 2 };
    short y{-3};
    std::cout<<x+y;

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

当我将之前代码中的 int 更改为 Short 时,新答案为 -1。

据我所知,这两种情况的正确答案应该是 4294967295,这是因为有符号和无符号整数算术产生无符号结果,但在使用短关键字时,我得到的答案是不同的。但我不明白为什么使用 Short 的代码给出了错误的答案,任何人都可以解释这里出了什么问题吗?

编辑:这不是其他问题的重复。别再标记这个了!请有人取消此标记

我已阅读当我在 C++ 中混合有符号和无符号类型时会发生什么?它很有帮助,但没有解决我的问题。

我重新发布这个问题是因为当我第一次问这个问题时,有人标记了这个问题,说它是重复的,但事实并非如此,请不要标记这个问题

c++

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