小编Fil*_*efp的帖子

将字符串转换为具有规范的列表

我想在python中创建一个我的字符串列表,它会显示字母在字符串中连续显示多少次.例如:

my_string= "google"
Run Code Online (Sandbox Code Playgroud)

我想创建一个如下所示的列表:

[['g', 1], ['o', 2], ['g', 1], ['l', 1], ['e', 1]]
Run Code Online (Sandbox Code Playgroud)

谢谢!

python string list

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

C++ 11线程编译错误,删除了复制构造函数和std :: thread,为什么?

这会发出大约100行错误.错误:使用run_me复制构造函数的已删除函数等.那么,这里的问题是什么?

#include<thread>
#include<iostream>  
#include<vector>
#include<fstream>
#include<string>
#include<chrono>
using namespace std;
//creating thread to print line from file
class run_me {
    ifstream fs;
    public:
    string s;
        run_me(): fs("lliftOff.cpp",ifstream::in) {}
    void operator()(){
        cout<<"hi";
        while(getline(fs,s) ) {
            cout<<s<<endl;
        this_thread::sleep_for (chrono::seconds(1));
        }
    }
    ~run_me() {
        fs.close();
    }
};
int main() {
    thread t1{run_me()};
t1.join();
cout<<"shutting down now"<<endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

伪析构函数名称对非类和非枚举类型有意义吗?

引用了N3797的 5.2.4/1 ,C++ 14最终工作草案:

在点之后使用伪析构函数名称.或箭头 - >运算符表示由type-name或decltype-specifier表示的非类型的析构函数.


  • 对于我们可以考虑的类型pseudo-destructor-name,它只是枚举类型吗?

c++ language-lawyer

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

由于错误的strcmp参数处理而生成警告

所以我有一个

unsigned char * pMyPtr 
Run Code Online (Sandbox Code Playgroud)

分配给某事.

然后我想将它与任意字符串进行比较

strcmp(const char* , const char* )
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,clang编译器告诉我

warning: passing (aka 'unsigned char *') to parameter of type 'const char *' converts between pointers to integer types with different sign

如何删除此警告?

c c++ clang

2
推荐指数
2
解决办法
288
查看次数

编写一个函数的两个版本,一个用于"清晰度",一个用于"速度"

我的教授分配了一个函数来编写一个函数,该函数接受一个整数数组并将所有零排序到数组的末尾,同时保持当前非零整数的顺序.限制是:

不能使用STL或其他模板化容器.必须有两个解决方案:一个强调速度,另一个强调清晰度.

我写了这个尝试速度的函数:

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

void sortArray(int array[], int size)
{
    int i = 0;
    int j = 1;
    int n = 0;
    for (i = j; i < size;)
    {
        if (array[i] == 0)
        {
            n++;
            i++;
        }
        else if (array[i] != 0 && j != i)
        {
            array[j++] = array[i++];
        }
        else
        {
            i++;
            n++;
        }
    }
    while (j < size)
    {
        array[j++] = 0;
    }
}

int main()
{
    //Example …
Run Code Online (Sandbox Code Playgroud)

c++

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

如何将字符串列表转换为字符串中各个字符的子列表列表?

所以我开始:

['BLUE', 'ORANGE', 'YELLOW', 'GREEN', 'BLACK', 'PURPLE', 'BROWN']
Run Code Online (Sandbox Code Playgroud)

而且我要:

[['B', 'L', 'U', 'E'], ['O', 'R', 'A', 'N', 'G', 'E'], ['Y', 'E', 'L', 'L', 'O', 'W'], ['G', 'R', 'E', 'E', 'N'], ['B','L', 'A', 'C', 'K'], ['P', 'U', 'R', 'P', 'L', 'E'], ['B', 'R', 'O', 'W','N']]
Run Code Online (Sandbox Code Playgroud)

我试过这个,因为看起来它会产生我想要的东西,但它一直告诉我,我有一个错误.AttributeError:'NoneType'对象没有属性'append':

first_list = ['BLUE', 'ORANGE', 'YELLOW', 'GREEN', 'BLACK', 'PURPLE', 'BROWN']
list_1 = []

for i in range (len(first_list)):
    list_1 = list_1.append(list(first_list[i]))

return list_1
Run Code Online (Sandbox Code Playgroud)

我一直在使用".append"时遇到麻烦,并在其他迭代中继续使用"+"但这只是给了我一个很长的所有字符列表,而不是一个字符子列表列表.感谢您的任何帮助或建议.

python list append sublist

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

用于函数定义的#if的用法

我需要知道#if定义的用法是否正确,并且知道以这种方式使用它的可能缺点.

#if defined TEST
int foo()
{
   return 0;
}
int foo1()
{
   return 0;
}
#else
int foo()
{
   return 1;
}
int foo1()
{
   return 1;
}
#endif
Run Code Online (Sandbox Code Playgroud)

编辑:我正在尝试将此代码用于我正在处理的两个不同平台.

c conditional-compilation

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

扩展通用

我有一个抽象的课

public abstract class Integrator {
    public abstract Point integrate();
    public abstract Point function(Double x, Point y);
}
Run Code Online (Sandbox Code Playgroud)

延长了

public abstract class Euler extends Integrator {
    public Point integrate() {
        ... // this calls function(x, y)
    }
}

public abstract class Central extends Integrator {
    public Point integrate() {
        ... // this calls function(x, y)
    }
}
Run Code Online (Sandbox Code Playgroud)

两者的实施方式integrate()不同.现在,我实例化的具体类是这样定义的

public class EulerIVP extends Euler {
    public EulerIVP(...) { ... }

    public Point function(Double x, Point y) {
        ...
    } …
Run Code Online (Sandbox Code Playgroud)

java generics polymorphism inheritance

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

使用QApplication :: applicationDirPath调用时,调用重载'arg(QString(&)())'是不明确的,为什么?

QString msgText = QString("The file has been saved at %1\sysconf.xml").arg(QApplication::applicationDirPath);
Run Code Online (Sandbox Code Playgroud)

给我上面的错误.我.arg()之前使用过,所以我想知道它为什么会给我这个错误?.arg()我的代码中的所有其他代码都正常工作.

c++ qstring qt

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

为什么我的if语句跳过了else?

我正在写一个计算代码行的小程序.以下是一行代码的定义: - 包含程序运行所需代码的任何行. - 空白行不是一行代码. - 评论不是一行代码. - 如果在同一行之后有代码和注释,那么它也很重要.

所以我有这段代码(简单的if语句):

found = lineRead.find("/*");
if(found != string::npos)
{
    found = lineRead.find("*/");
    if(found != string::npos)
        inComment = false;
    else
        inComment == true;
}
return inComment;
Run Code Online (Sandbox Code Playgroud)

假使,假设

String lineRead = "cout<<\"helloworld!\";/*blockcomment"
Bool inComment (is true if the previous line didn't have end block comment token)
Run Code Online (Sandbox Code Playgroud)

所以我的程序到达第一个if语句,因为它/*在那一行找到,查找*/,转到第二个if语句然后直接跳转到return语句而不改变inComment(应该设置为true因为下一行的文本仍然是在块内注释).

谁知道为什么会这样?

c++ string if-statement counting

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