车立方" type="application/atom+xml">

hahakubile Blog, Powered by 车立方

Welcome to hahakubile's blog, You should know him. Thanks to 车立方

Problem-printf Std String

问题

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <string>

using namespace std;

int main() {
   string a = "test";
   printf("%s/n", a);
}

编译错误

  • warning: cannot pass objects of non-POD type ‘struct std::string’ through ‘…’; callcall will abort at runtime
  • warning: format ‘%s’ expects type ‘char*’, but argument 2 has type ‘int’

原因

  • printf只能输出C语言内置的数据,而string不是内置的,只是一个扩展的类
  • &a代表的是这个字符串的存储地址,并不是指向字符串的首地址

解决

printf("%s\n", a.c_str());
  • const char *c_str();
  • c_str()提供了这样一种方法,它返回const char*类型(可读不可改)的指向字符数组的指针

About Static_cast

static_cast

static_cast<type> (object);

The static_cast operator can be used for operations such as:

  • Converting a pointer of a base class to a pointer of a derived class,
  • Convert numeric data types such as enums to ints or ints to floats.

示例

int score = 1340;

float scale = static_cast<float>(score) / MAX_SCORE + 1;

// 如果这样呢?会有什么不同
float scale = score / MAX_SCORE + 1;

需要注意的是,在基础类型转换时,并非进行四舍五入,而是直接舍去小数部分!

Avoid Assigning Negative Numbers to Unsigned Int|short

问题

// 定义了一个int变量i,i存在为负数的情况
int i = -1; 
// 又定义了一个unsigned short or int的变量j
unsigned short j;
// 如果把i的值赋给j
j = i; 

// 会发生什么?j为65535
std::cout << j << std::endl;

结论

这是一个简单的赋值,但由于前后未注意,导致这种bug发生,试想如果后面使用下面语句进行判断的话:

if(j > 0) {
  // 这里,j本应该是-1,但由于65535>0,导致错误
}

看到一句很有意思的话,You’re lying to the compiler.

Select All Columns Except Some in MySQL

问题

如果一个表有多列,要想不查询指定列(其余列都要),该如何实现?

在stackoverflow上有类似的问题,Select all columns except one in MySQL?

实现

SET @database    = 'database_name';
SET @tablename   = 'table_name';
SET @cols2delete = 'col1,col2,col3';

#If you do have a lots of cols, use this sql to change group_concat_max_len
SET @@group_concat_max_len = 2048;  

SET @sql = CONCAT(
'SELECT ', 
(
    SELECT GROUP_CONCAT( IF(FIND_IN_SET(COLUMN_NAME, @cols2delete), NULL, COLUMN_NAME ) )
    FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @tablename AND TABLE_SCHEMA = @database
), 
' FROM ',
@tablename);

SELECT @sql;

PREPARE stmt1 FROM @sql;
EXECUTE stmt1;

说明

涉及到以下内容:

  • 从INFORMATION_SCHEMA.COLUMNS表中获取COLUMNS名称
  • GROUP_CONCAT拼接在一起
  • FIND_IN_SET判断是否在排除的列当中
  • IF语句使用
  • Mysql prepare…execute用法

Problem-Running Weka-Registering Weka Editors Error

ubuntu下安装weka

sudo apt-get install weka

启动weka,控制台打印warning如下:

---Registering Weka Editors---
Trying to add JDBC driver: RmiJdbc.RJDriver - Error, not in CLASSPATH?
Trying to add JDBC driver: jdbc.idbDriver - Error, not in CLASSPATH?
Trying to add JDBC driver: org.gjt.mm.mysql.Driver - Error, not in CLASSPATH?
Trying to add JDBC driver: com.mckoi.JDBCDriver - Error, not in CLASSPATH?
Trying to add JDBC driver: org.hsqldb.jdbcDriver - Error, not in CLASSPATH?

问题原因

These are actually just warnings rather than true errors. The system has some default connection strings for various common databases and Weka does a quick check to see if the appropriate JDBC drivers are available to the system at startup. If you are not planning to connect to MySQL, Hypersonic, instantdb etc. then these messages can safely be ignored.

也就是说,这是weka的例行检查,如果不打算连接数据库,完全可以忽略这些warning信息。

解决办法

  • 下载对应的jar包(上面这些都是用于连接不同数据库)
  • 修改/usr/bin/weka启动脚本,添加JAVA_CLASSPTH(也可添加在~/.bashrc中)

参考[1]、[2]、[3]

1 2 3

Problem-SSH Permissions Denied: This Private Key Are Too Open

问题:

$ ssh -T git@github.com     
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0766 for '***/.ssh/id_rsa' are too open.
It is recommended that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: ***/.ssh/id_rsa
Permission denied (publickey).

解决方法:

cd ~/.ssh
chmod 700 id_rsa

参考:12

About IronSpread

IronSpread: Scripting Excel with Python

  • MIT的两名学生开发的Excel 2010 插件
  • 替代VBA
  • 允许用户在Excel中创建自动化的任务

相比之前使用python的插件完成excel清理,也许这会是个方便的选择。

@QucikStart