cout float的显示问题

Posted on 四月 3rd, 2009 in Linux, Program | No Comments »

在c++,使用cout直接显示float类型的数据,在不同系统上显示方式可能有所不同。
测试代码:
#include <iostream>

using namespace std;

int main()
{
float t=0.000000277;
cout<<t<<endl;
return 0;
}

在LINUX上:

2.77e-07

而在WINDOWS上结果如下:

2.77e-007

ubuntu下使用netcdf库

Posted on 三月 26th, 2009 in Linux, Program | No Comments »

netcdf自身带了C++接口,可是按照例子,怎么也连接不了,最后发现是要加参数:

g++ -lnetcdf_c++ -lnetcdf -o test *.cpp

c++的类模板

Posted on 十二月 16th, 2008 in Program | No Comments »

c++的类模板中有一些要注意的地方:
1、除非编译器实现了export关键字,否则将模板成员函数放置在一个独立的实现文件中将无法运行。(在GCC中,就是无法连接,编译可以通过)
因为模板不是函数,它们不能单独编译。模板必须与特定的模板实例化请求一起使用。

2、在类模板的操作符重载的友元函数中,要先声明,如<<操作符,要在函数后添加一对<>符合:

template <class T>
class Matrix;
template <class T>
ostream& operator <<(ostream& os,const Matrix<T>& matrix);

template <class T>
class Matrix
{
public:
friend ostream& operator << <>(ostream& os,const Matrix<T>& matrix);
}

而+-*/等操作符的友元重载则需要定义在类声明内部,否则连接会出错。

C# 2008:From Novice to Professional

Posted on 十二月 11th, 2007 in Program | No Comments »

Apress.Beginning.C.Sharp.2008.From.Novice.to.Professional.Nov.2007
书名:《C# 2008:From Novice to Professional》
作者:Christian Gross (Author)
出版商:Apress
发行日期:2007年11月
语言:英语
ISBN-10/ISBN-13:1590598695/978-1590598696

http://www.amazon.com/exec/obidos/tg/detail/-/1590598695/

book image

粗看了一遍,介绍的比较全面,感觉不是很深入。

生成UUID/GUID

Posted on 十一月 13th, 2007 in Program | 1 Comment »

php的生成办法:

<?php
class System
{
function currentTimeMillis()
{
list($usec, $sec) = explode(" ",microtime());
return $sec.substr($usec, 2, 3);
}

}

class NetAddress
{

var $Name = 'localhost';
var $IP = '127.0.0.1';

function getLocalHost() // static
{
$address = new NetAddress();
$address->Name = $_ENV["COMPUTERNAME"];
$address->IP = $_SERVER["SERVER_ADDR"];

return $address;
}

function toString()
{
return strtolower($this->Name.'/'.$this->IP);
}

}

class Random
{
function nextLong()
{
$tmp = rand(0,1)?'-':'';
return $tmp.rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);
}
}

// 三段
// 一段是微秒 一段是地址 一段是随机数
class Guid
{

var $valueBeforeMD5;
var $valueAfterMD5;

function Guid()
{
$this->getGuid();
}
//
function getGuid()
{
$address = NetAddress::getLocalHost();
$this->valueBeforeMD5 = $address->toString().':'.System::currentTimeMillis().':'.Random::nextLong();
$this->valueAfterMD5 = md5($this->valueBeforeMD5);
}

function newGuid()
{
$Guid = new Guid();
return $Guid;
}

function toString()
{
$raw = strtoupper($this->valueAfterMD5);
return substr($raw,0,8).'-'.substr($raw,8,4).'-'.substr($raw,12,4).'-'.substr($raw,16,4).'-'.substr($raw,20);
}

}
?>

然后就可以这样调用:

1. <?php
2. require_once("guid.class.php");
3. $Guid = new Guid();
4. print $Guid->toString();
5. ?>

linux下可以用uuidgen命令来生成UUID,也可以在c/c++程序里调用:

/* clear.c */
void uuid_clear(uuid_t uu);

/* compare.c */
int uuid_compare(const uuid_t uu1, const uuid_t uu2);

/* copy.c */
void uuid_copy(uuid_t dst, const uuid_t src);

/* gen_uuid.c */
void uuid_generate(uuid_t out);
void uuid_generate_random(uuid_t out);
void uuid_generate_time(uuid_t out);

/* isnull.c */
int uuid_is_null(const uuid_t uu);

/* parse.c */
int uuid_parse(const char *in, uuid_t uu);

/* unparse.c */
void uuid_unparse(const uuid_t uu, char *out);

/* uuid_time.c */
time_t uuid_time(const uuid_t uu, struct timeval *ret_tv);
int uuid_type(const uuid_t uu);
int uuid_variant(const uuid_t uu);

在ubuntu下,需要确认已经安装对应的库:

sudo aptitude install libuuid1 uuid-dev

c++中如何创建目录

Posted on 十一月 10th, 2007 in Program | No Comments »

