重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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;
}
排序算法模板
选择排序函数模板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]);
}
}
冒泡排序函数模板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;
}
}
}
插入排序函数模板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;
}
}
快速排序函数模板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);
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了威宁免费建站欢迎大家使用!