给出一小段代码,您将如何确定一般的复杂性.我发现自己对Big O问题非常困惑.例如,一个非常简单的问题:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.println("*");
}
}
Run Code Online (Sandbox Code Playgroud)
电讯局长用类似的东西解释了这一点.像这样是n选择2 =(n(n-1))/ 2 = n ^ 2 + 0.5,然后移除常数使其变为n ^ 2.我可以把int测试值和尝试但是这个组合的东西怎么样?
如果是if语句怎么办?如何确定复杂性?
for (int i = 0; i < n; i++) {
if (i % 2 ==0) {
for (int j = i; j < n; j++) { ... }
} else {
for (int j = 0; j < i; j++) { ... } …
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试使用googletest与MinGW,-std=c++0x
但它抱怨_stricmp is not declared in this scope
当我不使用它时它不会-std=c++0x
.我不知道是什么_stricmp
,我刚刚发现它已定义cstring/string.h
,所以为什么它在C++ 0x中消失了?
我正和Lua玩一点.
我遇到了以下具有意外行为的代码段:
a = 3;
b = 5;
c = a-- * b++; // some computation
print(a, b, c);
Run Code Online (Sandbox Code Playgroud)
Lua运行该程序没有任何错误,但没有2 6 15
按预期打印.为什么?
考虑最小化的代码片段:
#include <vector>
class Bar
{
public:
constexpr Bar() {}
};
consteval Bar foo()
{
return Bar();
}
int main()
{
std::vector<Bar> bars{ foo(), foo() };
}
Run Code Online (Sandbox Code Playgroud)
这不能在最新的 MSVC 编译器(Visual Studio 2022 版本 17.3.3)上编译,但可以在 Clang 或 GCC 上编译。
代码是否格式不正确,或者是 MSVC 中的错误?
C++ 11引入了强类型枚举,具有语法enum class
.这些与整数类型不兼容,需要显式强制转换才能获取其数值.C++ 11还引入了为表单中的弱类型枚举指定存储类的功能enum name : type {}
.这在这里很好.
但看起来即使弱类型的枚举具有给定的存储类,其项目的类型仍然是int
.我尝试使用Visual Studio 2012,11月CTP发布.请考虑以下代码:
enum charEnum : char { A = 'A' };
enum longEnum : long long { Tera = 1000000000000 };
void fct(char val) {}
void fct(int val) {}
void fct(long long val) {}
int main()
{
static_assert(sizeof(A) == sizeof(char), "check charEnum size");
static_assert(sizeof(Tera) == sizeof(long long), "check longEnum size");
fct('A'); // calls fct(char)
fct(1); // calls fct(int)
fct(2ll); // calls fct(long long)
fct(A); // …
Run Code Online (Sandbox Code Playgroud) 我现在正在学习C++,我听过很多关于嵌入式脚本语言的知识.我想象它完全不同.
我以为我会用C++编写所有性能繁重的函数,并用Lua或Python等脚本语言调用它们.
但它似乎是另一种方式. - >在Lua/Python中编写函数并用C代码调用它们.
在C++中嵌入语言而不是用C++编写API并用另一种语言调用这些函数有什么好处?
例:
// function in c++
int expensiveFunction(){
return 1;
}
Run Code Online (Sandbox Code Playgroud)
然后在Python中我将调用此函数,我将从C++获得性能,但由于Python的运行时解释器,可以在运行时进行更改.
我正在尝试为基于bplists的Safari远程调试协议编写一个解剖器并且已经相当成功(当前代码在这里:https://github.com/andydavies/bplist-dissector).
虽然我在重新组装数据包时遇到了困难.
通常,协议发送一个包含4个字节的数据包,其中包含下一个数据包的长度,然后是包含bplist的数据包.
不幸的是,来自iOS模拟器的一些数据包不符合此约定,并且四个字节或者标记在bplist数据包的前面,或者标记在前一个bplist数据包的末尾,或者数据是多个bplists.
我试着用重组他们desegment_len
和desegment_offset
如下:
function p_bplist.dissector(buf, pkt, root)
-- length of data packet
local dataPacketLength = tonumber(buf(0, 4):uint())
local desiredPacketLength = dataPacketLength + 4
-- if not enough data indicate how much more we need
if desiredPacketLen > buf:len() then
pkt.desegment_len = dataPacketLength
pkt.desegment_offset = 0
return
end
-- have more than needed so set offset for next dissection
if buf:len() > desiredPacketLength then
pkt.desegment_len = DESEGMENT_ONE_MORE_SEGMENT
pkt.desegment_offset = desiredPacketLength
end …
Run Code Online (Sandbox Code Playgroud) 我发现,将lambda函数转换为std::function<>
值的相对少量代码的编译时间可能非常高,特别是对于Clang编译器.
考虑以下创建100个lambda函数的虚拟代码:
#if MODE==1
#include <functional>
using LambdaType = std::function<int()>;
#elif MODE==2
using LambdaType = int(*)();
#elif MODE==3
#include "function.h" // https://github.com/skarupke/std_function
using LambdaType = func::function<int()>;
#endif
static int total=0;
void add(LambdaType lambda)
{
total += lambda();
}
int main(int argc, const char* argv[])
{
add([]{ return 1; });
add([]{ return 2; });
add([]{ return 3; });
// 96 more such lines...
add([]{ return 100; });
return total == 5050 ? 0 : 1;
}
Run Code Online (Sandbox Code Playgroud)
根据MODE
预处理器宏,该代码可以在以下三种方式之间进行选择,以便通过lambda函数传递 …
我的同事正在使用 NaN 进行一些基本实验,并对 Visual Studio 上与他的预期不符的行为感到困惑。经过讨论,他似乎发现了 MSVC 2019 中可能存在的编译器错误。
此代码片段无法在 MSVC 上编译,但在 Clang 和 GCC 上可以正常编译:
#include <limits>
int main()
{
static_assert(!(1 < std::numeric_limits<double>::quiet_NaN()), "compiler bug?");
}
Run Code Online (Sandbox Code Playgroud)
演示: https: //godbolt.org/z/xGdqd5
Il 似乎问题涉及常量与 的编译时比较std::numeric_limits<double>::quiet_NaN()
,这在现实生活中并不真正有用。
如果与 IEEE-754 所期望的变量进行比较,则比较>
和<
始终为 false 。quiet_NaN
c++ ×6
lua ×4
c++11 ×3
visual-c++ ×2
algorithm ×1
big-o ×1
c ×1
c++20 ×1
clang ×1
compiler-bug ×1
consteval ×1
googletest ×1
lambda ×1
mingw ×1
nan ×1
python ×1
recurrence ×1
recursion ×1
std-function ×1
wireshark ×1