首页 > 代码库 > C++函数

C++函数

    /**     * 将整数字符串按照特定字符拆分成字符串数组     * example 输入:"1,4,3,110,2,90,7" 输出 "1","4","3","110","2","90","7"     * @return      */void split(string& strInput, char chsymbol, vector<string>& vecSplit){    string::size_type startPos = 0;    string::size_type endPos = string::npos;    while ((endPos = strInput.find(chsymbol, startPos)) != string::npos)    {        vecSplit.push_back(strInput.substr(startPos, endPos - startPos));        startPos = endPos + 1;     }    vecSplit.push_back(strInput.substr(startPos));}

 

// 字符串整数间用空格隔开void outputWithSpace(vector<string>& vecDelOutPut, string& strOutput){    int count = 0;    for (vector<string>::iterator it = vecDelOutPut.begin(); it != vecDelOutPut.end(); it++)    {        strOutput.append(it->c_str());        strOutput.append(" ");    }    // 去掉最后一个空格    string::size_type npos = strOutput.find_last_of( );    strOutput = strOutput.substr(0, npos);}

 

/**    * 升序排列    * @return     * @param 输入 开始,结束,比较函数    */    sort(vecSplit.begin(), vecSplit.end(), compare);    // sort比较函数bool compare(string a, string b){    return (atoi(a.c_str()) < atoi(b.c_str()));}// compare函数还可以定义为int型,如果返回正数则a>b,负数则a<b,0的话就是a=b

 

/***********reverse 倒序*************/#include <algorithm>std:reverse(string.begin(), string.end());

 

/************* advance函数,操作迭代器 template <class InputIterator, class Distance>void advance (InputIterator& i, Distance n);当n为正数时,向前移动;当n为负数时,向后移动*******************************************/#include <algorithm>unsigned int curPlayer = 1;list<int>::iterator it = listPlayer.begin();advance(it, curPlayer);

 

/***************************** String2Int *****************************/int Str2Int( const std::string &str ){    int i = 0;    std::istringstream io(str);    if (str == "")    {        i = 0;    }    else if (str.length() >= 3 && (str.substr(0, 2) == "0x" || str.substr(0, 2) == "0X"))    {        io >> std::hex >> i;    }    else if (str.length() >= 2 && str.substr(0, 1) == "-")    {        io >> i;    }    else if (!IsDigitStr(str))    {        i = 0;    }    else    {        io >> i;    }    return i;}

 

/* * 判断一个字符串是否只包含0~9的数字 * @param str 输入字符串 * @return bool  true 字符串只包含0~9的数字 */bool IsDigitStr(const std::string & Str){    for (std::string::const_iterator it = Str.begin(); it != Str.end(); ++it)    {        if (!isdigit(*it))        {            return false;        }    }    return true;}

 

/**********************IntToStr*********************/std::string IntToStr(const int i){    std::string str;        char szBuf[16] = {0};    str.append(szBuf, (unsigned int)(sprintf(szBuf, "%d", i)));        return str;}//或者#include <sstream>long iInput;ostringstream ostrInput;ostrInput << iInput;string strInput = ostrInput.str();

 

/**********************replace*********************/     #include <algorithm>         string   strInfo="This is Winter, Winter is a programmer. Do you know Winter?";     cout<<"old string is :"<<endl<<strInfo<<endl;          /// 将strInfo中的‘W‘替换成‘w‘     replace(strInfo.begin(),strInfo.end(),W,w);     cout<<"new string is :"<<endl<<strInfo<<endl;

 

/*************************判断map插入是否成功***********************/#include <utility>typedef std::map<ACE_UINT32, SMPP_LOG_INFO> LOGVALUEMAP;m_upLogMap.erase(EMFReq.m_head.m_sequence_number);// 保存logMapstd::pair<LOGVALUEMAP::iterator, bool> PairRev = m_upLogMap.insert(LOGVALUEMAP::value_type(EMFReq.m_head.m_sequence_number, logInfo));if (!PairRev.second){    ......}

 

/*********************map(list)按vaule大小排序*************/#include <algorithm>typedef pair<char, int> PAIR;vector<PAIR> vecPair;stable_sort(vecPair.begin(), vecPair.end(), compare);int compare(const PAIR a, const PAIR b){    return a.second > b.second;}

 

/****************小写转大写*******************/std::string sl = "hello";std::transform(sl.begin(), sl.end(), sl.begin(), toupper);   transform 遍历容器里面元素 执行操作第1和2参数数据起始和结束位置(迭代器)参数3写入目标起始位置参数4执行操作(函数)

 

/* * 找到str中的strKey将其替换为strValue. * @param str 待替换的字符串 * @param strKey 需替换的字符源子串 * @param strValue 需替换的字符目的子串 */void ReplaceKeyValue(std::string& str, const std::string& strKey, const std::string& strValue){    std::string::size_type findPos = 0;    while ((findPos = str.find(strKey, findPos)) != std::string::npos)    {        str.replace(findPos, strKey.size(), strValue);        findPos += strValue.size();    }}

 

C++函数