c++中读写文件直接使用fstream就可以操作,但如何对目录进行操作?
一番搜索下来后,发现std库并不能处理目录的操作,需要使用额外库来进行操作。
windows平台下:

#include <windows.h>

CreateDirectory (char *DirName, SECURITY_ATTRIBUTES Attribs);

linux平台下:

#include <sys/stat.h>

mkdir (const char *path, mode_t mode);

一个例子
#include <sys/types.h>
#include <sys/stat.h>

int status;
...
status = mkdir("/home/cnd/mod1", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

还可以使用boost的跨平台方案:

#include "boost/filesystem.hpp" // includes all needed Boost.Filesystem declarations
#include <iostream> // for std::cout
using boost::filesystem; // for ease of tutorial presentation;
// a namespace alias is preferred practice in real code
create_directory( "foobar" );

BTW: boost filesystem在g++下的编译方法:

g++ -lboost_filesystem ...

c++中的string转换

Posted on 九月 20th, 2007 in Program | No Comments »

如何转换string到其他类型?如何从其他类型转换到string?
代码1:toString
template <class T> std::string toString(const T& a)
{
std::ostringstream ost;
ost << a;
return ost.str();
}

代码2:fromString
template<typename T>T fromString( const std::string& s)
{
std::istringstream is(s);
T t;
is >> t;
return t;
}

使用方法:
int d = fromString<int>( s );
string str=toString(d);

参考:

http://www.thescripts.com/forum/thread62219.html

使用FileChooserDialog打开多文件

Posted on 九月 12th, 2007 in Program | 2 Comments »

单文件比较简单,
看到这个函数的原型是这样的:
Glib::SListHandle<Glib::ustring> Gtk::FileChooser::get_filenames ( ) const
因此,就以为要使用这样的变量来进行声明:
Glib::SListHandle<Glib::ustring> filenames =dialog.get_filenames();
for(int i=0;i<filenames.size();i++)
{
//Notice that this is a std::string, not a Glib::ustring.
//std::string filename = dialog.get_filename();
std::cout << "File selected: " << filenames[i] << std::endl;
}

结果发现编译不通过,再仔细看看,Glib::SListHandle并没有重载[]操作符。
再网络上搜索后发现有人和我的问题相同:

That looks like a case where the GLib documentation was automatically
converted to glibmm documentation, but it doesn't really make sense.
SListHandle and ListHandle types are 'intermediate' types that you
shouldn't ever really need to use. They just provide handy
conversions to any of the standard containers. So, you should just be
able to use either std::list<> or std::vector<> or something like
that. So something like the following:

std::list<Glib::ustring> filenames = chooser.get_filenames();

而且,紧跟的一个邮件说明需要使用std::string来声明,就是应该这样:
std::vector<std::string> filenames=chooser.get_filenames();
恩,这样就清楚了。

参考:http://mail.gnome.org/archives/gtkmm-list/2007-May/msg00178.html

c++ compile error: discards qualifier

Posted on 六月 9th, 2007 in Program | No Comments »

在调试matrix类的时候,遇到了这个错误。

参考这儿知道这个错误的原因了:

discards qualifier

1. Example myfile.cpp: In function `int main()': myfile.cpp:20: passing `const DayOfYear' as `this' argument of `void DayOfYear::Set(int, int)' discards qualifiers
2. Meaning
You have an inconsistency with the use of "const"
3. Usual Causes
1. A non-const member function is being invoked for a const object
2. A const object is being passed as a non-const parameter
3. A const member function calls a non-const member function

应该是const的问题,要么是应该是const而没有进行const,或不是const而进行了const。
代码里这两个问题都遇到了。

这儿是修改后的代码,原来的代码都没有进行const定义。

T operator ()(const unsigned long row, const unsigned long col) const;

Matrix<T> operator ~();

Matrix<T>& operator =(const Matrix<T>& rmatrix);

Matrix<T>& operator= (const T& scalar);

Matrix<T> operator -(const T a);

Matrix<T> operator -(const Matrix<T>& matrix);

Matrix<T>& operator -=(const T a);

Matrix<T>& operator -=(const Matrix<T>& matrix);

之前的编译错误为:

includes/matrix.hpp:1126: error: passing 'const ldas::Matrix' as 'this' argument of 'T ldas::Matrix::operator()(long unsigned int, long unsigned int) [with T = double]' discards qualifiers

若在中文LOCALE下,错误为:

includes/matrix.hpp:1147: 错误: 将 ‘const ldas::Matrix’ 作为 ‘ldas::Matrix ldas::Matrix::operator-() [with T = double]’ 的 ‘this’ 实参时丢弃了类型限定

最终,在调试了对应的const定义后,代码己可以在gcc/g++ 4.1.2下进行编译了。

C++的++重载

Posted on 十一月 30th, 2005 in Program | No Comments »

++重载主要要考虑到前增量和后增量的区别。
class A
{
int t;
public:
A& operator++(); //前缀
A operator ++(int); //后缀
}
A& A::operator++()
{
++t;
return *this;
}
A A::operator++(int)
{
A a=*this;
++t;
return a;
}