小编Dei*_*Dei的帖子

GCC和Clang竖起大拇指 - Visual Studio旅行.逻辑错误?

无法将其分解为一个较小的例子......所以我用一个std::multimap来存储一些值...这是一个简单的多项式类.

问题是最后一个函数,它将两个多项式相乘.当它们相乘时,它会产生一个带有多个项的多项式,这些项可以相加在一起(2x^2 + 3x^2 => 5x^2).

所以我尝试这样做,Visual Studio抱怨(Debug Assertion Failed! Expression: map/set iterator not dereferencable).经过测试并与Clang和GCC一起工作正常.此外,它在发布模式下与Visual Studio完美配合,所以我认为这可能是我的错误或逻辑上的错误!所以我的问题是:以下代码是否有效且正确?如果没有,我该如何改进呢?请不要过多评论其他部分.

#include <iostream>
#include <cstddef>
#include <map>
#include <iterator>
#include <initializer_list>

//DIRTY YET SO BEAUTIFUL HACK       //don't judge the macro :P
#define EXPAND_PACK(FUN) \
        using type = int[]; \
        type{ 0, ((FUN), void(), 0)... }

template<class T>
class polynomial
{
private:
    struct _custom_compare //for std::multimap to sort properly
    {
        bool operator()(const std::size_t& a, const std::size_t& b) const
        {
            return …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

0
推荐指数
1
解决办法
65
查看次数

具有非完全整数类型的结构和枚举

现在我知道以下是如何工作的:

template<std::uint8_t num>
struct factorial 
{
    static_assert(num <= 20, "Integer overflow!!");
    enum { value = num * factorial<num - 1>::value };
};

template <>
struct factorial<0> 
{
    enum { value = 1 };
};
Run Code Online (Sandbox Code Playgroud)

它在编译时使用递归计算数字的阶乘!

您可以像这样调用此函数: std::cout << factorial<20>::value;

美丽,但enum只能保持整体类型(最多std::uintmax_t).这意味着我们无法计算大于20的数字的阶乘.

现在我写了一个很好的bigint小班,它消除了界限.

考虑以下:

template<std::uintmax_t num>
struct factorial
{
    static bigint value()
    {
        bigint result("1");

        for (bigint i("2"); i <= num; ++i)
            result *= i;

        return result;
    }
};
Run Code Online (Sandbox Code Playgroud)

这需要我这样称呼:std::cout << factorial<99>::value();.显然......因为它是一个功能.

我的问题是:有没有办法修改代码/函数,所以它会被调用,就像第一个例子,但仍然使用bigint …

c++ enums templates c++11

0
推荐指数
1
解决办法
76
查看次数

laravel 新项目 - 获取 /name 的索引而不是欢迎页面

我刚刚使用“composer create-project --prefer-dist laravel/laravel name 5.2.29”在 XAMPP 上创建了一个新的 laravel 项目。

我得到一个文件列表(索引/)而不是 laravel 欢迎页面。

在此输入图像描述

我该如何解决这个问题?

php laravel

0
推荐指数
1
解决办法
1万
查看次数

变量陷入If

我正在尝试学习C++,而我正在创建一些无用的应用程序来测试.我正在使用const char和参数,在这段代码中我无法获得标题字符串.

const char* title = "";
if (argc >= 3) {
    string tittle(argv[2]);
    title = tittle.c_str();
}
Run Code Online (Sandbox Code Playgroud)

请帮我!

c++ string if-statement const char

0
推荐指数
1
解决办法
47
查看次数

如何使用模板化 std::function 作为参数?

我想for_each为 2D-vector创建函数,但有一个错误:

error: no matching function for call to ‘each(std::vector<std::vector<int> >&, main()::<lambda(int&)>)’
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

#include <iostream>
#include <vector>
#include <functional>
using namespace std;

template <class VarType>
void each(vector<vector<VarType>> &ve, function<void (VarType &)> action) {
    for(vector<VarType> &v : ve) {
        for(VarType &p : v) {
            action(p);
        }
    }
}

int main() {
    vector<vector<int>> map(5);
    for(vector<int> &ve : map) {
        ve.resize(4);
    }

    each(map, [](int &val) {
        val = 1;
    });
}
Run Code Online (Sandbox Code Playgroud)

c++ templates

0
推荐指数
1
解决办法
38
查看次数

如何在扫描目录中的文件时跳过文件

我有一个程序列出目录中的所有文件.如果文件被断开链接,我想跳过该文件并继续扫描文件中的其他文件.如果有人能指出我错在哪里,我将非常感激.以下是我的代码的一部分

d = opendir(".");
while((dir = readdir(d)) != NULL) {

 char buff[256];
 int target = readlink (dir->d_name, buff, sizeof(buff));
    if (target == -1)
    {
        printf("i found broken link  so continuing to next file..\n");
        continue;
    }
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,当我只有一个断开的链接打印时

i found broken link  so continuing to next file
i found broken link  so continuing to next file
i found broken link  so continuing to next file
Run Code Online (Sandbox Code Playgroud)

并继续,直到最后一个文件.

c

-4
推荐指数
1
解决办法
268
查看次数

标签 统计

c++ ×4

c++11 ×2

templates ×2

c ×1

char ×1

const ×1

enums ×1

if-statement ×1

laravel ×1

php ×1

string ×1