Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿

本文主要涉及boost中algorithm之string问题
基本的字符串函数类型有
替换
裁剪
大小写替换
正则表达式
切割
判断
和擦除操作等等
//! boost之2:字符串算法类 
#include <string>
#include 
<vector>
#include 
<iostream>
#include 
<iterator>
#include 
<functional>
#include 
<boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main()
{  
    
//! 字符串 
    string str(" abc-*-ABC-*-aBc ");
    
    
//! 以-或者*切割字符串 
    vector<std::string> ret;
    split(ret,str,is_any_of(
"-*"),token_compress_on);

    
for(unsigned int index=0;index<ret.size();index++)
    {
        cout
<<index<<":"<<ret[index]<<endl;
    };
    
    
//! 切换为小写
    to_lower(str); 
    cout
<<"*"<<str<<"*"<<endl;
    
//! 去掉左边的空格
    trim_left(str);
    cout
<<"*"<<str<<"*"<<endl;
    
//! 去掉右边的空格
    trim_right(str);
    cout
<<"*"<<str<<"*"<<endl;
    
    
//! 替换
    replace_all(str,"a","A"); 
    cout
<<str<<endl;
    
//! 擦除 
    cout<<erase_all_copy(str,"A")<<endl;
    
    replace_nth(str,
"c",2,"ccsdu2004"); 
    cout
<<str<<endl;
    
    
//! 擦除给定范围
    cout<<erase_range_copy(str,make_iterator_range(str.begin()+2,str.begin()+5))<<endl; 
    
    cout
<<"is start with:A:"<<starts_with(str,string("A"))<<endl;
    cout
<<"is end with:C:"<<ends_with(str,string("C"))<<endl;
    cout
<<"is contain with:ccs:"<<contains(str,string("ccs"))<<endl;
    
    cout
<<endl;
    system(
"PAUSE");
    
return 0;
}


posted @ 2010-08-10 10:11 ccsdu2009 阅读(624) | 评论 (0)编辑 收藏
 
发一系列关于boost库的使用文章以备使用
//! boost库引导1.minmax 
#include <list>
#include 
<algorithm>
#include 
<cstdlib>
#include 
<iostream>
#include 
<boost/algorithm/minmax.hpp>
#include 
<boost/algorithm/minmax_element.hpp>

using namespace std;
inline 
void Print(int i)
{
    std::cout
<<i<<std::endl;  
}

inline 
int Rand()
{
    
return rand()%10;       
}

int main()
{
    list
<int> l;
    typedef list
<int>::const_iterator iterator;
    
//! 使用给定泛函子生成12个随机数并推入链表 
    generate_n(front_inserter(l),12,Rand);
    std::for_each(l.begin(),l.end(),Print);
    std::cout
<<"list size is:"<<l.size()<<std::endl;
    
//! 获取给定序列的对大最小值,返回为std::pair<..,..> 
    pair<iterator,iterator> result = boost::minmax_element(l.begin(),l.end());
    cout
<<"the smallest element is:"<<*(result.first)<<endl;
    cout
<<"the largest element is:"<<*(result.second)<<endl;
    
//! 获取第一个最小元素 
    iterator minitr = boost::first_min_element(l.begin(),l.end());
    cout
<<"first min element is:"<<*minitr<<endl;
    system(
"PAUSE");
    
return 1;
}

 
//! 后论:似乎没看到什么强大的功能
//!ccsdu2004

posted @ 2010-08-09 13:31 ccsdu2009 阅读(649) | 评论 (1)编辑 收藏
 
