我的常用库C++

1.将数字转为string

template <typename T>
string GetString(const T parameter)
{
	std::stringstream newStr;
	newStr << parameter;
	return newStr.str();
}

2.将字符串安装特定字符分割

//字符串分割函数
vector<string> split(string str, string pattern)
{
	vector<string> ret;
	if (pattern.empty()) return ret;
	size_t start = 0, index = str.find_first_of(pattern, 0);
	while (index != str.npos)
	{
		if (start != index)
			ret.push_back(str.substr(start, index - start));
		start = index + 1;
		index = str.find_first_of(pattern, start);
	}
	if (!str.substr(start).empty())
		ret.push_back(str.substr(start));
	return ret;
}

3.字符串首位去掉空格

//去掉首尾空格
std::string& trim(std::string &s)
{
	if (s.empty())
	{
		return s;
	}

	s.erase(0, s.find_first_not_of(" "));
	s.erase(s.find_last_not_of(" ") + 1);
	return s;
}

4.整型转换

void str2int(int &int_temp, const string &string_temp)
{
	stringstream stream(string_temp);
	stream >> int_temp;
}

void  int2byte(int i, byte *bytes, int size = 4)
{
	//byte[] bytes = new byte[4];  
	memset(bytes, 0, sizeof(byte) *  size);
	bytes[0] = (byte)(0xff & i);
	bytes[1] = (byte)((0xff00 & i) >> 8);
	bytes[2] = (byte)((0xff0000 & i) >> 16);
	bytes[3] = (byte)((0xff000000 & i) >> 24);
	return;
}

//byte转int  
int byte2int(byte* bytes, int size = 4)
{
	int addr = bytes[0] & 0xFF;
	addr |= ((bytes[1] << 8) & 0xFF00);
	addr |= ((bytes[2] << 16) & 0xFF0000);
	addr |= ((bytes[3] << 24) & 0xFF000000);
	return addr;
}

5.判断字符串是否全是数字

方法一:判断字符的ASCII范围(数字的范围为48~57)

#include <iostream>
using namespace std;  

bool AllisNum(string str); 
 
int main( void )  
{  

    string str1 = "wolaiqiao23";  
    string str2 = "1990";  

    if (AllisNum(str1))
    {
        cout<<"str1 is a num"<<endl;  
    }
    else
    {
        cout<<"str1 is not a num"<<endl;  
    }

    if (AllisNum(str2))
    {
        cout<<"str2 is a num"<<endl;  
    }
    else
    {
        cout<<"str2 is not a num"<<endl;  
    }

    cin.get();
    return 0;  
}  
 
bool AllisNum(string str)  
{  
    for (int i = 0; i < str.size(); i++)
    {
        int tmp = (int)str[i];
        if (tmp >= 48 && tmp <= 57)
        {
            continue;
        }
        else
        {
            return false;
        }
    } 
    return true;
}

方法二:使用C++提供的stringstream对象 

#include <iostream>
#include <sstream>  
using namespace std;  

bool isNum(string str);  

int main( void )  
{
    string str1 = "wolaiqiao23r";  
    string str2 = "1990";  
    if(isNum(str1))  
    {  
        cout << "str1 is a num" << endl;  
    }  
    else
    {  
        cout << "str1 is not a num" << endl;  

    }  
    if(isNum(str2))  
    {  
        cout<<"str2 is a num"<<endl;  
    }  
    else
    {  
        cout<<"str2 is not a num"<<endl;  

    }  

    cin.get();
    return 0;  
}  

bool isNum(string str)  
{  
    stringstream sin(str);  
    double d;  
    char c;  
    if(!(sin >> d))  
    {
        return false;
    }
    if (sin >> c) 
    {
        return false;
    }  
    return true;  
}

6.求最值

template <typename T1,typename T2> inline T1 MAX_FUNC(T1 x,T2 y)
{
    return x>y?x:y;
}
template <typename T1,typename T2> inline T1 MIN_FUNC(T1 x,T2 y)
{
    return x<y?x:y;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值