std::string::size()的坑
字符串
https://ac.nowcoder.com/acm/problem/18386
one
#include <bits/stdc++.h>
using namespace std;
int main() {
int judge[270];
int num = 0;
int acc = 0;
int l = 0;
int r = 0;
string c;
cin >> c;
int len = c.size();
int result = len + 1;
memset(judge, 0, sizeof(judge));
while (l < c.size()) {
while (r < c.size() && acc < 26) {
judge[c[r] - 'a']++;
if (judge[c[r++] - 'a'] == 1) {
acc++;
}
}
if (acc == 26) {
result = min(result, r - l + 1);
if (judge[c[l] - 'a'] == 1) acc--;
}
judge[c[l] - 'a']--;
l++;
}
cout << result;
return 0;
}
c++stl库中string.length()和string.size()的坑
length()和size()的返回值都是size_t类型,也就是unsigned int,在遇到下面情况,会出现不符合你预想的情况:
#include<iostream>
#include<string>
using namespace std;
int main(){
string haystack = "";
string needdle = "a";
int i=0;
cout<<haystack.size()<<endl;
cout<<needdle.size()<<endl;
cout<<haystack.size()-needdle.size();
return 0;
}
//输出为false
*signed int和unsigned int运算时,会进行类型提升,*signed int会提升为unsigned int,-1就变成了4294967295。
另外,string的length和size方法没有任何区别。 ———————————————— 版权声明:本文为CSDN博主「乐观的神」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_35926606/article/details/116503944