以下代码:
while (node)
{
if (node->previous== NULL) break;
struct Node* prevNode = node->previous;
len = strlen(prevNode->entity);
//pp is a char* fyi
pp-=len;
strncpy(pp, prevNode->entity, len+1);
*(--pp) = '/';
node = prevNode;
}
Run Code Online (Sandbox Code Playgroud)
在 GCC 中生成以下警告/错误(我将所有警告视为错误):
../someFile.C:1116:24: error: 'char* strncpy(char*, const char*, size_t)' specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
1116 | strncpy(pp, prevNode->entity, len+1);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../someFile.C:1114:29: note: length computed here
1114 | len = strlen(prevNode->entity);
| ~~~~~~^~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
为什么 GCC 给我一个警告?依赖缓冲区大小的源参数大小有什么问题?有人可以举例说明这可能导致什么问题吗?代码做了它应该做什么我只是好奇为什么我会收到警告。
我觉得奇怪的是,尽管有大量的多线程构造,但该标准却缺少线程池类。什么原因可能会阻止委员会将其添加到标准中?
基本上我的问题是,为什么不能编译?
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int main() {
vector<int> v{1,2,3};
auto hash_function=[](const vector<int>& v){
size_t hash;
for (int i = 0; i < v.size(); ++i) {
hash+=v[i]+31*hash;
}
return hash;
};
unordered_set<vector<int>, decltype(hash_function)> s(hash_function);
std::cout<<s.bucket_count();
std::cout<<"here";
}
Run Code Online (Sandbox Code Playgroud)
但如果我将 unordered_set 行更改为此
unordered_set<vector<int>, decltype(hash_function)> s(10,hash_function);
Run Code Online (Sandbox Code Playgroud)
它会。为什么需要初始桶计数?使用 lambda 迫使我添加初始存储桶计数,但使用函子则不会,这似乎很奇怪。请参阅此处的示例:C++ unordered_set of 矢量,以证明函子版本不需要初始数量的存储桶。
我使用新项目向导在 Intellij 设置中创建了一个新的 Jetpack Compose 桌面项目。我想像在 Android 中一样使用 ConstraintLayout,所以我添加以下行
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
Run Code Online (Sandbox Code Playgroud)
在 build.gradle 中,如下所示:
sourceSets {
val jvmMain by getting {
dependencies {
implementation(compose.desktop.currentOs)
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
}
}
val jvmTest by getting
}
Run Code Online (Sandbox Code Playgroud)
但是我仍然无法访问约束布局。
根据这个问题的答案,似乎可以在桌面中使用约束布局。我究竟做错了什么?
我已经使用常规方法(双链表+哈希映射)在LeetCode上实现了LRU缓存问题的设计。对于那些不熟悉该问题的人,此实现看起来像这样:
我知道为什么要使用这种方法(两端都快速移除/插入,中间快速访问)。我无法理解的是为什么当人们只使用基于数组的双端队列(在Java ArrayDeque中,C ++只是双端队列)时,为什么有人同时使用HashMap和LinkedList的原因。此双端队列使两端的插入/删除操作变得容易,并且在中间可以快速访问,这正是LRU缓存所需要的。您还可以使用更少的空间,因为您不需要存储指向每个节点的指针。
是否有理由为什么使用后一种方法而不是Deque / ArrayDeque方法对LRU缓存进行几乎通用的设计(至少在大多数教程中如此)?HashMap / LinkedList方法有什么好处吗?
通常在解决递归或动态规划问题时,我发现自己画了一个递归树来帮助我简化问题。但是,对于一些复杂的问题,我可以访问解决方案,但不知道如何绘制树。
到目前为止,我所尝试的是打印出调用函数及其参数,这在一些示例中证明是有帮助的。但是,我在这个答案中看到了由 mathematica 在这里生成的 fibonacci(5) 树:https : //mathematica.stackexchange.com/questions/116344/how-do-i-create-a-recursive-tree-plot-for -斐波那契数列
我想知道是否可以使用 Python、Java 或 C++ 等主流高级语言生成相同类型的树?树可以像图像中那样将节点作为函数名称和参数。
我正在以下无用程序中的 gcc 中设置调试模式:
#define _GLIBCXX_DEBUG 1
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v{1,2,3};
for(int i=0; i<100000000000000;i++)
cout<<v[i];
}
Run Code Online (Sandbox Code Playgroud)
该程序让我知道我的索引超出范围。但是,如果我翻转前两行的顺序,则不会收到此类错误消息(#include 在 #define 之前)。为什么是这样?有没有办法在程序的另一行上切换调试模式(没有编译器标志)?我这么问是因为我正在 Leetcode.com 上解决问题,我无法传递编译器标志或修改问题的第一行。
这样做会保护代码免受过多std ::的攻击吗?
// These underscore_cased type aliases make my code shorter and easier to read for me.
// For Example:
template<class T> using v=std::vector<T>;
template<class T, class U> using u_m=std::unordered_map<T,U>;
template<class T> using p_q=std::priority_queue<T>;
template<class T,class U> using u_mm=unordered_multimap<T,U>;
Run Code Online (Sandbox Code Playgroud)
还是这种冒险行为?
我没有看到这会如何污染我的命名空间,就像使用命名空间std那样。
我有以下头文件MappingSingleton.h:
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include<iterator>
#include <sstream>
#include <thread>
#include <mutex>
#include "SOPExpr.h"
class MappingSingleton {
private:
static MappingSingleton& mapping_singleton;
MappingSingleton();
public:
static unsigned int wireID;
static bool is_init;
std::vector<SOPExpr> m_hex_to_SOP_4;
std::vector<int> m_hex_to_line_number_4;
std::vector<SOPExpr> m_hex_to_SOP_3;
std::vector<int> m_hex_to_line_number_3;
std::vector<SOPExpr> m_hex_to_SOP_2;
std::vector<int> m_hex_to_line_number_2;
void loadLUTNHexToStringMapping(std::vector<SOPExpr>& m_hex_to_SOP, std::vector<int> &m_hex_to_line_number, short int LUT_size);
std::string getQTPrims(std::string& LUT_init_string);
static MappingSingleton getInstance();
private:
static void initSingleton();
static std::once_flag init_instance_flag;
int getExprIndex(std::string &key, short lut_size);
int wire_id;
};
Run Code Online (Sandbox Code Playgroud)
以及MappingSingleton.cpp中的以下函数:
MappingSingleton MappingSingleton::getInstance() {
std::call_once(MappingSingleton::init_instance_flag,&MappingSingleton::initSingleton); …
Run Code Online (Sandbox Code Playgroud) 最小可重复示例:
using namespace std;
#include<string>
#include<string_view>
#include<iostream>
int main()
{
string s = "someString";
string_view sV = string_view(s);
string_view sVTwo = string_view(begin(s), end(s));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
创建 string_view sV 的时间复杂度是否与字符串 s 中的字符数成线性关系,或者与字符串 s 的长度无关?同样,构造 sVTwo 的时间复杂度是线性的还是恒定的,取决于字符串中有多少个字符?
我感到困惑的原因是我无法分辨这里的哪些构造函数: https: //en.cppreference.com/w/cpp/string/basic_string_view/basic_string_view用于构造字符串。