拜ugg为偶师傅,为保证技术贴,大家一起来!
来csdn一个月了,先拜个师傅//。
// ***** C-style character string implementation *****
#include <iostream>;
#include <cstring>;
int main()
{
int errors = 0;
const char *pc = "a very long literal string";
for ( int ix = 0; ix < 1000000; ++ix )
{
int len = strlen( pc );
char *pc2 = new char[ len + 1 ];
strcpy( pc2, pc );
if ( strcmp( pc2, pc ))
++errors;
delete [] pc2;
}
cout << "C-style character strings: "
<< errors << " errors occurred.\n";
}
// ***** string implementation *****
#include <iostream>;
#include <string>;
int main() {
int errors = 0;
string str( "a very long literal string" );
for ( int ix = 0; ix < 1000000; ++ix )
{
int len = str.size();
string str2 = str;
if ( str != str2 )
++errors;
}
cout << "string class: "
<< errors << " errors occurred.\n";
}
// 看了c++ primer 有点不懂。。。。
第二段程序怎么还比第一段程序的效率高??