如下:
int Parse(int argc,char* argv[])
{
    
if(argc < 2)
    {
        cout
<<"need para."<<endl;
        system(
"PAUSE");
        
return -1;
    }

    
bool is_input_file_para = false;
    
bool is_output_file_para = false;

    
for(int i = 1; i < argc;i++)
    {
        
if(strcmp(argv[i],"-read"== 0)
        {
            is_input_file_para 
= true;
            is_output_file_para 
= false;
        }
        
else if(strcmp(argv[i],"-write"== 0)
        {
            is_input_file_para 
= false;
            is_output_file_para 
= true;
        }
        
else
        {
            
if(is_input_file_para == true)
            {
                input_file_name 
= argv[i];
                is_output_file_para 
= true;
            }
            
else if(is_output_file_para == true)
            {
                output_file_name 
= argv[i];
                is_input_file_para 
= true;
            }
        }
    }
    
return 1;
}

该函数解析命令行获取输入文件和输出文件名字
使用方法为:
srilm_lm -read gonewiththewind.count -write gonewiththewind.lm
其中srilm_lm为程序名
该函数可使用于win32和utunbu下
posted @ 2010-08-04 19:31 ccsdu2009 阅读(2279) | 评论 (1)编辑 收藏
 
由于需要使用c++对字符串进行切割
故对相关的知识做一个总结
1.使用
std::stringstream
切割字符串
比如:
    std::stringstream str(text);
    std::
string tok;
    
while(getline(str,tok,(char)32)){}
2.还是使用std::stringstream流析出
比如:
    std::stringstream str(s);
    
string t1,t2,t3,t4;
    str 
>>t1;
    str 
>>t2;
    str 
>>t3;
    str 
>>t4;
3.使用strtok
比如:
char* token = strtok(str.c_str()," ");
4.使用boost的
tokenizer
比如:
boost::tokenizer<> ss(s,char_separator(' '));
for(boost::tokenizer<>::iterator beg=ss.begin(); beg!=ss.end(); ++beg){cout<<*beg<<endl;}
不过其默认的分隔为空格和标点符号
如果需要定制其模板参数
可以按照下面的书写:
class char_separator
{
public:      
    char_separator(
char c):sep(c){}
    
void reset(){}
    template 
<typename InputIterator,typename Token>
    
bool operator()(InputIterator& next,InputIterator end,Token& tok)
    {
        tok 
= Token();
        
for(;next != end && *next == sep;++next);
        
if (next == end) 
            
return false;
        
for(;next != end && *next != sep;++next)
        {
            tok 
+= *next;
        }
        
return true;
    }
private:
    
char sep;
}; 
比如:
boost::tokenizer<char_separator> mytok(str,char_separator('@'));
    
for(boost::tokenizer<char_separator>::iterator beg=mytok.begin(); beg!=mytok.end(); ++beg)
    {
        
if(index == 0)
            text1 
= *beg;
        
else if(index == 1)
            text2 
= *beg;
        
else if(index == 2)
            text3 
= *beg;
        
else if(index == 3)
            text4 
= *beg;            
        index
++;
    }
5.还是boost.
#include <cstdlib>
#include 
<iostream>
#include 
<string>
#include 
<vector>
#include 
<iostream>
#include 
<iterator>
#include 
<functional>
#include 
<boost/algorithm/string/classification.hpp>
#include 
<boost/algorithm/string/split.hpp>
#include 
<boost/algorithm/string/find_iterator.hpp>

using namespace std;
using namespace boost;

int main(int argc, char *argv[])
{
    vector
<string> strlist;
    
string str("12 34 56 678  ccsdu2004");
    split(strlist,str,is_any_of(
" "),token_compress_on);

    
for(unsigned int index=0;index<strlist.size();index++)
    {
        cout
<<index <<":"<<strlist[index]<<endl;
    };
        
    system(
"PAUSE");
    
return EXIT_SUCCESS;
}
6.其他不知道的方法若干
...

posted @ 2010-08-04 14:16 ccsdu2009 阅读(2856) | 评论 (3)编辑 收藏
 
Srilm的全称是Stanford Research Institute Language Modeling Toolkit
他被用来构建和应用统计语言模型,主要用于语音识别,统计标注和切分,以及机器翻译等工作
由于需要我要在win32下编译她
可是在网上找到很多都是基于cygwin的交叉式编译
使用起来总是有点问题
不过还好找到了一个使用srilm的vc解决方案
在这里:
http://www.inference.phy.cam.ac.uk/kv227/srilm/
然后需要做的就是下载srilm解压放在srilm_vc2008 or srilm_2005文件夹下编译即可

posted @ 2010-08-03 10:46 ccsdu2009 阅读(2085) | 评论 (1)编辑 收藏
 
插件系统是游戏引擎中一个比较大的子系统
这个系统的目的就是动态增加引擎的功能而不必修改引擎接口

之前虽然做过插件这块‘
但是感觉设计的不好
这次总算弄了一个比较完备的插件系统

相关对象和结构
1.插件 Plugin
///////////////////////////////////////////////////////////
/// 定义引擎插件数据结构
///////////////////////////////////////////////////////////
class Plugin
{
public:
    Plugin()
    {   
        Reset();
    }   
    
///////////////////////////////////////////////////////
    
/// 插件重启
    
///////////////////////////////////////////////////////     
    void Reset();
    
///////////////////////////////////////////////////////
    
/// 插件名字
    
///////////////////////////////////////////////////////     
    engine_string name;
    
///////////////////////////////////////////////////////
    
/// 插件作者
    
///////////////////////////////////////////////////////     
    engine_string maker;
    
///////////////////////////////////////////////////////
    
///插件描述
    
///////////////////////////////////////////////////////     
    engine_string description;
    
///////////////////////////////////////////////////////
    
/// 插件版本
    
///////////////////////////////////////////////////////     
    ushort  vermain;
    
ushort  versub;
    
ushort  verpitch;
    
///////////////////////////////////////////////////////
    
/// 插件类型
    
///////////////////////////////////////////////////////     
    ushort  type;
    
///////////////////////////////////////////////////////
    
/// 插件合法性标记
    
///////////////////////////////////////////////////////     
    ushort  valid;
};
插件数据结构只包含了插件的一些基本信息 比如名字,描述,作者,功能类型等

2.PluginLoader
////////////////////////////////////////////////////////
/// 定义插件载入类
////////////////////////////////////////////////////////
class PluginLoader
{
public:
    
/////////////////////////////////////////////////////
    
/// 构造,析构插件载入
    
/////////////////////////////////////////////////////
    PluginLoader();
    
~PluginLoader();
public:     
    
/////////////////////////////////////////////////////
    
/// 载入插件和卸载
    
/////////////////////////////////////////////////////
    bool  Load(const engine_string &plugin);
    
void  Free();     
    
/////////////////////////////////////////////////////
    
/// 获取插件指定符号地址
    
/////////////////////////////////////////////////////     
    void* GetAddress(const engine_string &name);
    
/////////////////////////////////////////////////////
    
/// 检测插件是否合法
    
/////////////////////////////////////////////////////     
    bool  IsValid()const{return handle != NULL;}
    
/////////////////////////////////////////////////////
    
/// 获取插件句柄
    
/////////////////////////////////////////////////////
    void* GetHandle(){return handle;}
    
void* GetHandle()const{return handle;}   
    
/////////////////////////////////////////////////////
    
/// 获取插件名
    
/////////////////////////////////////////////////////
    engine_string GetPluginName()const{return pluginame;}   
private:     
    
void*         handle;
    engine_string pluginame;   
};

PluginLoader主要是载入插件文件并获取给定符号的函数指针
这个并没没有暴漏出来供用户调用
3.PluginFactory

///////////////////////////////////////////////////////////
/// 定义引擎插件工厂基类
///////////////////////////////////////////////////////////
class GAPI PluginFactory : public Object
{
public:
    
///////////////////////////////////////////////////////
    
/// 构造和析构引擎插件工厂基类
    
///////////////////////////////////////////////////////
    PluginFactory();
    
virtual ~PluginFactory();
public:
    
///////////////////////////////////////////////////////
    
/// 注册,反注册插件
    
///////////////////////////////////////////////////////
    virtual bool ENGINE_CALL RegisterFactory(const engine_string& plugin) = 0;
    
virtual void ENGINE_CALL UnregisterFacory() = 0;
public:     
    
///////////////////////////////////////////////////////
    
/// 获取对应插件类型标识
    
///////////////////////////////////////////////////////     
    virtual ushort GetPluginType()const = 0;
private:
    DECLARE_OBJECT(PluginFactory)        
};

插件工厂是一切需要从插件中获取功能的对象工厂
她主要提供了插件的注册和反注册以及获取插件工厂所对应的插件类型

4.PluginManager 插件管理器
///////////////////////////////////////////////////////////
/// 定义引擎插件管理器
///////////////////////////////////////////////////////////
class GAPI PluginManager
{   
public:
    
///////////////////////////////////////////////////////
    
/// 获取,设置插件目录
    
///////////////////////////////////////////////////////
    engine_string GetPluginFolder()const;
    
void SetPluginFolder(const engine_string& folder);
public:
    
///////////////////////////////////////////////////////
    
/// 插件装载和卸载
    
///////////////////////////////////////////////////////
    void InstallPlugin();   
    
void UnstallPlugin();   
public:
    
///////////////////////////////////////////////////////
    
/// 注册,反注册插件工厂
    
///////////////////////////////////////////////////////        
    bool RegisterPluginFactory(PluginFactory* factory);
    
bool UnregisterPluginFactory(PluginFactory* factory);
public:
    
///////////////////////////////////////////////////////
    
/// 获取插件个数
    
///////////////////////////////////////////////////////        
    ushort ENGINE_CALL GetPluginNumber()const;
    
///////////////////////////////////////////////////////
    
/// 获取给定索引插件信息
    
///////////////////////////////////////////////////////
    Plugin ENGINE_CALL GetPluginByType(ushort index)const;
    
///////////////////////////////////////////////////////
    
/// 获取给定索引的插件名
    
///////////////////////////////////////////////////////  
    engine_string ENGINE_CALL GetPluginNameByType(ushort index)const;   
    
///////////////////////////////////////////////////////
    
/// 获取给定插件类型的插件载入类
    
///////////////////////////////////////////////////////      
private:
    PluginManagerImpl  
*impl;
    DEFINE_SINGLETON(PluginManager);
};

}

//! 定义插件管理器单件
#define GLOBAL_PLUGINMANAGER_PTR (core::PluginManager::GetInstance())
插件管理器是插件系统的核心,充当了插件管理者的角色
要使用插件线需要设置插件目录
然后装载插件
然后注册特定的插件工厂
然后就可以通过插件工厂获取插件对象指针咯

在这里插件管理器是作为一个单间使用的
具体的宏 DEFINE_SINGLETON(PluginManager);
展开之后为:
#define DEFINE_SINGLETON(SingletonObject)\
private:\
    
static std::auto_ptr<SingletonObject> instance;\
protected:\
    SingletonObject();\
public:\
    
~SingletonObject();\
    
static SingletonObject* GetInstance(){\
    
if(!instance.get())\
    {\
        instance 
= std::auto_ptr<SingletonObject>(new SingletonObject());\
    }\
    
return instance.get();\
}

#define IMPLEMENT_SINGLETON(SingletonObject)\
std::auto_ptr
<SingletonObject> SingletonObject::instance(NULL);
下面是具体的一个插件对象---摄像头捕获类
如下:通过调用CaptureImage就可以把摄像头所见程序保存为image了
///////////////////////////////////////////////////////////
/// 定义摄像头捕获类(以插件形式提供之)
///////////////////////////////////////////////////////////
class GAPI CapturePicture : public Object
{
public:
    
///////////////////////////////////////////////////////
    
/// 构造和析构
    
///////////////////////////////////////////////////////
    CapturePicture();
    
virtual ~CapturePicture();
public:
    
///////////////////////////////////////////////////////
    
/// 获取摄像头图形数据
    
///////////////////////////////////////////////////////
    virtual bool CaptureImage(RefPtr<Image> &image);
private:
    DECLARE_OBJECT(CapturePicture)        
};

通过调用CaptureImage就可以把摄像头所见程序保存为image了

下面这个是对于的工厂:
///////////////////////////////////////////////////////////
/// 定义摄像头捕获工厂类
///////////////////////////////////////////////////////////
class GAPI CapturePictureFactory : public PluginFactory
{
public:
    
///////////////////////////////////////////////////////
    
/// 构造和析构
    
///////////////////////////////////////////////////////
    CapturePictureFactory();
    
virtual ~CapturePictureFactory();
public:
    
///////////////////////////////////////////////////////
    
/// 获取摄像头捕获指针
    
///////////////////////////////////////////////////////
    CapturePicture* Create();
    
///////////////////////////////////////////////////////
    
/// 注册插件
    
///////////////////////////////////////////////////////
    bool ENGINE_CALL RegisterFactory(const engine_string& plugin);
    
void ENGINE_CALL UnregisterFacory();
    
///////////////////////////////////////////////////////
    
/// 获取对应插件类型标识
    
///////////////////////////////////////////////////////     
    ushort GetPluginType()const;
private:
    PluginLoader
* loader;
    DECLARE_OBJECT(CapturePictureFactory)
};
这个只是多了一个函数Createer而已

然后再说具体的插件部分
插件3函数:
extern "C" void    GAPI Plugin_Info(Plugin &plugin);         
extern "C" Object* GAPI Plugin_Install();
extern "C" void    GAPI Plugin_Unstall(void*);  
具体为通过Plugin_Info
通过Plugin_Install获取插件实际对象指针
通过Plugin_Unstall卸载插件

最后一个部分是插件的使用小例子:
#include <cstdlib>
#include 
<iostream>
#include 
<GEngine/Header.hpp>

using namespace std;

int main(int argc, char *argv[])
{
    GLOBAL_PLUGINMANAGER_PTR
->SetPluginFolder("plugin");
    GLOBAL_PLUGINMANAGER_PTR
->InstallPlugin();
    std::cout
<<"插件个数:"<<GLOBAL_PLUGINMANAGER_PTR->GetPluginNumber()<<std::endl;
    core::CapturePictureFactory factory;
    std::cout
<<"注册视频捕获插件工厂:"<<GLOBAL_PLUGINMANAGER_PTR->RegisterPluginFactory(&factory)<<std::endl;
    core::CapturePicture
* capturepicture = factory.Create();
    std::cout
<<"插件工厂产品标识:"<<factory.GetPluginType()<<std::endl;
    core::RefPtr
<core::Device> device = core::InitDevice("插件测试");
    
if(!capturepicture)
    {
        std::cout
<<"生成摄像头捕获指针失败了"<<std::endl;
        system(
"PAUSE");
        
return -1;
    }
    core::RefPtr
<core::ResourceManager> resmgr = device->GetResourceManager();
    core::RefPtr
<core::ImageManager> imagemanager = resmgr->GetImageManager();
    core::RefPtr
<core::Image> image = imagemanager->CreateObject("capturepicture");
   
    capturepicture
->CaptureImage(image);
    std::cout
<<"save image is:"<<image->Save("capture.bmp")<<std::endl;
   
    BEGIN_LOOP(device)
       glClearColor(
0.1,0.3,0.2,1.0f);
       glClear(GL_COLOR_BUFFER_BIT 
| GL_DEPTH_BUFFER_BIT);     
    END_LOOP(device)   
   
    GLOBAL_PLUGINMANAGER_PTR
->UnregisterPluginFactory(&factory);
    GLOBAL_PLUGINMANAGER_PTR
->UnstallPlugin();
    
//system("PAUSE");
    return EXIT_SUCCESS;
}
题后话:
当前引擎支持xp,vista,win7环境
在编译器支持vc7.1,vc8,vc9,devc++,codeblock
也许在不久的将来我会弄一个linux版本的
posted @ 2010-07-29 16:40 ccsdu2009 阅读(2516) | 评论 (17)编辑 收藏
 
一直以来使用FreeImage保存图像总是采用下面的办法:

FIBITMAP* bitmap =FreeImage_Allocate(width,height,24);    
const unsigned char* source = data;
    
for(int y=0; y < height; y++ )
    {
        unsigned 
char* scanline = (unsigned char*)FreeImage_GetScanLine(bitmap, height - y - 1 );     
        memcpy(scanline,data,
sizeof(source[0]) * width);
        data 
+= width;
    }
FreeImage_Save(FIF_BMP,bitmap,file,0)
不过今天看到了函数:
FreeImage_ConvertFromRawBits
使用它可以更加快速的保存图形
如下:
FIBITMAP* bitmap = FreeImage_ConvertFromRawBits(data,width,height,pitch,24,FI_RGBA_BLUE_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_RED_MASK,FALSE); 
    
然后调用FreeImageSave即可

另外关于获取FreeImage图形到数组
也有2个办法
一个是逐行复制
一个是使用memcpy(data,FreeImage.GetDibBits())
当然也可以逐像素复制咯
posted @ 2010-07-21 20:14 ccsdu2009 阅读(4357) | 评论 (0)编辑 收藏
 
大律法求取图形阈值
放在这里

int GetOtsuValue(unsigned char* data,int width,int height)
{
    
int table[256];
    memset(table,
0,sizeof(int)*256);
    
for(int i = 0; i < width*height;i++)    
    {
        table[data[i]] 
++;        
    }
    
    
int sum = 0;
    
for(int i = 0 ;i < 256;i++)
    {
        sum 
+= table[i];
        
if(sum >= width*height*0.5)
           
return i;
    }
    
return 255;
}


posted @ 2010-07-21 17:13 ccsdu2009 阅读(1156) | 评论 (0)编辑 收藏
 
1.C风格的链接方式
比如:
#define GAPI G_DLL_API
#define G_FUNC(ret) extern "C" GAPI ret
编译函数:
G_FUNC(Vector3) GetRandVec3();
对于msvc系列编译器是不允许的
对于mingw系列是被允许的咯
2.对模板支持的差别
对没有实际调用的模板类成员函数存在的问题处理方式的差异
msvc可以给出编译错误
mingw不能

举例来说:
template <class T>
struct Vec2
{
    Vec2();
    Vec2(T x,T y);
    
    Vec2
<T> operator+=(const Vec2& v2)
    {
        
return Vec(x+v2.x,y+v2.y_);  
    }

    T x_,y_;
};
实例化为
Vec2<int> v2;

3.对静态浮点类型成员变量处理的差异
msvc不允许静态浮点类型的成员变量
比如
struct Math
{  
    static const float PI = 3.14.15925f;
};
mingw是允许的

posted @ 2010-07-16 10:35 ccsdu2009 阅读(528) | 评论 (1)编辑 收藏
 
先放这里改天写吧!
posted @ 2010-06-28 20:28 ccsdu2009 阅读(532) | 评论 (0)编辑 收藏
仅列出标题
共38页: First 24 25 26 27 28 29 30 31 32 Last