C++常用代码段

类型最大值和最小值

64位系统:

#include <iostream>
#include <climits>
#include <cfloat>
using namespace std;

int main(int argc, char *argv[])
{
    cout<<"Maximum values:"<<endl;
    cout<<"char: "<<CHAR_MAX<<endl;
    cout<<"unsigned char: "<<UCHAR_MAX<<endl;
    cout<<"int: "<<INT_MAX<<endl;
    cout<<"unsigned int: "<<UINT_MAX<<endl;
    cout<<"short: "<<SHRT_MAX<<endl;
    cout<<"unsigned short: "<<USHRT_MAX<<endl;
    cout<<"long: "<<LONG_MAX<<endl;
    cout<<"unsigned long: "<<ULONG_MAX<<endl;
    cout<<"long long: "<<LLONG_MAX<<endl;
    cout<<"unsigned long long: "<<ULLONG_MAX<<endl;
    cout << "float: " << fixed << FLT_MAX  << endl;
    cout << "double: " << fixed << DBL_MAX  << endl << endl;

    cout<<"Minimun values:"<<endl;
    cout<<"char="<<CHAR_MIN<<endl;
    cout<<"short="<<SHRT_MIN<<endl;
    cout<<"int="<<INT_MIN<<endl;
    cout<<"long="<<LONG_MIN<<endl;
    cout<<"long long="<<LONG_LONG_MIN<<endl;
    cout.precision(50);
    cout << "positive float: " << fixed << FLT_MIN  << endl;
    cout << "positive double: " << fixed << DBL_MIN  << endl;
    cout << defaultfloat;

    float t = FLT_MAX * (-1);
    cout <<"float: "<< t << endl;
}

输出:

Maximum values:
char: 127
unsigned char: 255
int: 2147483647
unsigned int: 4294967295
short: 32767
unsigned short: 65535
long: 9223372036854775807
unsigned long: 18446744073709551615
long long: 9223372036854775807
unsigned long long: 18446744073709551615
float: 340282346638528859811704183484516925440.000000
double: 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000

Minimun values:
char=-128
short=-32768
int=-2147483648
long=-9223372036854775808
long long=-9223372036854775808
positive float: 0.00000000000000000000000000000000000001175494350822
positive double: 0.00000000000000000000000000000000000000000000000000
float: -340282346638528859811704183484516925440

数值与字符串之间的转换

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

int main(int argc, char *argv[])
{
    float t = FLT_MAX * (-1);
    cout <<"float:" <<t << endl;

    string s1 = to_string(t);
    cout << "float to string:"<<s1 << endl;

    string s2 = "-123.1231";
    cout << "string to float:" << stof(s2) << endl;
    string s3 = "+3892.123";
    cout << "string to int:"<<stoi(s3) << endl;
}

输出:

float:-3.40282e+38
float to string:-340282346638528859811704183484516925440.000000
string to float:-123.123
string to int:3892

随机数

#include <iostream>
#include <stdlib.h>
#include <time.h>

int main(){
    int i = 0;
    srand((unsigned int)time(0)); //随机初始化种子
    while(i < 100)
    {
        float t = 255* (float)rand()/RAND_MAX; // 产生0-255之间的浮点随机数
        cout << t << " ";
        i++;
    }
    return 0;
}

分割字符串

#include <iostream>
#include <boost/algorithm/string.hpp>
using namespace std;
int main(int argc, char *argv[])
{
    string line("test;;test2;test3;");
    vector<string> strs;
    boost::split(strs,line,boost::is_any_of(";"));
    for(auto c:strs)
        cout << c << endl;
}

输出:

test

test2
test3

替换文件的指定字符

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

void str_replace(string & s,string & src,string & dst)
{
    string::size_type pos = 0;
    while((pos = s.find(src,pos)) != string::npos)
    {
        s.replace(pos,src.size(),dst);
        pos += dst.size();
    }
}
int main(int argc, char *argv[])
{
    ifstream in("test.txt",ios::in);
    if(!in.is_open())
        return -1;
    string t;
    string t_src= ";";
    string t_dst= " ";
    vector<string> t_save;
    while(getline(in,t))
    {
        str_replace(t,t_src,t_dst);
        t_save.push_back(t);
    }
    in.close();
    ofstream out("test.txt",ios::out);
    for(auto & c: t_save)
        out << c << endl;
    out.close();
}

输入:test.txt

123;ljljk;123

输出:test.txt

123 ljljk 123

文件夹遍历

C++和C都可以利用dirent来编译文件夹。但是要注意的是,在linux下,不能用_finddata_t这个结构体来遍历文件夹,_finddata_t定义在io.h中,然而这个结构体是windows所特有的,linux下面的io.h并没有_finddata_t的定义。因此,在linux下面调用必然会出错:error: aggregate ‘*********’ has incomplete type and cannot be defined.

#include <iostream> 
#include <dirent.h>
#include <stdio.h>
#include <cstring>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>

using namespace std;

void readdir(string path,int is_recursive = 0)
{
	DIR * dir= NULL;
	struct dirent * ptr = NULL;
	struct stat statbuf;
	if((dir = opendir(path.c_str())) == NULL)
		return ;
	chdir(path.c_str());
	while((ptr = readdir(dir)) !=NULL)
	{
		lstat(ptr->d_name,&statbuf);
		if(S_IFDIR & statbuf.st_mode)
		{
			if(is_recursive)
			{
				if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0)
					continue;
				readdir(ptr->d_name);
			}
		}
		else{
			cout << "file:" << ptr->d_name << endl;
		}
	}
	chdir("..");
}
int main()
{
    readdir("11/",0);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值