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) 我有一个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)
谢谢
我有以下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, …
据我所知,取消引用 -*smart_ptr和get()+ 取消引用*smart_ptr.get()使用智能指针做同样的事情,但可能有一些我不知道的事情,因为我在第二种方法中看到了很多情况被使用了,那有什么意义呢?它会以任何方式影响性能吗?
我有一段简单的代码:
#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) 有人可以帮助我理解为什么这不起作用吗?
#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)
[编辑以消除不必要的错误猜测]
使用我创建的自定义类作为键在地图中搜索键时,我遇到了奇怪的行为。
尽管它们存在于地图中,但它似乎没有找到密钥。
有谁知道这是什么原因?
代码(可以在这里运行):
#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) 我刚刚了解到我可以将 VS 2019 与 C++ 20 一起使用,并且我正在尝试使用它。我正在尝试使以下功能使用std::span,因为那样做data_size并且key_size将是多余的。
一切都好,除了*data和data++。它应该是什么?
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) 有没有办法使用多态来为集合和多集提供一个泛型?如下。
注意:但仅适用于集合,(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) 如何返回一个类似于 null 的值std::optional?我的函数接收一个 int 索引作为参数来遍历列表,如果索引值无效,如何std::optional使用它返回类似于 null 的值?
c++ ×10
std ×10
c++11 ×2
c++20 ×1
multiset ×1
polymorphism ×1
set ×1
shared-ptr ×1
sorting ×1
stdmap ×1
stdoptional ×1
stdset ×1
vector ×1