我一直在使用std :: vector,并且想知道是否应该使用std :: map进行关键查找以提高性能。
这是我完整的测试代码。
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <ctime>
#include <chrono>
using namespace std;
vector<string> myStrings = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo", "ppp", "qqq", "rrr", "sss", "ttt", "uuu", "vvv", "www", "xxx", "yyy", "zzz"};
struct MyData {
string key;
int value;
};
int findStringPosFromVec(const vector<MyData> &myVec, const string &str) {
auto it = std::find_if(begin(myVec), end(myVec),
[&str](const MyData& data){return data.key == str;});
if (it == …Run Code Online (Sandbox Code Playgroud) 我发现我的应用程序有时会在lua_getglobal(L, "name");多次调用时崩溃.
我试过放置它lua_pop(L, 1);后lua_getglobal(L, "name");不再崩溃.
可以lua_getglobal(L, "name");多次调用导致内存泄漏?
有没有人知道为什么我的应用程序没有崩溃lua_pop(L, 1);?
我想t使用以下格式创建一个表。
t[uniqueID] = order
Run Code Online (Sandbox Code Playgroud)
该uniqueID会是唯一的,但order可以是相同或不同的各一次。
然后我想按升序对表格进行排序,以便我可以相应地打印出来uniqueID。
我的代码:
t = {}
function compare(a, b)
return a[2] < b[2]
end
function printid()
for k, v in pairs(t) do
print(k)
end
end
function main()
t[5] = 47
t[6] = 45
t[7] = 49
table.sort(t, compare)
printid()
end
Run Code Online (Sandbox Code Playgroud)
我得到的结果:
5
6
7
Run Code Online (Sandbox Code Playgroud)
我期望的结果:
6
5
7
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到我想要的结果?
我试图弄清楚如何通过指针访问前向声明的类的成员变量.
我的代码:
#include <iostream>
class Boo;
class Foo{
public:
int getNum() {
return booPtr->num; //Error: Member access into incomplete type 'Boo'
}
Boo *booPtr;
};
class Boo : public Foo {
public:
Boo() {
booPtr = this;
}
int num = 45;
};
int main() {
Boo boo;
int num = boo.getNum();
std::cout << num << '\n';
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
"return booPtr-> num":成员访问不完整类型'Boo'
结果我期待:
45
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个错误以及实现同样的事情会更常见,更安全的方法呢?
我试图在向量中找出最小值/最大值 pair<int, std::string>
我的代码:
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::pair<int, std::string>> pairs;
pairs = {{6,"a"}, {4,"b"}, {5,"c"}, {8,"d"}, {7,"e"}};
int min = 0, max = 0;
//how to find out min/max?
std::cout << "Minimum Number : " << min << '\n';
std::cout << "Maximum Number : " << max << '\n';
}
Run Code Online (Sandbox Code Playgroud)
我想要的结果:
Minimum Value : 4
Maximum Value : 8
Program ended with exit code: 0
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到我想要的结果?
编辑:
这是我迄今为止的解决方案.
std::sort(pairs.begin(), pairs.end());
min = pairs[0].first;
max …Run Code Online (Sandbox Code Playgroud) 我试图userdata从chunk AC++中的Lua脚本()(通过我的示例中的函数返回的变量)获取然后,然后从C++(通过我的示例中的函数参数)将其传递userdata回Lua脚本(chunk B),以便userdata可以chunk B像在原来一样使用chunk A.
MyBindings.h
class Vec2
{
public:
Vec2():x(0), y(0){};
Vec2(float x, float y):x(x), y(y){};
float x, y;
};
Run Code Online (Sandbox Code Playgroud)
MyBindings.i
%module my
%{
#include "MyBindings.h"
%}
%include "MyBindings.h"
Run Code Online (Sandbox Code Playgroud)
main.cpp
#include <iostream>
#include <lua.hpp>
extern "C"
{
int luaopen_my(lua_State *L);
}
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaopen_my(L);
lua_settop(L, 0);
/* chunk A */
luaL_dostring(L, "local vec2 = my.Vec2(3, 4)\n"
"function setup()\n"
"return …Run Code Online (Sandbox Code Playgroud) 我想按特定键的值对字典进行排序(示例中的“foo”)
mydict = dict()
mydict["a"] = {"foo": 0.3, "boo": 0.1}
mydict["b"] = {"foo": 0.1, "boo": 0.2}
mydict["c"] = {"foo": 0.8, "boo": 0.3}
mydict["d"] = {"foo": 0.5, "boo": 0.4}
mydict["e"] = {"foo": 0.2, "boo": 0.5}
# sort by "foo" in descending order
# expected result: [{"foo": 0.8, "boo": 0.3}, {"foo": 0.5, "boo": 0.4}, {"foo": 0.3, "boo": 0.1}, {"foo": 0.2, "boo": 0.5}, {"foo": 0.1, "boo": 0.2}]
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到上面评论中显示的我想要的结果?
我想在保留其类型的同时删除浮动剩余物.
(即3.14159f应该变成3.0f)
到目前为止,我能做的是两次铸造这种类型.
float f = 3.14159f;
float r = static_cast<float>(static_cast<int>(f));
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?或者有更简单的方法吗?
先感谢您.
我正在尝试从CPP文件访问其他头文件中的静态变量。
Boo.hpp”#ifndef Boo_hpp
#define Boo_hpp
static int num;
class Boo
{
public:
Boo()
{
num = 35;
}
};
#endif /* Boo_hpp */
Run Code Online (Sandbox Code Playgroud)
Foo.hpp”#ifndef Foo_hpp
#define Foo_hpp
class Foo
{
public:
Foo();
};
#endif /* Foo_hpp */
Run Code Online (Sandbox Code Playgroud)
Foo.cpp”#include "Foo.hpp"
#include "Boo.hpp"
#include <iostream>
Foo::Foo()
{
std::cout << "Num : " << num << '\n';
}
Run Code Online (Sandbox Code Playgroud)
main.cpp”#include <iostream>
#include "Foo.hpp"
#include "Boo.hpp"
int main()
{
Boo boo;
Foo foo; …Run Code Online (Sandbox Code Playgroud) 我不能math.pow(x,y)在Lua中使用。
我不知道为什么,它说语法错误。
math.sin(f)并且math.cos(f)有效。
math.pow()在Lua中还有其他选择吗?
这是我在头文件中的代码:
typedef struct _aaa
{
static void do_something(t_bbb *b); //this line is problematic
} t_aaa;
typedef struct _bbb
{
t_aaa *a;
} t_bbb;
Run Code Online (Sandbox Code Playgroud)
但它说:
未知的类型名称
t_bbb
如何使代码可编辑?
我从其他人编写的代码中发现了以下两个函数,但我并不真正理解这些函数的作用.
typedef union
{
t_float f; //float
unsigned int ui;
}t_bigorsmall32;
static inline int PD_BADFLOAT(t_float f) /* malformed float */
{
t_bigorsmall32 pun;
pun.f = f;
pun.ui &= 0x7f800000;
return((pun.ui == 0) | (pun.ui == 0x7f800000));
}
static inline int PD_BIGORSMALL(t_float f) /* exponent outside (-64,64) */
{
t_bigorsmall32 pun;
pun.f = f;
return((pun.ui & 0x20000000) == ((pun.ui >> 1) & 0x20000000));
}
Run Code Online (Sandbox Code Playgroud)
如果有人能解释他们的所作所为,我将不胜感激.我还想知道标准库或C++中是否有任何内置的替代函数.