标签: std

使用 std::sort 时“二进制表达式的操作数无效”

invalid operands to binary expression当我尝试编译使用 std::sort 的项目时,出现错误。

我正在使用 std::sort 像这样:

vector <record> vrec;
...
sort(vrec.begin(), vrec.end());
Run Code Online (Sandbox Code Playgroud)

我已经重载了 < 运算符,如下所示:

bool operator< (record &r1, record &r2) { ... }
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误消息的摘录:

invalid operands to binary expression ('const record' and 'const record')

operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}

                                                         ~~~ ^ ~~~
Run Code Online (Sandbox Code Playgroud)

c++ sorting std

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

与“operator[]”不匹配(操作数类型为“std::unique_ptr&lt;std::vector&lt;int&gt; &gt;”和“int”)

我有一个std::unique_ptr<std::vector<int>>,我正在尝试使用运算符访问一个元素[]。如何访问 中包含的向量的特定索引std::unique_ptr

#include <memory>
#include <vector>

int main()
{
    std::unique_ptr<std::vector<int>> x;
    x[0] = 1;
}
Run Code Online (Sandbox Code Playgroud)

谢谢

c++ smart-pointers vector std

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

set::find 查找不存在的元素

我有以下Edge课程:

class Edge {
public:
int src, dest;

bool operator== (const Edge &edge) const {
    return ((src == edge.src) && (dest == edge.dest)) || ((src == edge.dest) && (dest == edge.src));
}

bool operator<(const Edge& edge) const {
    return !(((src == edge.src) && (dest == edge.dest)) || ((src == edge.dest) && (dest == edge.src)));
}

Edge(int src, int dest) {
    this->src = src;
    this->dest = dest;
}
};
Run Code Online (Sandbox Code Playgroud)

覆盖<运算符的要点是,当我尝试使find集合中的边Edge(0, 1)等于 时Edge(1, …

c++ std stdset c++11

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

*smart_ptr 和 *smart_ptr.get() 有什么区别

据我所知,取消引用 -*smart_ptrget()+ 取消引用*smart_ptr.get()使用智能指针做同样的事情,但可能有一些我不知道的事情,因为我在第二种方法中看到了很多情况被使用了,那有什么意义呢?它会以任何方式影响性能吗?

c++ smart-pointers std shared-ptr c++11

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

std::unordered_map::clear() 有什么作用?

我有一段简单的代码:

#pragma GCC optimize ("O0")
#include <unordered_map>
int main()
{
    std::unordered_map<int, int> map;
        
    static constexpr const int MaxN = 2e6 + 69;
    map.reserve(MaxN);
    
    int t = 1000;
    while (t--)
    {
        map.clear();
    }
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这段代码所做的只是创建一个巨大的std::unordered_map,在堆上保留大量内存,同时仍然保持为空,并清除 1000 次。令我惊讶的是,执行这个程序需要一秒钟多的时间。

根据cppreference元素std::unordered_map::clear数量是线性的,它是 0,而不是桶的数量。因此,这个函数在我的程序中应该什么都不做,而且应该不到一毫秒。

试图进一步分析代码,我写了这个:

#pragma GCC optimize ("O0")
#include <chrono>
#include <iostream>
#include <unordered_map>

#include <map>
template <typename T>
struct verbose_pointer
{
    using element_type = T;
    T* value = nullptr;
    static std::map<T*, std::size_t> accessTimes; …
Run Code Online (Sandbox Code Playgroud)

c++ unordered-map std

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

std::transform 不适用于静态分配的字符串

有人可以帮助我理解为什么这不起作用吗?

#include <vector>
#include <algorithm>

using namespace std;

struct person {
    int age;
    char name[30];
};

int main()
{
    vector<person> persons(2);
    vector<char*> names(2);
    
    persons[0].age = 1; 
    strcpy_s(persons[0].name, "mike");
    persons[1].age = 11; 
    strcpy_s(persons[1].name, "pol");
    transform(persons.begin(), persons.end(), names.begin(), 
        [](person p) -> char* {return p.name; });
    // ... names gets the same wrong pointer for both elements
}
Run Code Online (Sandbox Code Playgroud)

[编辑以消除不必要的错误猜测]

c++ std

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

自定义类 std::map 奇怪的行为

使用我创建的自定义类作为键在地图中搜索键时,我遇到了奇怪的行为。

尽管它们存在于地图中,但它似乎没有找到密钥。

有谁知道这是什么原因?

代码(可以在这里运行):

#include <iostream>
#include <map>

using namespace std;

typedef short int dimension;

class Point {
public:
    Point() : m_x(0), m_y(0) {}

    Point(const Point &other) : m_x(other.m_x), m_y(other.m_y) {};

    Point(dimension x, dimension y) : m_x(x), m_y(y) {}

    bool operator<(const Point &other) const {
        if (m_x < other.m_x) return true;
        return (m_y < other.m_y);
    }

private:
    dimension m_x;
    dimension m_y;
};


int main() {
    map<Point, bool> points = {{Point(0, 2), true},
                               {Point(1, 1), true},
                               {Point(2, 4), true}}; …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap std

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

使函数使用 std::span 而不是旧方法

我刚刚了解到我可以将 VS 2019 与 C++ 20 一起使用,并且我正在尝试使用它。我正在尝试使以下功能使用std::span,因为那样做data_size并且key_size将是多余的。

一切都好,除了*datadata++。它应该是什么?

int rc4(rc4_context* context, const std::uint8_t* data, const std::size_t data_size, const std::uint8_t* key, const std::size_t key_size, std::uint8_t* output)
{
    std::uint32_t i, j;

    // Check parameters
    if (!context || !key)
        return ERROR_INVALID_PARAMETER;

    // Clear context
    context->i = 0;
    context->j = 0;

    // Initialize the S array with identity permutation
    for (i = 0; i < 256; i++)
    {
        context->s[i] = static_cast<std::uint8_t>(i);
    }

    // S …
Run Code Online (Sandbox Code Playgroud)

c++ std c++20

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

C ++中集合和多集之间的多态性

有没有办法使用多态来为集合和多集提供一个泛型?如下。

注意:但仅适用于集合,(set, multiset)

template<typename T>
void foo(parent_set<T> &s) {
    // do something
}
// main
set<int> s1 = {1, 2, 3, 4, 5};
foo(s1);
multiset<int> s2 = {1, 2, 2, 2, 2, 3};
foo(s2); 
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism std set multiset

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

如何使用 std::optional 返回 NULL 值?

如何返回一个类似于 null 的值std::optional?我的函数接收一个 int 索引作为参数来遍历列表,如果索引值无效,如何std::optional使用它返回类似于 null 的值?

c++ std stdoptional

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