小编Bjö*_*lex的帖子

string.replace('the','')正在留下空格

我有一个字符串,它是我从MP3 ID3标签获得的艺术家的名字

sArtist = "The Beatles"
Run Code Online (Sandbox Code Playgroud)

我想要的是改变它

sArtist = "Beatles, the"
Run Code Online (Sandbox Code Playgroud)

我遇到了两个不同的问题.我的第一个问题是我似乎在为''换取''.

if sArtist.lower().find('the') == 0:
    sArtist = sArtist.lower().replace('the','')
    sArtist = sArtist + ", the"
Run Code Online (Sandbox Code Playgroud)

我的第二个问题是因为我必须检查"The"和"the"我使用sArtist.lower().然而,这将我的结果从"甲壳虫乐队"改为"披头士乐队".为了解决这个问题,我刚刚删除了.lower并添加了第二行代码来明确查找这两种情况.

if sArtist.lower().find('the') == 0:
    sArtist = sArtist.replace('the','')
    sArtist = sArtist.replace('The','')
    sArtist = sArtist + ", the"
Run Code Online (Sandbox Code Playgroud)

所以我真正需要解决的问题是为什么我用' <SPACE>而不是'代替'' <NULL>.但如果有人有更好的方法来做到这一点我会很高兴教育:)

python string mp3 id3

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

如何初始化GLfloat数组?

我想在我创建的项目中添加一些照明,但是我得到以下编译器错误:

error C2440: 'initializing' : cannot convert from 'float' to 'GLfloat []' 
Run Code Online (Sandbox Code Playgroud)

问题是什么?

GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);


GLfloat lightColor0[] = (0.5f, 0.5f, 0.5f, 1.0f);
GLfloat lightPos0[] = (4.0f, 0.0f, 8.0f, 1.0f);

glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightPos0);

GLfloat lightColor1[] = (0.5f, 0.2f, 0.2f, 1.0f);
GLfloat lightPos1[] = (-1.0f, 0.5f, 0.5f, 0.0f);

glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightPos1);
Run Code Online (Sandbox Code Playgroud)

c c++

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

如何快速提取这三个数字?

我有一个std::string包含3个数字,用空格分隔,例如:123334 33335 54544.如何快速提取这三个数字?

int r = ... /* 123334 */
int g = ... /* 33335 */
int b = ... /* 54544*/
Run Code Online (Sandbox Code Playgroud)

c++ string parsing stl

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

将指针传递给结构

嗨,我有一个函数A ( xy * abc),指向结构的指针.

typedef struct
{
int a;
char * b;
} xy;

typedef struct
{
xy c;
xy d;
} uv;

uv *sha;
Run Code Online (Sandbox Code Playgroud)

如果我需要调用函数Acd使用uv,我应该如何传递参数?我A用这个来调用函数:

A (&sha->c);
A (&sha->d);
Run Code Online (Sandbox Code Playgroud)

这个电话是否正确?

请帮助我

c c++ pointers structure

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

多维动态矩阵我该如何创建一个?

我在Ubuntu上使用C++.我想知道如何创建一个动态长度的矩阵?

我做到了这一点:int matrix[12][]但它不起作用.列的维度将在我的代码中逐步增加.有人可以在示例中向我显示正确的代码吗?

c++

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

如何在c ++中创建锯齿状的字符串数组?

我想在c ++中创建锯齿状的二维数组.

int arrsize[3] = {10, 5, 2};
char** record;
record = (char**)malloc(3);
cout << endl << sizeof(record) << endl;
for (int i = 0; i < 3; i++) 
{
    record[i] = (char *)malloc(arrsize[i] * sizeof(char *));
    cout << endl << sizeof(record[i]) << endl;
}
Run Code Online (Sandbox Code Playgroud)

我想设置record[0]名称(应该有10个字母),record[1]标记(应该有5位数字)和record[3]Id(应该有2位数字).我该如何实现呢?我直接将记录数组写入二进制文件.我不想用structclass.

c++ jagged-arrays

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

读取.txt中所有大写或小写Python 2.7.1的所有行?

我正在尝试读取所有上部或全部小写的文件行.

如果file.txt包含:

Rememberr 8? when you
. Tellingmee THAT one
didntrememberthat
onethingtoday
Run Code Online (Sandbox Code Playgroud)

我希望它能读到:

didntrememberthat
ONETHINGTODAY
Run Code Online (Sandbox Code Playgroud)

到目前为止,我有:

def function(file_name):
    data=[]
    f=open(file_name,'r')
    lines=f.readlines()
    for k in lines:
        single=tuple(k)
        for j in single:
            j=str(j)
            if j.isupper() or j.islower() == False:
            lines=f.readlines()[j:]
Run Code Online (Sandbox Code Playgroud)

然后我明白了:

lines=f.readlines()[j:]
TypeError: slice indices must be integers or None or have an __index__ method
Run Code Online (Sandbox Code Playgroud)

这是有道理的,因为j它不是一个整数.但是,j当我遇到if-statement 时,我怎么能找到它的位置?

如果有一种更简单的方法可以做到这一点非常棒

python element skip lines

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

如何用条件运算符表示三种情况?

是否可以使用条件运算符表达案例?例如 :

 if(rval==1)
     DO THIS 1;
 else if(rval==2)
     DO THIS 2;
 else if (rval ==3)
     DO THIS 3;
Run Code Online (Sandbox Code Playgroud)

c c++ conditional-operator

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

找到平方根到精度

我刚刚接受了各公司在采访中提出的问题.我发现一个是"找到一个精确数字的平方根.函数定义应该是这样的:double getSquareRoot(int num, int precision)".

我写了一个小函数,它给出了平方根,但不关心精度:

double getSquareRoot(int num){
 int num1=0, num2=0;
 for(int i=1 ;; i++){
   if(i*i == num){
    std::cout<<i <<" is the sq root"<<std::endl;
    break;
   }
  else if(i*i > num){
   num2 = i;
   num1 = --i;
   break;
  }
} 
 // in the above for loop, i get the num1 and num2 where my input should lie 
 // between them
 // in the 2nd loop below.. now i will do the same process but incrementing 
 // by 0.005 …
Run Code Online (Sandbox Code Playgroud)

c++ performance

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

复制构造函数未调用

#include <iostream>


int main(void)
{

class date {
private:
  int day;
  int month;
  int year;
public:
  date( )  { std::cout << "default constructor called" << std::endl; }
  date& operator=(const date& a) { std::cout << "copy constructor called" << std::endl; day=a.day; month=a.month; year=a.year; }
  date(int d ,int m ,int y  ) : day(d),month(m),year(y){ std::cout << "constructor called" << std::endl; }
  void p_date(){ std::cout << "day=" << day << ",month=" << month  << ",year=" << year << std::endl; }
  date& add_day(int d) …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor

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