一些有用的c++編程片段

一、tokenizer,通常用於解析傳入的參數

typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep(",");
tokenizer tok(test_uins, sep);
for(tokenizer::iterator tok_iter = tok.begin(); tok_iter != tok.end(); ++tok_iter)
{
int64_t uin = boost::lexical_cast<int64_t>(*tok_iter); 
v_test_uins.push_back(uin);
}

二、 字符串轉換成10進制或者16進制的數字

       strtoul(string.c_str(), NULL, 16)

      或者使用boost::lexical_cast

三、 數字轉化爲字符串

       snprintf(buf, sizeof(buf), "%d", int)

或者 atoi

四、 創建一個目錄,如果已經存在則不報錯

if(mkdir(str_dir, 0755)) {
if(errno != EEXIST) {
LOG_ERROR("failed to make dir %s", str_dir);
return -1;
}
}

五、ip地址轉化函數

inet_addr("1.1.1.1")返回網絡序, inet_network("1.1.1.1")返回主機序,inet_aton("1.1.1.1")可以處理255.255.255.255這種ip地址並返回網絡序ip地址

inet_ntoa(struct in_addr xxx)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章