本文整理汇总了C++中std::ostringstream::rdbuf方法的典型用法代码示例。如果您正苦于以下问题:C++ ostringstream::rdbuf方法的具体用法?C++ ostringstream::rdbuf怎么用?C++ ostringstream::rdbuf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::ostringstream
的用法示例。
在下文中一共展示了ostringstream::rdbuf方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GIVEN
#include <lab14a.h>
#include <doctest.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
SCENARIO( "treat animals") {
GIVEN( "an vector of unique_ptr<Animal>" ) {
auto zoo = make_animals();
WHEN( "n is 1" ) {
THEN( "each animal should complain about a shot" ) {
std::ostringstream actual;
auto cout_buff = std::cout.rdbuf(); // save pointer to std::cout buffer
std::cout.rdbuf(actual.rdbuf()); // sub local for cout
treat_animals(zoo);
std::cout.rdbuf(cout_buff); // restore cout
std::string expected = "meow.\n";
auto pos = actual.str().find_first_of(expected);
REQUIRE_MESSAGE(pos != std::string::npos, "Did not give a Cat a shot");
expected = "arf, arf!\n";
pos = actual.str().find_first_of(expected);
REQUIRE_MESSAGE(pos != std::string::npos, "Did not give a Dog a shot");
expected = "Hoot!\n";
pos = actual.str().find_first_of(expected);
REQUIRE_MESSAGE(pos != std::string::npos, "Did not give a Owl a shot");
示例2: defined
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#if defined(_MSC_VER)
#pragma warning (push)
#pragma warning (disable : 4189) // local variable is initialized but not referenced
#endif
TEST_CASE("README", "[hide]")
{
{
// redirect std::cout for the README file
auto old_cout_buffer = std::cout.rdbuf();
std::ostringstream new_stream;
std::cout.rdbuf(new_stream.rdbuf());
{
// create an empty structure (null)
json j;
// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
// add a Boolean that is stored as bool
j["happy"] = true;
// add a string that is stored as std::string
j["name"] = "Niels";
// add another null object by passing nullptr
j["nothing"] = nullptr;
示例3: out
// Copyright (C) 2016 Jonathan Müller <[email protected]>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#include <standardese/output.hpp>
#include <catch.hpp>
using namespace standardese;
TEST_CASE("output_stream_base")
{
std::ostringstream str;
streambuf_output out(*str.rdbuf());
SECTION("newline test")
{
out.write_char('a');
out.write_new_line();
out.write_str("b\n", 2);
out.write_new_line();
out.write_str("c ignore me", 1);
out.write_blank_line();
out.write_str("d\n", 2);
out.write_blank_line();
REQUIRE(str.str() == "a\nb\nc\n\nd\n\n");
}
示例4: Serialize
Serialize(const void* Buffer, std::size_t BufferSize) : Data()
{
Data.rdbuf()->pubsetbuf(const_cast<char*>(reinterpret_cast<const char*>(Buffer)), BufferSize);
}
示例5: ExpectedStreamOutput
ExpectedStreamOutput(std::ostream &stream,const char *expected_)
: expected(expected_), original_stream(stream), original_streambuf(*(stream.rdbuf(substituted_stream.rdbuf())))
{
}
示例6: Hello
* Sources :
*/
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <sstream>
#include <iostream>
#include <string>
#include "lab_5.h"
using std::string;
TEST_CASE("Hello Function") {
string cout_output;
std::streambuf* oldCout = cout.rdbuf();
std::ostringstream captureCout;
cout.rdbuf(captureCout.rdbuf());
Hello();
cout.rdbuf(oldCout);
cout_output = captureCout.str();
SECTION("Hello()") {
CHECK(cout_output == "Hello world!");
}
captureCout.str("");
}
TEST_CASE("Print Message Function") {
string cout_output;
std::streambuf* oldCout = cout.rdbuf();
std::ostringstream captureCout;
cout.rdbuf(captureCout.rdbuf());
PrintMessage("Hello again!");