我目前正在使用以下代码来修正std::strings程序中的所有内容:
std::string s;
s.erase(s.find_last_not_of(" \n\r\t")+1);
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我想知道是否有一些可能会失败的最终案例?
当然,欢迎使用优雅替代品和左侧解决方案的答案.
我有这个代码,但是当我输入一个超过整数值的值时它会打印垃圾。我实际上有 if 条件来检查范围。如何使它工作?我知道这对你来说很容易。我只是忘记了一些 C++ 的东西,我没有时间。
#include <iostream>
#include <typeinfo>
#include<cfloat>
#include<climits>
using namespace std;
int g1, g2;
string text = " year";
string text2 = " age";
int main() {
g1 = 2100000000000000000000000;
g2 = 16;
if (g1 < INT_MIN || g1 > INT_MAX) {
std::cout << "Please enter an integer";
} else {
std::cout << g1 << text;
std::cout << g2 << text2;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) int a, b;
cin >> a >> b;
cout << a << endl << b;
Run Code Online (Sandbox Code Playgroud)
input1:3.5 5.5
input2:3 5.5
考虑下面的代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
int sum = 0;
for (auto &it : a) {
cin >> it;
sum += it;
}
cout << sum << "\n";
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
输入类似(或任何大于 INT_MAX 的值到 k 中)
5 1234567891564
1 2 3 4 …Run Code Online (Sandbox Code Playgroud)