我有一个简单的程序,旨在存储一组C++ 17 std::filesystem::path对象.既然有std::filesystem::hash_value标准的一部分,为什么这个代码不能编译而不必我自己提供std::hash?
当我使用gcc 8.1.1编译和链接时,g++ -std=c++17 -NO_HASH=1 hashtest.cpp -o hashtest -lstdc++fs我的哈希函数被包含在内并且一切运行完美.但是,如果我将其更改为-NO_HASH=0,我会得到一个很长的错误消息列表,其中一个关键是:
usr/include/c++/8/bits/hashtable.h:195:21: error: static assertion failed: hash function must be invocable with an argument of key type
static_assert(__is_invocable<const _H1&, const _Key&>{},
Run Code Online (Sandbox Code Playgroud)
如果您想玩,这是一个现场Coliru版本.
真的没有定义std::hash<std::filesystem::path>吗? 我错过了什么?
对于那些对我为什么想要这样的东西感兴趣的人,就是这样:https://codereview.stackexchange.com/questions/124307/from-new-q-to-compiler-in-30-seconds
#include <optional>
#include <unordered_set>
#include <filesystem>
#include <string>
#include <iostream>
namespace fs = std::filesystem;
#if NO_HASH
namespace std {
template <>
struct hash<fs::path> {
std::size_t operator()(const …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用来自全系列可表示的正数的随机数来测试我创建的数学课float,但我发现我似乎在使用时遇到了问题std::random.这个计划
#include <random>
#include <iostream>
#include <functional>
template <typename T>
class Rand {
public:
Rand(T lo=std::numeric_limits<T>::min(),
T hi=std::numeric_limits<T>::max()) :
r(bind(std::uniform_real_distribution<>(lo, hi),std::mt19937_64{})) {}
T operator()() const { return r(); }
private:
std::function<T()> r;
};
int main()
{
Rand<float> f{};
const int samples = 1000000;
float min = std::numeric_limits<float>::max();
float max = std::numeric_limits<float>::min();
std::cout << "range min = " << max
<< ", max = " << min << '\n';
for (int i=0; i < samples; ++i) { …Run Code Online (Sandbox Code Playgroud) 我最近template specialization在C++中发现过.
template <typename T>
void fct(void) {}
template <>
void fct<int>(void) {}
int main(void) {
fct<int>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想对类中的成员函数使用模板专门化.
class MyClass {
public:
template <typename T>
static void fct(void) {}
template <>
static void fct<int>(void) {}
};
int main(void) {
MyClass::fct<int>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,编译时g++给出了以下错误:
error: explicit specialization in non-namespace scope ‘struct MyClass’
error: template-id ‘toto<int>’ in declaration of primary template
Run Code Online (Sandbox Code Playgroud)
我注意到,模板特化可以在主范围或命名空间中工作,但不能在结构或类中工作.
我在stackoverflow上发现了一些关于使用命名空间的内容,如下面的代码所示:
namespace myNameSpace {
template <typename T>
void fct(void) {}
template <> …Run Code Online (Sandbox Code Playgroud) 我正在使用C++ constexpr在编译时评估此代码可以正确评估的计算的Fibonacci序列的最大项.为此,我使用以下代码:
constexpr unsigned long long fibo(unsigned n) {
return (n < 2) ? 1 : fibo(n-1)+fibo(n-2);
}
constexpr unsigned max_fibo(unsigned n) {
return (fibo(n+1) >= fibo(n)) ? max_fibo(n+1) : n;
}
const unsigned MAX_FIBO_TERM = max_fibo(0);
Run Code Online (Sandbox Code Playgroud)
这很好,并快速构造MAX_FIBO_TERM(我的机器上的92)正确的值.但是,使用该fibo函数在运行时实际计算项是非常慢的(仅计算前48个项需要大约90秒).
我可以使用替代函数来计算更快的术语:
unsigned long long fiboiter(unsigned n, unsigned long long &prev,
unsigned long long &prevprev) {
return prev = (n < 2) ? prevprev = 1 :
(std::swap(prev, prevprev), prev + prevprev);
}
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法将其变为有效的constexpr功能.(这就是为什么它以那种奇怪的方式写的.)当我运行这个版本时,我的机器需要0.005秒.
当我用g++ …
我有一个文件以下列格式存储数据(这只是一个小样本):
AD,Andorra,AN,AD,AND,20.00,Andorra la Vella,Europe,Euro,EUR,67627.00
AE,United Arab Emirates,AE,AE,ARE,784.00,Abu Dhabi,Middle East,UAE Dirham,AED,2407460.00
AF,Afghanistan,AF,AF,AFG,4.00,Kabul,Asia,Afghani,AFA,26813057.00
AG,Antigua and Barbuda,AC,AG,ATG,28.00,Saint John's,Central America and the Caribbean,East Caribbean Dollar,XCD,66970.00
AI,Anguilla,AV,AI,AIA,660.00,The Valley,Central America and the Caribbean,East Caribbean Dollar,XCD,12132.00
Run Code Online (Sandbox Code Playgroud)
我想存储每行的第二个字段,以便我的数组只包含国家名称,如下所示:
string countryArray[] = {"Andorra,United Arab Emirates", "Afghanistan", "Antigua and Barbuda", "Anguilla"}
Run Code Online (Sandbox Code Playgroud)
但每次运行我的代码时,都会发生分段错误.这是我的代码:
countryArray[256];
if (myfile)
{
while (getline(myfile,line))
{
std::string s = line;
std::string delimiter = ",";
size_t pos = 0;
std::string token;
short loop=0;
while ((pos = s.find(delimiter)) != std::string::npos)
{
token = s.substr(0, pos);
s.erase(0, pos + delimiter.length()); …Run Code Online (Sandbox Code Playgroud)