小编Dam*_*hev的帖子

C++ 标准库算法的可读性

我曾经以一种可以用简单英语阅读的方式编写代码。

\n

有人可以告诉我如何更好地用这些术语阅读下面的代码吗?

\n

我的问题是,下面的代码读作 \xe2\x80\x9cerase all unique elements\xe2\x80\x9d ,而它的作用恰恰相反。

\n

这并不是建议以某种方式重新设计 STL 和范围,我只是好奇std::unique在这种情况下考虑/处理(以及其他一些方法)的一些技巧,以使本文不那么违反直觉。

\n

如果下面的代码中缺少 \xe2\x80\x9cnot\xe2\x80\x9d (这可能有助于读取为“不唯一”或“除了唯一”),那么阅读此代码的最佳方法是什么?

\n
std::vector<T> v = \xe2\x80\xa6;\nstd::sort(v.begin(), v.end());\nv.erase(std::unique(v.begin(), v.end()), v.end());\n
Run Code Online (Sandbox Code Playgroud)\n

Ranges 版本在这些方面并没有更好:

\n
std::vector<T> v = \xe2\x80\xa6;\nstd::ranges::sort(v);\nv.erase(std::ranges::unique(v), v.end());\n
Run Code Online (Sandbox Code Playgroud)\n

c++ std-ranges

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

Visual C++:为什么调用来自不同编译单元的类方法?

我有两个独立的编译单元,没有任何头文件:

\n

单元a.cpp:

\n
#include <algorithm>\n#include <vector>\nclass my_predicate\n{\n    const std::vector<int>& vec;\npublic:\n    my_predicate(const std::vector<int>& container) : vec(container) { }\n\n    bool operator() (size_t idx1, size_t idx2)\n    {\n        return vec[idx1] < vec[idx2];\n    }\n};\n\nint main()\n{\n    std::vector<int> v1,v2;\n    v1.resize(10);\n    v2.resize(10);\n\n    std::sort(v1.begin(), v1.end(), my_predicate(v2));\n}\n
Run Code Online (Sandbox Code Playgroud)\n

单元b.cpp:

\n
#include <algorithm>\n#include <vector>\n\nclass my_predicate\n{\n    const std::vector<char>& vec;\npublic:\n    my_predicate(const std::vector<char>& container) : vec(container) { }\n\n    bool operator() (size_t idx1, size_t idx2)\n    {\n        std::cout << "Why the operator from b.cpp is called?" << std::endl;\n        return vec[idx1] < vec[idx2];\n    }\n};\n\nvoid bar()\n{\n    std::vector<char> …
Run Code Online (Sandbox Code Playgroud)

c++ visual-studio visual-c++ c++17

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

使用结构化绑定声明来解压 std::equal_range 的结果

出于可读性考虑,我避免在复杂代码中使用std::pair来保持变量名称有意义。

考虑到std::equal_range返回迭代器的,使用结构化绑定以这种方式解包迭代器std::pair是否有效且安全:

代替

auto it_lower = std::lower_bound(my_array.begin(), my_array.end(), val, comparator);
auto it_upper = std::upper_bound(my_array.begin(), my_array.end(), val, comparator);
Run Code Online (Sandbox Code Playgroud)

使用

auto [it_lower, it_upper] = std::equal_range(my_array.begin(), my_array.end(), val, comparator);
Run Code Online (Sandbox Code Playgroud)

方法?

equal_range忘记and lower_bound/对可能的不同实现upper_bound(不同的性能等)并仅关注结构化捆绑的使用,从编译代码的表示和性能来看这是否相等?我不是问编译器会做什么。我的问题是,如果我正确使用该工具,我可以期待类似的结果吗?还是有一些不同甚至更糟糕的陷阱?

我错过了什么还是这完全没问题?

c++ stl

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

如果没有数据发送到 std::ofstream,如何避免创建文件?

当 C++ 创建std::ofstream它时,它立即并隐式创建底层文件。

我完全同意这种行为,除非我有一个代码,只有在运行期间才能看到是否会产生任何数据。

因此,我想避免在没有数据发送给空文件时创建空文件(事务完整性:没有数据,文件系统上没有更改)。

我看到两种我不太喜欢的方法:

  1. 查看是否有内容发送到流 ( tellg()),如果流为空,则删除该文件。我不喜欢创建和删除文件(有时文件很多)并且remove操作本身承担了太多责任。
  2. 创建std::stringstream,收集输出并std::ofstream仅在字符串流不为空的情况下创建和复制内容。好多了,但仍然需要临时内存分配,这可能很大。

对此有更好的解决方案吗?我是否缺少一些想法?

以代码的形式:

#include <fstream>

int main()
{
    std::ofstream ofs("file.txt");

    // Some code that might or might not output to ofs
    // Some other code that might or might not output to ofs
    // Some more code that might or might not output to ofs

    // It would be nice if file is not created if no data sent to ofs
} …
Run Code Online (Sandbox Code Playgroud)

c++ std

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

为什么C++函数不能用作类型?

C++ 函数不能用作作为函子或谓词传递的类型的原因是什么?

函子的主要目的之一是充当函数,以便可以以函数方式调用它。好吧,它可以做更多的事情(有状态等),但是为什么我们不能使用函数而不是函子,因为函子的唯一目的是被调用?

好吧,应该构造对象,对于函子也是如此,但是如果我们看到函数已被传递,则可以跳过这一步。

下面的代码中,函数和函子的行为类似,直到我尝试将它们传递给 std::map。

#include <algorithm>
#include <map>
#include <ranges>
#include <span>
#include <vector>

bool std_span_less(const std::span<const unsigned int> lhs, const std::span<const unsigned int> rhs) {
    return std::ranges::lexicographical_compare(lhs, rhs);
}

struct std_span_less_fn {
    bool operator() (const std::span<const unsigned int> lhs, const std::span<const unsigned int> rhs) const {
        return std::ranges::lexicographical_compare(lhs, rhs);
    }
};

int main()
{
    using fn_type = bool (*)(const std::span<const unsigned int> , const std::span<const unsigned int> );

    std::vector<unsigned int> lexems = { 0, 1, …
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×5

c++17 ×1

std ×1

std-ranges ×1

stl ×1

visual-c++ ×1

visual-studio ×1