C++刷题常用方法总结(持续更新...)

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")); 
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值