#include <iostream>#include <string.h>#include <stdio.h>#include <time.h>using namespace std;clock_t start1,start2,end1,end2;//C-style character string implementationstart1 = clock();const char *pc = "a very long literal string";const size_t len = strlen(pc);for (size_t ix = 0; ix != 1000000; ++ix) { char *pc = new char[len + 1]; strcpy(pc2, pc); if (strcmp(pc2, pc)) ; delete [] pc2;}end1 = clock();cout << "C-style character need "<< end1-start1 << endl;//string implementationstart2 = clock();string str("a very long literal string");for (int ix = 0; ix != 1000000; ++ix) { string str2 = str; if (str != str2) ;}end2 = clock();cout << "C-style character need "<< end1-start1 << endl;
报错如下: [wsl@localhost timecmp]$ g++ -o a timecmp.cpp timecmp.cpp:9: error: expected constructor, destructor, or type conversion before ‘=’ token timecmp.cpp:13: error: expected unqualified-id before ‘for’ timecmp.cpp:13: error: expected constructor, destructor, or type conversion before ‘!=’ token timecmp.cpp:13: error: expected unqualified-id before ‘++’ token timecmp.cpp:20: error: expected constructor, destructor, or type conversion before ‘=’ token timecmp.cpp:22: error: expected constructor, destructor, or type conversion before ‘<<’ token timecmp.cpp:25: error: expected constructor, destructor, or type conversion before ‘=’ token timecmp.cpp:28: error: expected unqualified-id before ‘for’ timecmp.cpp:28: error: expected constructor, destructor, or type conversion before ‘!=’ token timecmp.cpp:28: error: expected unqualified-id before ‘++’ token timecmp.cpp:33: error: expected constructor, destructor, or type conversion before ‘=’ token timecmp.cpp:35: error: expected constructor, destructor, or type conversion before ‘<<’ token