C++用法的学习心得

说实话我对C++语言不是很懂,更谈不上对用法的了解了。我一看到C++大量的英文组合就头疼,每次上课也只是一知半解。我就像以下图片中的人一样,老师上课讲,我在下面昏昏欲睡。

 

如:

Lab Exercise  — Overloading printArray

#include <iostream>

using std::cout;

using std::endl;

template< typename T >

void printArray( const T *array, int count ){

for ( int i = 0; i < count; i++ )

cout << array[ i ] << " ";

cout << endl;    }

template< typename T >

int printArray(const T *array,int count,  int lowSubscrip, int highSubscript ){

if(highSubscript<=0||lowSubscrip<0||lowSubscrip>=highSubscript||count<(highSubscript-lowSubscrip))

return 0;   

int coun = 0;

for ( int i=lowSubscrip;i<=highSubscript;i++){

coun++;

cout << array[ i ] << ' ';   }

cout<< '\n';

return coun;  }

int main()

{

const int ACOUNT = 5;

const int BCOUNT = 7;

const int CCOUNT = 6;      

int a[ ACOUNT ] = { 1, 2, 3, 4, 5 };

double b[ BCOUNT ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };

char c[ CCOUNT ] = "HELLO";

int elements;

cout << "\nUsing original printArray function\n";

printArray( a, ACOUNT );

cout << "Array a contains:\n";

elements = printArray(a,ACOUNT,0,ACOUNT-1);

cout << elements << " elements were output\n";out << "Array a from positions 1 to 3 is:\n";

elements = printArray(a,ACOUNT,1,3);

cout << elements << " elements were output\n";

cout << "Array a output with invalid subscripts:\n";

elements = printArray(a,ACOUNT,-1,10);

cout << elements << " elements were output\n\n";

cout << "\nUsing original printArray function\n";

printArray( b, BCOUNT );

cout << "Array b contains:\n";

elements = printArray(b,BCOUNT,0,BCOUNT - 1);

cout << elements << " elements were output\n";

cout << "Array b from positions 1 to 3 is:\n";

elements = printArray(b,BCOUNT,1,3);

cout << elements << " elements were output\n";

cout << "Array b output with invalid subscripts:\n";

elements =printArray(b,BCOUNT,-1,10);

cout << elements << " elements were output\n\n";

cout << "\nUsing original printArray function\n";

printArray( c, CCOUNT );

cout << "Array c contains:\n";

elements =printArray(c,CCOUNT,0, CCOUNT - 2);

cout << elements << " elements were output\n";

cout << "Array c from positions 1 to 3 is:\n";

elements =printArray(c,CCOUNT,1, 3);

cout<< elements << " elements were output\n";

cout << "Array c output with invalid subscripts:\n";

elements = printArray(c,CCOUNT,-1, 10);

cout << elements << " elements were output" << endl;     

return 0;

}

这段代码看了就头大,我只会基本的,知道到开头是:#include <iostream>

输入cin,输出cout等等最简单的,一旦混在一起我就两眼抓瞎。

 

posted @ 2015-09-09 16:47  韩书星  阅读(189)  评论(0编辑  收藏  举报