C++ vector iterator std::ref(vec)
阿新 • • 發佈:2022-03-19
void Util::getStructVector(vector<BookStruct> &vec, int len) { for (int i = 0; i < len; i++) { BookStruct bs; bs.BookIndex = i; bs.BookId = (unsigned long long)i * i * i * i * i * i; bs.BookName = (char *)malloc(40); bs.BookTitle = (char *)malloc(40); strcpy(bs.BookName, getUuid27()); strcpy(bs.BookTitle, getUuid27()); vec.push_back(bs); } } char *Util::getUuid27() { uuid_t newUUID; uuid_generate(newUUID); uuid_unparse(newUUID, Util::uuidValue); return Util::uuidValue; } void Util::printStructVector32(vector<BookStruct> &vec) { vector<BookStruct>::iterator itr = vec.begin(); while (itr != vec.end()) { cout << fixed << "Index=" << itr->BookIndex << ",Id=" << itr->BookId << ",Name=" << itr->BookName << ",Title=" << itr->BookTitle << endl;free(itr->BookName); free(itr->BookTitle); itr++; } } void Util::structVector33(int len) { vector<BookStruct> vec; getStructVector(std::ref(vec), len); printStructVector32(std::ref(vec)); cout << "Finished in void Util::structVector33(int len) and now is " << getTimeNow() << endl; }
g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -luuid -lpthread
time ./h1 10000