/** * Divide integer strings into string arrays by specific characters * Example input: "1,4,3,110,2,90,7" Output "1", "4", "3", "" "," 2 "," All "," 7 "* @return */voidSplitstring& Strinput,CharChsymbol, 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));}
//string integers separated by spacesvoidOutputwithspace (vector<string>& Vecdeloutput,string&stroutput) { intCount =0; for(vector<string>::iterator it = Vecdeloutput.begin (); It! = Vecdeloutput.end (); it++) {Stroutput.append (it-c_str ()); Stroutput.append (" "); } //remove the last space string:: Size_type NPOs = stroutput.find_last_of (' '); Stroutput= Stroutput.substr (0, NPOs);}
/* * * ascending order * @return * @param input start, end, compare function */ sort (vecsplit.begin (), Vecsplit.end (), compare); // Sort comparison Function bool Compare (stringstring b) { return (Atoi (A.c_str ()) < atoi (B.c_str ()));} // The compare function can also be defined as an int, a>b if a positive number is returned, or a negative number a<b,0 a=b
/* **********reverse Reverse ************ */ <algorithm>std:reverse (stringstring. End ());
/* ************ advance function, Operation iterator template <class Inputiterator, class distance>void advance (inputiterator& I , Distance N); moves forward when N is a positive number, and when n is negative, moves backwards *******************************************/< algorithm>int1; list<int>::iterator it = Listplayer.begin (); Advance (it, curplayer);
/***************************** string2int *****************************/intStr2Int (ConstSTD::string&str) { inti =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; } returni;}
/* * determine if a string contains only 0~9 numbers * @param str input String * @ return bool True string contains only the number of 0~9 */ bool isdigitstr (const std::string & Str) { string :: const_iterator it = Str.begin (); It! = Str.end (); ++it) { it) { return false
; }}
return
true
;}
/**********************inttostr*********************/std::stringIntToStr (Const inti) {std::stringstr; Charszbuf[ -] = {0}; Str.append (Szbuf, (unsignedint) (sprintf (Szbuf,"%d", i))); returnstr;}//or#include <sstream>LongIinput;ostringstream ostrinput;ostrinput<<Iinput;stringStrinput = Ostrinput.str ();
/**********************replace*********************/#include<algorithm>stringstrinfo="This is Winter, Winter is a programmer. Do you know Winter?"; cout<<"Old string is:"<<endl<<strInfo<<Endl; ///replace ' W ' in Strinfo with ' W 'Replace (Strinfo.begin (), Strinfo.end (),'W','W'); cout<<"new string is:"<<endl<<strInfo<<endl;
/* ************************ determine if the map insertion was successful ********************** */ <utility>typedef std::map<ace_uint32, smpp_log_info> logvaluemap;m_ Uplogmap.erase (EMFReq.m_head.m_sequence_number); // Save Logmap bool> Pairrev = m_uplogmap.insert (Logvaluemap::value_type (EMFReq.m_head.m_sequence_number, Loginfo )); if (! Pairrev.second) { ......}
/* ********************map (list) sorted by vaule size ************ */ <algorithm>typedef pair<charint> pair;vector<pair > vecpair;stable_sort (Vecpair.begin (), vecpair.end (), compare); int Compare (constconst PAIR b) { return a.second > B.second;}
/* *************** lowercase to uppercase ****************** */ std:: string " Hello " ; Std::transform (Sl.begin (), Sl.end (), Sl.begin (), toupper); Transform traversal container inside element execute operation 1th and 2 parameter data start and end position (iterator) Parameter 3 write target start position parameter 4 perform operation (function)
/** Find strkey in STR to replace it with strvalue. * @param str to replace the string * @param strkey to replace the word Fu Yuanji String * @param strvalue the character purpose substring to be replaced*/voidReplacekeyvalue (std::string& STR,ConstSTD::string& Strkey,ConstSTD::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 + + functions