【问题标题】:how do i configure `stdin` to read from a c++ string?如何配置“stdin”以读取 C++ 字符串?
【发布时间】:2021-02-22 15:07:38
【问题描述】:

考虑一个 cpp 函数solve()。数字字符串a(示例测试字符串)作为参数传递给函数solve()。 我想让stdin 读取字符串a 中的数字。

这实际上是一个压力测试。所以在这个压力测试中,这个函数solve()被提供了一个字符串到return,这个结果将针对另一个函数solveFast()获得的另一个结果进行测试。

注意:- 函数 solve() 中的算法已经提供给我们。我希望针对我自己的算法(solveFast())对该算法进行压力测试。保证 solve() 函数中的算法针对其测试输入提供正确的输出

#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::stringstream&gt;&gt; 运算符,就像 std::cin

标签: c++ stdin stress-testing


【解决方案1】:

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");
}

【讨论】:

  • 在第 3 行我们可以写 std::istringstream&amp; input 而不是 std::istream&amp; input 吗?我知道istringstream 是一个容器……但是istream 呢?istream 也是一个容器吗?抱歉这个愚蠢的问题......但我需要知道......请解释一下......
  • 我不明白。请研究如何以及什么是继承。不,您不能写solve(std::istringstream&amp;,因为std::cinstd::istream 类型,而std::istream 不继承自std::istringstreamIs istream a container too? 好吧,我想说两者都是“流”。 cppreference I/O 是一个很好的资源,但我推荐一本很好的 C++ 书籍。
猜你喜欢
  • 2018-09-15
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 2014-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多