我如何配置 `stdin` 以从 C++ 字符串中读取?
how do i configure `stdin` to read from a c++ string?
考虑一个 cpp 函数 solve()
。数字字符串 a
(示例测试字符串)作为参数传递给函数 solve()
。
我希望 stdin
读取字符串 a
.
中的数字
其实就是压力测试。因此,在此压力测试中,此函数 solve()
提供了一个字符串 return
结果,该结果将与从另一个函数 solveFast()
.
获得的另一个结果进行测试
NOTE:- Algorithm in the function solve()
is already given to us. I wish to stress-test this algorithm against my own algorithm (in solveFast()
). It is guaranteed that Algorithm in the function solve()
provides the correct output against its test inputs
#include <bits/stdc++.h>
using namespace std;
int solve(string s) {
// below given: an algorithm that
//uses stdin to take input sample test case (i.e.,not string s)
int n; //number of integers in string s
//ignore the purpose of the code below.
//Just observe that it is taking the inputs as cin (not from string s.
cin >> n;
int first, second, large;
for (int i = 0; i < n - 1; i++) {
if (i == 0) {
cin >> first >> second;
large = (second > first) ? second : first;
if (second < first) first = second;
} else {
cin >> second; // new num
if (second > large) {
first = large;
large = second;
}
else if(second > first){
first = second;
}
}
}
int result = large * first;
return result;
}
int solveFast(string s) {
/*
* my solution here
*/
return result;
}
int32_t main() {
//stress-testing code starts here
while (true) {
string a;
/*
* generating a sample test case and storing it in a string 'a'
*/
int res1 = solve(a);
int res2 = solveFast(a);
if(res1!=res2){
cout << "Wrong answer: " << res1 << " " << res2 << endl;
break;
}
else{
cout << "OK\n";
}
}
//stress-testig code ends
/*
for (int i = 1; i <= t; ++i) {
int n;
cin >> n;
vector<int> numbers(n);
for (int i = 0; i < n; i++) {
cin >> numbers[i];
}
auto result = solve(numbers);
cout << result << endl;
}
*/
return 0;
}
将 std::istream
传递给您的函数,然后从字符串构造一个 istream。作为一般规则,不要在要进行单元测试的代码中使用全局变量(如 std::cin
)。
#include <sstream>
#include <iostream>
int solve(std::istream& input, std::string s) {
std::string variable;
input >> variable;
return 0;
}
void unit_test_solve() {
std::istringstream input("some string");
solve(input, "bla");
}
int real_code() {
solve(std::cin, "bla");
}
考虑一个 cpp 函数 solve()
。数字字符串 a
(示例测试字符串)作为参数传递给函数 solve()
。
我希望 stdin
读取字符串 a
.
其实就是压力测试。因此,在此压力测试中,此函数 solve()
提供了一个字符串 return
结果,该结果将与从另一个函数 solveFast()
.
NOTE:- Algorithm in the function
solve()
is already given to us. I wish to stress-test this algorithm against my own algorithm (insolveFast()
). It is guaranteed that Algorithm in the functionsolve()
provides the correct output against its test inputs
#include <bits/stdc++.h>
using namespace std;
int solve(string s) {
// below given: an algorithm that
//uses stdin to take input sample test case (i.e.,not string s)
int n; //number of integers in string s
//ignore the purpose of the code below.
//Just observe that it is taking the inputs as cin (not from string s.
cin >> n;
int first, second, large;
for (int i = 0; i < n - 1; i++) {
if (i == 0) {
cin >> first >> second;
large = (second > first) ? second : first;
if (second < first) first = second;
} else {
cin >> second; // new num
if (second > large) {
first = large;
large = second;
}
else if(second > first){
first = second;
}
}
}
int result = large * first;
return result;
}
int solveFast(string s) {
/*
* my solution here
*/
return result;
}
int32_t main() {
//stress-testing code starts here
while (true) {
string a;
/*
* generating a sample test case and storing it in a string 'a'
*/
int res1 = solve(a);
int res2 = solveFast(a);
if(res1!=res2){
cout << "Wrong answer: " << res1 << " " << res2 << endl;
break;
}
else{
cout << "OK\n";
}
}
//stress-testig code ends
/*
for (int i = 1; i <= t; ++i) {
int n;
cin >> n;
vector<int> numbers(n);
for (int i = 0; i < n; i++) {
cin >> numbers[i];
}
auto result = solve(numbers);
cout << result << endl;
}
*/
return 0;
}
将 std::istream
传递给您的函数,然后从字符串构造一个 istream。作为一般规则,不要在要进行单元测试的代码中使用全局变量(如 std::cin
)。
#include <sstream>
#include <iostream>
int solve(std::istream& input, std::string s) {
std::string variable;
input >> variable;
return 0;
}
void unit_test_solve() {
std::istringstream input("some string");
solve(input, "bla");
}
int real_code() {
solve(std::cin, "bla");
}