c++ - Sorting a C-style array -
i'm stuck on 1 part of assignment. i'm not sure c-style array is. when sort normal array, sort function works. however, red squiggly in code try sort c-style array. there i'm doing wrong? appreciate help. in advance.
// goal: populate c-style array of 40 million elements random values between // 1 , 4 billion , sort via sort() algorithm. note should use new // operator allocate array. // note: array created/initialized with 40 million elements using new operator. start_time = time(null); // record start time { size_t *a1 = new size_t[forty_million]; (int = 0; < forty_million; ++i) { a1[i] = randomint(engine); } sort(a1.begin(), a1.end()); } end_time = time(null); // record end time total_time = end_time - start_time; // calculate time compute cout << "it took " << static_cast<long>(total_time) << " seconds compute part " << part++ << "\n" << endl;
i not sure mean red squiggly line
, guess is, ide highlights part of code indicating may not correct. based on question, best guess is, erroneous line is
sort(a1.begin(), a1.end());
and possibly because a1
not container. reasoning seconding on reference terminology c-style
array.
c-style
array non standard-library implementation of homogeneous collection of elements contiguous in memory.
such arrays not objects in pure terminology of object oriented, , not have methods (member functions in c++) , properties ( data members in c++).
naturally, may not able perform method call fetch start , end iterators, indicating begin , end of array.
fortunately there couple of way outs
if using c++11 , beyond, can leverage free standing functions std::begin , std::end instead of invoking member functions
sort(std::begin(a1), std::end(a1));
if size of array know, can add array end of array.
sort(a1, a1 + sizeof(a1)/sizeof(a1[0]));
or better
template < typename ty, size_t n > size_t countof( ty ( & arr )[ n ] ) { return n; } ................. sort(a1, a1 + n);
Comments
Post a Comment