《C++.Primer.Plus.第五版.中文版》的源代码

源代码在线查看: left.cpp

软件大小: 135 K
上传用户: wait2010
关键词: Primer Plus 源代码
下载地址: 免注册下载 普通下载 VIP

相关代码

				// left.cpp -- string function with a default argument
				#include 
				const int ArSize = 80;
				char * left(const char * str, int n = 1);
				int main()
				{
				    using namespace std;
				    char sample[ArSize];
				    cout 				    cin.get(sample,ArSize);
				    char *ps = left(sample, 4);
				    cout 				    delete [] ps;       // free old string
				    ps = left(sample);
				    cout 				    delete [] ps;       // free new string
				    return 0;
				}
				
				// This function returns a pointer to a new string
				// consisting of the first n characters in the str string.
				char * left(const char * str, int n)
				{
				    if(n < 0)
				        n = 0;
				    char * p = new char[n+1];
				    int i;
				    for (i = 0; i < n && str[i]; i++)
				        p[i] = str[i];  // copy characters
				    while (i 				        p[i++] = '\0';  // set rest of string to '\0'
				    return p; 
				}
							

相关资源