c++小积累
luckyliu99
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
字符串替换
在c++中字符串替换需要用到replace()函数。常用模式:replace(pos,cnt,“target”)pos表示原字符串需要替换的位置cnt表示需要替换的字符个数“terget”表示需要替换成的字符串#include <iostream>#include <string>using namespace std;int main() { string s; getline(cin, s); int pos = 0; while ((pos = (s.原创 2020-11-21 18:24:04 · 611 阅读 · 0 评论 -
整数输入可含前缀0
在处理一道时间问题的输入时,因为时间都是以“hh:mm”形式输入的,对于<10的小时就会出现前缀0,刚开始以为不能直接用int接收,还用字符串接受再进行处理的,后来发现直接用int完全ok.#include <iostream>#include <string>#include <cstdio>#include <algorithm>using namespace std;int main() { int hh, mm; char c;原创 2020-11-19 17:57:00 · 289 阅读 · 0 评论 -
删除字符串中的特定字符
当要删除字符串中的特定字符时,只需要用find()和erase()两个函数搭配使用即可,find()用于找到特定字符的位置,而erase()则用于删除该位置上的字符。find(c,pos)表示从pos这个位置开始找c字符erase(pos,n)表示从pos这个位置开始删除n个字符例如下面就是我写的一个删除输入字符串中所有字符“a”的代码。#include <iostream>#include <string>#include <algorithm>usi原创 2020-11-19 11:19:37 · 4660 阅读 · 0 评论 -
string转换成char*
在写一个题目的时候,发现需要用到将字符串中的字符排序,如果将string转换成char数组的形式就可以用sort()直接写,翻阅大佬博客后,发现有如下三种方法:c_str()#include <iostream>#include <string>#include <algorithm>using namespace std;int main() { string s; cin >> s; char *p = (char *)s.c_st转载 2020-11-18 12:56:02 · 920 阅读 · 0 评论 -
string字符串输入
cin和cout写输入输出非常方便,但是用cin输入string字符串时,默认遇到空格回车制表符等空白字符即字符串输入结束。#include <iostream>#include <string>using namespace std;int main() { string a, b; cin >> a >> b; cout << "a:" << a << endl; cout << "b:"原创 2020-11-18 12:35:51 · 10393 阅读 · 0 评论 -
输出左右对齐
有些题目的输出要求占多大的宽度以及左对齐还是右对齐,属于常识性的语言问题,记录一下。右对齐#include <iostream>#include <cstdio>using namespace std;int main() { for (int i = 1; i <= 100; i++) { printf("%5d", i);//右对齐 if (i % 5 == 0) cout << endl; } return 0;}/*原创 2020-11-18 10:22:47 · 659 阅读 · 0 评论 -
结构体排序问题
关于如何对结构体排序,自己做个小总结,方面以后查阅。单项排序#include <iostream>#include <algorithm>using namespace std;struct s { int a; int b; int c;};bool cmp(s s1, s s2) { return s1.b < s2.b; //b小的在前面}s stu[4] = {{1, 2, 3}, {3, 1, 4}, {2, 2, 4}, {0, 2,原创 2020-11-18 10:14:23 · 363 阅读 · 0 评论 -
最大公约数和最小公倍数
借鉴大佬博客,递归求解太妙了,拿小本本记下来int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b;}int lcm(int a, int b){ return a * b / gcd(a, b);}参考链接:https://blog.youkuaiyun.com/qq_44324091/article/details/89819655原创 2020-11-17 23:25:22 · 150 阅读 · 0 评论
分享