cin输入加速
此段代码粘贴在类外
static auto x = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
return 0;
}();
正无穷和负无穷表示
可以使用如下定义,这样定义的好处是正无穷+正无穷仍然是正无穷,负无穷同理。
const int MAX = 0x3F3F3F3F;
const int MIN = 0xC0C0C0C0;
或者直接使用头文件<limits>中的定义:
#include <limits>
const int MAX = INT_MAX;
const int MIN = INT_MIN;
string字符串大小写转换
#include <algorithm>
string s = "abc ,ACB";
transform(s.begin(), s.end(), s.begin(), toupper); // 转换为大写
transform(s.begin(), s.end(), s.begin(), tolower); // 转换为小写
单个字符大小写转换
char c1 = tolower('A');
char c2 = toupper('d');
数字==>字符串
#include <string>
string s = to_string(231);
字符串==>数字
#include <string>
string s = "123.43dsfe";
int i = stoi(s);
long l = stol(s);
float f = stof(s);
double d = stod(s);
分割字符串
C++中没有现成的split函数,需要自己实现
#include <vector>
#include <iterator>
string original = "ni hha是我们!的!d he!!llo world!ds "; // 原始字符串
string split = " !我"; // 分割字符串
/***********************************/
vector<string> res; // 输出的容器
string temp;
for (char c : original) {
if (split.find(c) == -1)
temp += c;
else if (!temp.empty()) {
res.push_back(temp);
temp.clear();
}
}
if (!temp.empty())
res.push_back(temp);
/***********************************/
// 测试结果
copy(res.cbegin(), res.cend(), ostream_iterator<string>(cout, "\n"));