boost::hash 具有大多数内置类型(包括容器)的散列函数.
但正如boost::hash_range函数描述中所述,范围的哈希算法
对元素的顺序很敏感,因此将它与无序容器一起使用是不合适的
因而,没有boost::hash专业化std::unordered_map,也没有boost::unordered_map.
问题是:
如果unordered_map没有从头开始重新实现哈希算法,是否有"简单而有效"的方法来哈希?
我正在编写一些代码来集成ODE.这个问题同样是对解决方案的编码建议的要求,所以如果您对我即将提供的建议有其他建议,请告诉我!
由ODE集成商集成的"对象"以6个"块"为单位......原因是我有一个std :: vector of double,它们按以下方式排列:
前3个双打是位置坐标; x,y和z.接下来的3个双打是速度坐标; x,y和z.
那么,现在你知道了,我有一个函数,它将成对的"位置" """向量"""作为参数并返回某种结果......看看我要去哪里?
目前,该函数需要以下列方式使用2个位置坐标:
std::vector<double> magic_gravity_formula(const std::vector<double> &r1,
const std::vector<double> &r2, const double m1, const double m2)
Run Code Online (Sandbox Code Playgroud)
我不想将3组中的所有数据复制到新的向量 - 这是一种疯狂(并且非常慢)的编程方式.
我可以使用指向原始数据的指针......并且只是将指针传递给x坐标(3个双打块中的第一个项目) - 这对我来说似乎没有问题,但也许有更好的方法?有点像Python或Matlab数组切片?我可以这样做吗?
我想传递一个新的向量(或某种包装类?),从已存储在数组中的数据创建...有点像
std::vector<double> sub_section_of_data = data[0..2] // Obviously pseudocode!
Run Code Online (Sandbox Code Playgroud)
好的,所以上面的内容是荒谬的,因为大概是一种实现该语法的语言仍然可以进行复制操作,这可能很慢 - 正是我想要避免的...
所以,是的,我不确定这里最好的方式 - 有人能建议一个"好"的解决方案吗?(以非主观的方式!)
编辑:为了使这一点非常清楚 - 问题是我不想做类似的事情:
std::vector<double> r1_temp;
r1_temp.push_back(data[0]); // Copy ! Bad !
r1_temp.push_back(data[1]);
r1_temp.push_back(data[2]);
... same for an r2 ...
std::vector<double> force = magic_gravity_formula(r1, r2, m1, m2);
Run Code Online (Sandbox Code Playgroud)
编辑2:考虑编译器选项 - 编译器是否会通过以下方式更改函数以接受参数来为我优化我的代码:
std::vector<double> super_gravity_formula(double x1, …Run Code Online (Sandbox Code Playgroud) 假设我有这个代码:
#include <iostream>
#include <unordered_map>
#include <functional>
using const_string_ref = std::reference_wrapper<const std::string>;
namespace std
{
template<>
struct hash<const_string_ref>
{
size_t operator()(const const_string_ref& ref) const
{
return std::hash<std::string>()(ref);
}
};
bool operator==(const const_string_ref& lhs,
const const_string_ref& rhs)
{
return (lhs.get() == rhs.get());
}
}
class test
{
public:
void process(const std::string& action)
{
(this->*(ACTIONS_PROCESSORS_MAP_.at(action)))();
}
private:
using action_processor = void (test::*)();
using actions_map = std::unordered_map<const_string_ref, action_processor>;
private:
static const std::string FIRST_KEY_;
static const std::string SECOND_KEY_;
static const actions_map ACTIONS_PROCESSORS_MAP_;
private:
void …Run Code Online (Sandbox Code Playgroud) 我正在启动一个进程,并希望检查其退出代码是否成功.
Process myProcess = new Process();
myProcess.Start();
myProcess.WaitForExit();
// What should i put instead of EXIT_SUCCESS ?
if (myProcess.ExitCode == EXIT_SUCCESS)
{
// Do something
}
Run Code Online (Sandbox Code Playgroud)
EXIT_SUCCESS 似乎不存在,C#中是否存在等效或规范的方式来检查零?
const num = 42
const str = `My number is ${num}`
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我对转换num为 a有什么保证string?
是保证只调用它的toString()方法还是可以用另一种方式进行转换?
我有一个算法,我想翻译我的代码,而不是使用数组,我想使用矢量.
你会如何翻译这个:( b + j和a的一面)
find_kth(a, b + j, i, size_b - j, k - j);
Run Code Online (Sandbox Code Playgroud)
哪里
int find_kth(int a[], int b[], int size_a, int size_b, int k);
Run Code Online (Sandbox Code Playgroud)
成
int find_kth(const vector<int>& a, const vector<int>& b, int size_a, int size_b, int k);
Run Code Online (Sandbox Code Playgroud)
它必须是等效的,所以像这样的调用返回相同的值,就像我使用数组一样:
min(a[0], b[0]);
Run Code Online (Sandbox Code Playgroud) 这个open-std文档表明std :: function支持自定义分配器,但是我无法在互联网上找到任何关于如何提供自定义分配器的常用参考.
我的研究倾向于让我相信自定义分配器已经实现boost::function但std::function尚未实现.
所以问题是std::function在C++ 11中使用自定义分配器吗?如果没有,它最有可能支持C++ 14中的一个?
#include <stdio.h>
#include <stdlib.h>
void main ()
{
system("dir");
}
Run Code Online (Sandbox Code Playgroud)
我已阅读过许多system()用于执行命令行程序的C++书籍.但是当我在C中尝试相同的命令时,它工作得很好.所以我的问题是它是标准的C或C++函数吗?请自由,因为我是C编程的新手.
#include<stdio.h>
int main()
{
char *p = NULL;
char str[] = "How do you do!!!!!";
p = &str;
printf("String is:%s",p);
p = "HELLO HOW ARE YOU";
printf("String is:%s",p);
printf("Hello");
int a = 10;
int *pa;
pa = &a;
printf("Contents of a is %d\n",a);
printf("Contents of pa is %x\n", (int)pa);
printf("Values of variable pointed to by pa is %d\n", *pa);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在参考上面的代码时:
p = &str;
printf("String is:%s",p);
Run Code Online (Sandbox Code Playgroud)
p将显示存储的字符串.
pa = &a;
printf("Values of variable pointed to by pa is …Run Code Online (Sandbox Code Playgroud) c++ ×6
array-view ×2
c ×2
c++11 ×2
pointers ×2
vector ×2
.net ×1
allocator ×1
arrays ×1
boost ×1
c# ×1
c++14 ×1
dereference ×1
exit-code ×1
hash ×1
javascript ×1
process ×1
slice ×1
std-function ×1
tostring ×1