小编And*_*dyG的帖子

找不到我的Python 3.3语法错误

我收到以下错误:

  File "foo.py", line 6
    print "This implementation requires the numpy module."
                                                         ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

在这个Python代码中:

#!/usr/bin/python

try:
    import numpy
except:
    print "This implementation requires the numpy module."
    exit(0)

###############################################################################

if __name__ == "__main__":
    R = [
         [1,2,3],
         [4,5,6]
        ]
Run Code Online (Sandbox Code Playgroud)

怎么了?

编辑:我使用Python 3.3

python python-3.x

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

从 std::exception 派生的类在 what() 上抛出无效指针错误

我试图从std::exception这样得出:

class bad_number : public std::exception
{
public:
    bad_number(const char*);
    virtual const char* what() const noexcept;
private:
    const char* num;
};

bad_number::bad_number(const char * num) : num(num){ }
const char* bad_number::what() const noexcept
{
    std::string str;
    str.append("Invalid number format:");
    str.append(num);
    char* result = new char[str.size()];
    strcpy(result, str.c_str());
    return result;
}

//The function that uses the exception
long double convert(const char *str)
{
    char *endptr;
    double result = strtold(str, &endptr);
    if (*endptr != '\0')
        throw bad_number(str);
    return result;
} …
Run Code Online (Sandbox Code Playgroud)

c++ exception

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

在派生类的构造函数体中调用基础构造函数

我有这个基类:

class BaseException
{
public:
    BaseException(string _message)
    {
        m_message = _message;
    }

    string GetErrorMessage() const
    {
        return m_message;
    }

protected:
    string m_message;
};
Run Code Online (Sandbox Code Playgroud)

和这个派生类

class Win32Exception : public BaseException
{
public:
    Win32Exception(string operation, int errCode, string sAdditionalInfo = "")
    {
        string message = "";
        message += "Operation \"" + operation + "\" failed with error code ";
        message += std::to_string(errCode);

        if (!sAdditionalInfo.empty())
            message += "\nAdditional info: " + sAdditionalInfo;

        BaseException(message);
    }
};
Run Code Online (Sandbox Code Playgroud)

编译器给我以下错误:

错误C2512:'BaseException':没有合适的默认构造函数可用

我知道我可以构建一个非常长的行来构造将在初始化列表中传递给基类的消息,但这种方式似乎更优雅.

我究竟做错了什么?

c++ inheritance constructor

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

使用 find 和 Union 检测图中的循环

    int main()
    {

    char line[100];
    int N = 5;
    vector<int>adj[N];
    FILE *in = fopen("test.txt", "r");

    for (int i = 1; i <= N; i++) // Accepting the graph from file
    {
        fgets(line, 100, in);

        char *pch = strtok(line, "\t \n");
        int u = atoi(pch);

        pch = strtok(NULL, "\t \n");
        while (pch != NULL)
        {
            int v = atoi(pch);
            adj[u-1].push_back(v);
            pch = strtok(NULL, "\t \n");
        }

    }
        for( int i = 0; i < 5; i++ )  // printing the graph …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm graph union-find

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

C++ 11 中带有选项 R 的正则表达式

我在 C++ 中有以下使用正则表达式的语句:

if (regex_match(argv[i], regex(R"((.*)\.o)")))
Run Code Online (Sandbox Code Playgroud)

我现在的问题是它是做什么的regex(R"((.*)\.o)")"?我知道外部 regex_match 函数检查是否argv[i]等于由逗号分隔的第二条语句。如果它们相等则计算结果为 true,如果不相等则计算结果为 false。

c++ regex

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

.Net 6 如何确定其环境

我创建了一个 .Net 6.0 控制台应用程序。我需要它能够使用实体框架迁移数据库。现在,当我运行它时,它说它的环境是“生产”。那是在哪里配置的呢?

在此输入图像描述

这是应用程序的启动代码。

    static void Main(string[] args)
    {
        using IHost host = Host.CreateDefaultBuilder(args)
            .UseSerilog((context, loggerConfiguration) => loggerConfiguration
                .ReadFrom.Configuration(context.Configuration)
                .WriteTo.Console())
            .ConfigureServices((context, services) =>
            {
                services.RegisterCoreServices(context.Configuration);
            })
            .Build();

        CallBatch(host.Services, args);

        host.RunAsync();
    }
Run Code Online (Sandbox Code Playgroud)

environment console-application .net-6.0

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

std::vector of structs:结构体的成员如何保存在内存中

我有以下课程:

struct xyzid{
  uint16_t i;
  uint16_t z,y,x;
};
      
std::vector<xyzid> example;

(...) // fill example

uint16_t* data = reinterpret_cast<uint16_t*>(example.data());
Run Code Online (Sandbox Code Playgroud)

我现在可以确定我的指针基本上是这样的:前 16 位在移动到下一个元素之前先data引用i、然后zy、吗?x或者,是否不能保证我的结构中的声明顺序保留在我的std::vector容器中?

c++ pointers

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

Python if/elif简化

这是我的代码的一部分:

for line in f:
    if animals[0].upper() in line:
        break
    elif animals[1].upper() in line:
        break
    elif animals[2].upper() in line:
        break
    elif animals[3].upper() in line:
        break
    elif animals[4].upper() in line:
        break
    elif animals[5].upper() in line:
        break
    elif animals[6].upper() in line:
        break
    elif animals[7].upper() in line:
        break
    elif animals[8].upper() in line:
        break
    elif animals[9].upper() in line:
        break
    elif animals[10].upper() in line:
        break
    print(line)
Run Code Online (Sandbox Code Playgroud)

我只想弄清楚它是否可以以某种方式简化,但我真的无法想出任何东西.有什么想法吗?

谢谢!

python if-statement simplification

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

如何在C++中创建一个数组,其长度等于用户输入?

我知道我不能创建这样的数组:

int main () {
    int length;
    std::cin >> length;
    int array [length] = {};
}
Run Code Online (Sandbox Code Playgroud)

有什么办法可以吗?

c++ arrays

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

创建一个包含名称列表的字典

我有这个名单: [Frank, Sam, Kevin, Jack]

是否可以使用列表中的名称创建一个字典来创建这样的东西?

'Frank' : {'Sam': 0, 'Kevin': 0, 'Jack': 0},
 'Sam' : {'Frank': 0, 'Kevin': 0, 'Jack': 0},
 'Kevin' : {'Frank': 0, 'Sam': 0, 'Jack': 0}
 'Jack' : {'Frank': 0, 'Sam': 0,  'Kevin': 0}
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以遍历列表,选择第一个名称然后用它创建一个字典,列表中的其他成员作为键,0作为默认值.然后重复列表中的其他元素.

我在考虑使用这样的东西.

my_dynamic_vars = dict()
my_dynamic_vars.update({string: dict()})
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激.

python string dictionary python-2.7 python-3.x

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