在 Visual Studio 2013 终极版中:
一种)
using namespace std;
typedef map<string, double> my_set;
Run Code Online (Sandbox Code Playgroud)
=> 错误:地图不是模板
b)
typedef std::map<string, double> my_set;
Run Code Online (Sandbox Code Playgroud)
=> 错误:不允许使用限定名称
a 或 b 在文件 foo.h 中,两个版本都不起作用。我究竟做错了什么?(是的,花了一些时间谷歌搜索 => 没有结果,c++ 是一种奇怪的语言)
我在写一些C++代码时遇到了以下现象:
我有一张看起来像这样的地图:
std::map<test_struct_t*, unsigned int, cmp_by_value> testmap;
Run Code Online (Sandbox Code Playgroud)
该映射位于我的程序中,结构定义为:
struct test_struct_t {
int x; int y; int val;
bool operator<(const test_struct_t &o) const {
return x < o.x || y < o.y || val < o.val;
}
test_struct_t(int a, int b, int c) : x(a), y(b), val(c) {}
};
Run Code Online (Sandbox Code Playgroud)
我写的自定义比较器是:
struct cmp_by_value {
bool operator()(const test_struct_t *a, const test_struct_t *b) const
{
return *a < *b;
}
};
Run Code Online (Sandbox Code Playgroud)
现在,在我的主要方法中,我执行以下操作:
testmap.insert({new test_struct_t(0, 0, 2 ), 6});
testmap.insert({new test_struct_t(0, 1, 2 ), 6}); …Run Code Online (Sandbox Code Playgroud) 我目前正在重构代码的一些旧部分。我正在研究的方法大量使用new和delete功能来实现其目的。我可以轻松地用共享指针替换所有这些,以获得更好的代码清晰度和安全性。问题是,最终我需要返回一个指向已分配内存的指针以匹配 API。这不适用于共享指针。
我可以分配一个新的内存块并new复制分配的内存内容shared_ptr(我的第一个想法)。但后来我想也许有一种机制可以告诉shared_ptr不要在上下文丢失时释放分配的内存?这样我就可以通过共享 ptr 返回指向已分配内存的指针,而无需释放它。
我想将所有值放入 a 中std::vector true。我写了两种方法:第一个有效,但第二个无效。它告诉我 myproj.exe 已触发断点。你知道问题出在哪里吗?
这个有效:
void first(std::vector<bool>& vect, unsigned int n)
{
for (unsigned int i = 0; i < n; i++)
{
vect.push_back(true);
}
}
Run Code Online (Sandbox Code Playgroud)
这一个不:
void secound(std::vector<bool>& vect, unsigned int n)
{
for(unsigned int i = 0; i < n; i++)
{
vect[i] = true; //crash here
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用向量作为成员字段来设置对象。我设置了构造函数,据我所知它确实初始化了这个成员变量,但是当我尝试访问向量时出现错误,除非我在对象的函数调用中初始化它。我收到一条警告说
C26495: Variable 'Engy::Graphics::Shape2D::m_vertices' is uninitialized. Always initialize a member variable (type.6).
问题是,我确实在我的构造函数中初始化了它。我缺少什么重要的东西吗?这是我的代码:
类声明(头文件)
class Shape2D:Object
{
protected:
std::vector<Vector> *m_vertices;
int type = GL_QUADS;
public:
Shape2D(std::vector<Vector> vertices);
Shape2D();
void draw();
void setVector(int index, Vector value);
Vector getVector(int index);
void translate(double x, double y);
void createVector(int i, double x, double y);
void createVector(double x, double y);
void addVector(Vector value);
};
Run Code Online (Sandbox Code Playgroud)
方法声明(.cpp 文件)
Shape2D::Shape2D(std::vector<Vector> vertices)
{
std::vector<Vector> m_vertices = vertices;
}
Shape2D::Shape2D()
{
std::vector<Vector> *m_vertices = new std::vector<Vector>;
}
void Shape2D::setVector(int index, …Run Code Online (Sandbox Code Playgroud) 很多时候我必须使用自定义比较函数进行排序。我可以实施它,但有时我会犯错误。如果它返回,我通常会感到困惑,true然后它会被交换吗?有人可以解释如果它返回会发生true什么以及如果它返回会false怎样?
我std::vector在这一行中使用:
std::vector<bool> visited(length);
Run Code Online (Sandbox Code Playgroud)
解决LeetCode 问题:
给定一个整数数组 arr,您最初位于数组的第一个索引处。
在一个步骤中,您可以从索引 i 跳转到索引:
- i + 1 其中: i + 1 < arr.length。
- i - 1 其中:i - 1 >= 0。
- j 其中: arr[i] == arr[j] 和 i != j。
返回到达数组最后一个索引的最小步数。
请注意,您不能在任何时候跳出数组。
示例 1:
输入:arr = [100,-23,-23,404,100,23,23,23,3,404]
输出:3
解释:你需要从索引 0 --> 4 --> 3 --> 9 跳三下。请注意,索引 9 是数组的最后一个索引。
约束:
1 <= arr.length <= 5 * 10^4 -10^8 <= arr[i] <= 10^8
#include <vector>
#include <unordered_map>
#include <queue>
class …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <memory>
int main() {
int i = 0;
float f = 0.0f;
double d1 = 0.0, d2 = 0.0, d3 = 0.0, d4 = 0.0;
auto a = [i,f,d1,d2,d3,d4](){};
std::cout << sizeof(std::unique_ptr<decltype(a)>) << std::endl; // 8
std::cout << sizeof(std::unique_ptr<char, decltype(a)>) << std::endl; // 48
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我只添加一个字符时,为什么这个程序的输出是 48?
我std::thread在我的 C++ 程序中执行了一个无限 while 循环。这样做时,我的程序使用了 45% 的 CPU(根据任务管理器)。当使用 '限制' 循环时std::this_thread::sleep_for(std::chrono::milliseconds(1)),CPU 使用率下降到 12%' 但是,当然,这个解决方案远非最佳,特别是考虑到我正在开发游戏的事实。如何在不休眠线程的情况下降低 CPU 使用率,因为只要 if 条件(位于循环内)为真,我就需要它可用?
我正在重构一个带有太多 if-else 的函数,类似于以下但更复杂。该函数的一些主要特征是:
condition1()和condition2())。doA()和doB())。(哦,是的,临时修复错误的美妙之处!)condition3/4/5/6())。retT foo() { // total complexity count = 6
if (!condition1()) { // complexity +1
return retT{};
}
if (!condition2()) { // complexity +1
return retT{};
}
if (condition3()) { // complexity +1
if (condition4() || condition5()) { // complexity +2
return doA();
}
else if (condition6()) { // complexity +1
return doB();
}
}
return retT{};
}
Run Code Online (Sandbox Code Playgroud)
目标是在精确的条件下调用那些实际的作品,而不是让它们容易受到 中 if-else 结构的变化的影响foo()。更具体地说,我想变成 …