ifstream ifs("1.txt");
if (ifs.is_open()) { ifs.seekg(0, ios::end);
streamoff len = ifs.tellg(); // streamoff == long long
cout<< len<< endl;
}
ifs.close();
按照分隔符截取單詞void split(const string& line, const string& delim, vector& arr)
{int start = 0;
int pos = 0;
while (line.find(delim, start) != string::npos) {pos = line.find(delim, start);
arr.push_back(line.substr(start, pos - start));
start = pos + delim.size();
}
if (start< line.size()) {arr.push_back(line.substr(start));
}
}
去除前后空格,并刪除單詞間多余空格string strip(const string& line)
{int pos_start = line.find_first_not_of(" \r\n\t");
int pos_end = line.find_last_not_of(" \r\n\t");
if (pos_start == string::npos || pos_end == string::npos) {return line;
}
string res = line.substr(pos_start, pos_end - pos_start + 1);
int pos = res.find(" ");
while (pos != -1) {res.replace(pos, 2, " ");
pos = res.find(" ", pos);
}
return res;
}
排序算法模板
選擇排序函數(shù)模板templatevoid selectionSort(T &arr, FUNC func)
{int n = static_cast(arr.size());
for (int i = 0; i< n - 1; ++i) {int idx = i;
for (int j = i + 1; j< n; ++j) {if (func(arr[j], arr[idx])) {idx = j;
}
}
swap(arr[idx], arr[i]);
}
}
冒泡排序函數(shù)模板templatevoid bubbleSort(T &arr, FUNC func)
{int n = static_cast(arr.size());
for (int i = 0; i< n - 1; ++i) {bool flag = false;
for (int j = 0; j< n - 1 - i; ++j) {if (func(arr[j + 1], arr[j])) {swap(arr[j + 1], arr[j]);
flag = true;
}
}
if (flag) {break;
}
}
}
插入排序函數(shù)模板templatevoid insertionSort(T &arr, FUNC func)
{int n = static_cast(arr.size());
for (int i = 0; i< n; ++i) {int j = i;
auto tmp = arr[i];
while (j--) {if (func(tmp, arr[j])) {arr[j + 1] = arr[j];
} else {break;
}
}
arr[j + 1] = tmp;
}
}
快速排序函數(shù)模板templatevoid quickSort(T &arr, int start, int end, FUNC func)
{int right = end;
int left = start;
auto key = arr[start];
while (left< right) {while (left< right && !func(arr[right], key)) {--right;
}
arr[left] = arr[right];
while (left< right && func(arr[left], key)) {++left;
}
arr[right] = arr[left];
}
arr[left] = key;
if (start< right) {quickSort(arr, start, right - 1, func);
}
if (left< end) {quickSort(arr, left + 1, end, func);
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序定制開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了威寧免費建站歡迎大家使用!