已经好几天没学习了,没打代码,之前过年走亲戚家各种事比较多,也和同学朋友出去玩了几天,所以一直没写代码,今天决定开始学习了。
今天做了道“字符串出现位置”,这道题是输入一个母字符串个一个子字符串,在母字符串中找出子字符串所在的位置,并输出。这道题我没写出来,看了下网上的代码:
#include
#include<stdio.h>
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
string a, b;
cin >> a >> b;
std::size_t ans = a.find(b);
cout << ((ans == std::string::npos) ? -1 : (int)ans) << endl;
return 0;
}
这个代码看不太懂,然后我又查了一些资料
1.“std::”是一个类(输入输出标准),包含了cin和cout成员,using namespace std,之后才能使用它的成员。
2.在64位环境里面sizt_t也是64位。但是int就不一定了。size_t是无符号的。
3.ans=a.find(b)的意思是在a中找到b的位置,并用ans记录位置。
4.((ans == std::string::npos) ? -1 : (int)ans) 三步运算符是代表,若ans大于0则输出ans,否则输出-1。
明天继续!
2019年2月8日学习日记
最新推荐文章于 2019-09-03 14:01:32 发布