C++常用功能:文件读写 计时 随机数 等的用法
1、字符串string类
1 2 3 4 5 string str; string str ("ABC" ) string str ("ABC" , strlen) string s ("ABC" ,stridx,strlen) string s (strlen, 'A' )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 str1.assign ("ABC" ); str1.length (); str1.size (); str1.capacity (); str1.resize (10 ); str1.resize (10 ,char c); str1.reserve (10 ); str1.swap (str2); str1.puch_back ('A' ); str1.append ("ABC" ); str1.insert (2 ,"ABC" ); str1.erase (2 ); str1.erase (2 ,1 ); str1.clear (); str1.replace (2 ,4 , "ABCD" ); str1.empty ();
1 2 3 4 5 str.c_str (); char *st = "hello" ;string st1 = st; string st2 (st, st + strlen(st)) ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <sstream> > istringstream iss ("123.5" ) ; double num; if (iss>>num) cout<<num << endl; string str="123.5" ; double num;if (istringstream (str)>>num) cout<<num<<endl; #define TO_NUM(str,num) (istringstream(str)>>num) ostringstream oss; oss <<123.5 ; string str= oss.str () ; #define TO_STRING(num) ( ((ostringstream&)(ostringstream()<<num)).str() ) double num=123.5 ;string str= TO_STRING (123.5 );
2、文件读写
文本文件读取与写入 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <fstream> int count;fstream inFile, outFile; if ( (_access( "in.txt" , 0 )) != -1 ) { printf_s ( "File crt_ACCESS.C exists.\n" ); inFile.open ("in.txt" , ios::in | ios::binary); } outFile.open ("out.txt" , ios::out | ios::binary); inFile >> count; outFile << count; inFile.close (); outFile.close ();
文件夹相关操作
1 2 3 4 5 6 7 8 9 10 #include <io.h> #include <unistd.h> <pre name="code" class ="cpp" >int access (const char * _Filename, int _AccessMode) _AccessMode:{ #define R_OK 4 #define W_OK 2 #define X_OK 1 #define F_OK 0 }
1 2 3 #include <direct.h> int mkdir (const char *_Path) int rmdir (const char *_Path)
函数返回0表示成功,返回-1表示失败或不存在
3、计时函数
time()
time()获取当前的系统时间,返回的结果是一个time_t类型,其实就是一个==大整数==,其值表示从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。
1 2 3 4 5 6 7 8 void test1 () { time_t start,stop; start = time (NULL ); foo (); stop = time (NULL ); printf ("Use Time:%ld\n" ,(stop-start)); }
clock()
clock()函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock) 常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元。
1 2 3 4 5 6 7 8 9 10 void test2 () { double dur; clock_t start,end; start = clock (); foo (); end = clock (); dur = (double )(end - start); printf ("Use Time:%f\n" ,(dur/CLOCKS_PER_SEC)); }
timeGetTime()
timeGetTime()函数以毫秒计的系统时间。该时间为从系统开启算起所经过的时间,是windows api。
1 2 3 4 5 6 7 8 void test3 () { DWORD t1,t2; t1 = timeGetTime (); foo (); t2 = timeGetTime (); printf ("Use Time:%f\n" ,(t2-t1)*1.0 /1000 ); }
4、随机数
cstdlib中伪随机数 C语言中通过函数rand和srand来产生伪随机数,这两个函数包含在头文件cstdlib中,其中srand用来产生设置随机数种子,rand每次返回一个当前的种子对应的随机数,这两个函数的声明如下:
1 2 void srand (unsigned seed) ;int rand (void ) ;
这两个函数的原型形如:
1 2 3 4 5 6 7 8 9 10 11 12 unsigned long int next = 1 ;int rand (void ) { next = next * 1103515245 + 12345 ; return (unsigned int )(next/65536 ) % 32768 ; } void srand (unsigned int seed) { next = seed; }
显然rand只能生成整数,需要小数时,只需要跟一个浮点数进行除法即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <iostream> #include <cstdlib> #include <ctime> template <typename T> T cRandom (int min, int max) { return (min + static_cast <T>(max * rand () / static_cast <T>(RAND_MAX + 1 ))); } int main (void ) { srand ((unsigned )time (NULL )); std::cout << "int: " << std::endl; for (int i = 0 ; i < 10 ; ++i) { std::cout << cRandom <int >(1 , 15 ) << " " ; } std::cout << std::endl; std::cout << "double: " << std::endl; for (int i = 0 ; i < 10 ; ++i) { std::cout << cRandom <double >(1 , 15 ) << " " ; } std::cout << std::endl; return 0 ; }
生成指定范围的随机数表达式:
要取得[a,b)的随机整数,使用(rand() % (b-a))+ a;
要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a;
要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1;
通用公式:a + rand() % n;其中的a是起始值,n是整数的范围。
要取得a到b之间的随机整数,另一种表示:a + (int)b * rand() / (RAND_MAX + 1)。
要取得0~1之间的浮点数,可以使用rand() / double(RAND_MAX)。
(2)C++11中的random类 random类是C++11中增加的类,该类可以用于生成随机数,具有C语言中rand生成伪随机数的功能,但其功能更强大。原生支持生成浮点数,及分布类。
随机数发生器主要由分布对象和随机数引擎对象组成。其中随机数引擎用于根据随机数种子来产生随机数,分布对象对产生的随机数根据需求进行分布。 random中定义了多种随机数引擎及分布类型,常用的引擎是默认伪随机数引擎类default_random_engine,常用的分布类有产生整型分布的uniform_int_distribution,产生浮点型的分布uniform_real_distribution。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <random> #include <iostream> #include <ctime> double random_unint (unsigned int min, unsigned int max, unsigned int seed = 0 ) { static std::default_random_engine e (seed) ; static std::uniform_real_distribution<double > u (min, max) ; return u (e); } int main (void ) { for (int i = 0 ; i < 15 ; ++i) { std::cout << random_unint (0 , 15 , time (NULL )) << " " ; } std::cout << std::endl; return 0 ; }
一个给定的随机数发生器一直会生成相同的随机数序列。一个函数如果定义了局部的随机数发生器,应该将其(包括引擎和分布对象)定义为static的,否则每次调用函数都会生成相同的序列。也就是说定义成static后每次调用还是之前那个发生器,第一次调用产生一批随机数,再次调用将产生接下来的随机数,否则每次调用产生的都是最前面的那些随机数。
依然需要使用time来做为种子产生每个不同时刻都不同的随机序列,但由于time默认返回的是以秒计的时间,所以有可能多次使用的都是相同的种子。
伪随机数引擎
随机数引擎支持的操作如下:
1 2 3 4 5 6 7 Engine e; Engine e (s) ; e.seed (s); e.min (); e.max (); Engine::result_type e.discard (u);
随机数引擎使用
1 2 std::default_random_engine e; std::cout << e ();
分布
分布类型有很多种,常用的有返回整型的uniform_int_distribution和返回浮点型的uniform_real_distribution
分布支持的操作
1 2 3 4 5 6 Dist d; Dist d (min, max) ; d (e) d.min () d.max () d.reset ();