本文整理汇总了C++中std::ostringstream类的典型用法代码示例。如果您正苦于以下问题:C++ ostringstream类的具体用法?C++ ostringstream怎么用?C++ ostringstream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ostringstream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: throw
void Logger::error(std::ostringstream& stream) throw()
{
string text = stream.str();
error(text.data());
}
示例2: initDrawLayer
Mpdshape* initDrawLayer(ofstream* f, TVector3 rot, TVector3 pos, Double_t alpha) {
points.str(""); position.str(""); rotation.str("");
// straw layer helpers
points << 0.0 << " " << 0.0 << " " << -layerThickness/2.0 << endl;
points << innerRadius << " " << outerRadius << endl;
points << 0.0 << " " << 0.0 << " " << layerThickness/2.0;
position << 0.0 << " " << 0.0 << " " << (initDist+layerThickness/2.0);
// straw layer definition
std::ostringstream layername;
layername << "stt01layer";
if (alpha > 0) {
layername << "right";
} else if (alpha < 0) {
layername << "left";
} else {
layername << "radial";
}
Mpdshape* layerR = new Mpdshape(f, (layername.str()).c_str(), "cave", "TUBE", "air",
points.str(),
position.str());
layerR->SetSegment(1);
// straw tube helpers
points.str(""); position.str(""); rotation.str("");
points << 0.0 << " " << 0.0 << " " << 0 << endl;
points << 0.0 << " " << tubeRadius << endl;
points << 0.0 << " " << 0.0 << " " << (outerRadius-innerRadius);
std::ostringstream tubename;
tubename << "stt01tube";
if (alpha > 0) {
tubename << "right";
} else if (alpha < 0) {
tubename << "left";
} else {
tubename << "radial";
}
// straw tube definition
Mpdshape* tube = new Mpdshape(f, (tubename.str()).c_str(), (layername.str()).c_str(), "TUBE", "kapton", points.str());
tube->SetSegment(1);
tube->SetMotherSegment(1);
// straw gas helpers
points.str(""); position.str(""); rotation.str("");
points << 0.0 << " " << 0.0 << " " << 0 << endl;
points << 0.0 << " " << gasRadius << endl;
points << 0.0 << " " << 0.0 << " " << (outerRadius-innerRadius);
std::ostringstream gasname;
gasname << "stt01gas";
if (alpha > 0) {
gasname << "right";
} else if (alpha < 0) {
gasname << "left";
} else {
gasname << "radial";
}
// straw gas definition
Mpdshape* gas = new Mpdshape(f, (gasname.str()).c_str(), (tubename.str()).c_str(), "TUBE", "DCHmixture",
points.str());
gas->SetSegment(1);
gas->SetMotherSegment(1);
// straw wire helpers
points.str(""); position.str(""); rotation.str("");
points << 0.0 << " " << 0.0 << " " << 0 << endl;
points << 0.0 << " " << wireRadius << endl;
points << 0.0 << " " << 0.0 << " " << (outerRadius-innerRadius);
std::ostringstream wirename;
wirename << "stt01wire";
if (alpha > 0) {
wirename << "right";
} else if (alpha < 0) {
wirename << "left";
} else {
wirename << "radial";
}
// straw wire definition
Mpdshape* wire = new Mpdshape(f, (wirename.str()).c_str(), (gasname.str()).c_str(), "TUBE", "copper",
points.str());
wire->SetSegment(1);
wire->SetMotherSegment(1);
// output first layer definition, moved to initial position and rotated(!)
layerR->SetRotation(rot.X(), rot.Y(), rot.Z());
layerR->SetPosition(pos.X(), pos.Y(), pos.Z());
layerR->DumpWithIncrement();
// output first tube definition
// first tube should be rotated too !!
tube->SetRotation(alpha+angleStraws, 90, 0);
TVector3 pos = getPosition(angleStraws);
tube->SetPosition(pos.X(), pos.Y(), pos.Z());
tube->DumpWithIncrement();
//.........这里部分代码省略.........
示例3: check
void test1() { // 3809
check("void f(double*&p) {\n"
" p = malloc(0x100);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
示例4: ptrptr
void ptrptr() {
check("void f() {\n"
" char **p = malloc(10);\n"
"}");
ASSERT_EQUALS("[test.c:3]: (error) Memory leak: p\n", errout.str());
}
示例5:
~ExpectedStreamOutput()
{
original_stream.rdbuf(&original_streambuf);
original_stream << substituted_stream.str();
}
示例6: main
int main(int argc, char **argv)
{
MPI_Init(&argc, &argv);
LONG_DESC << "This program simulates a transport problem on a sphere"
" according to a benchmark from a Nair & Lauritzen paper.\n"
<< "It starts with a partitioned mesh on a sphere, add a tracer, and steps through.\n" <<
"The flow reverses after half time, and it should return to original configuration, if the integration was exact. ";
ProgOptions opts(LONG_DESC.str(), BRIEF_DESC);
// read a homme file, partitioned in 16 so far
std::string fileN= TestDir + "/HN16.h5m";
const char *filename_mesh1 = fileN.c_str();
opts.addOpt<double>("gtolerance,g",
"geometric absolute tolerance (used for point concidence on the sphere)", >ol);
std::string input_file;
opts.addOpt<std::string>("input_file,i", "input mesh file, partitioned",
&input_file);
std::string extra_read_opts;
opts.addOpt<std::string>("extra_read_options,O", "extra read options ",
&extra_read_opts);
//int field_type;
opts.addOpt<int>("field_type,f",
"field type-- 1: quasi-smooth; 2: smooth; 3: slotted cylinders (non-smooth)", &field_type);
opts.addOpt<int>("num_steps,n",
"number of steps ", &numSteps);
//bool reorder = false;
opts.addOpt<void>("write_debug_files,w", "write debugging files during simulation ",
&writeFiles);
opts.addOpt<void>("write_velocity_files,v", "Reorder mesh to group entities by partition",
&velocity);
opts.addOpt<void>("write_result_in_parallel,p", "write tracer result files",
¶llelWrite);
opts.parseCommandLine(argc, argv);
if (!input_file.empty())
filename_mesh1=input_file.c_str();
// read in parallel, in the "euler_set", the initial mesh
std::string optsRead = std::string("PARALLEL=READ_PART;PARTITION=PARALLEL_PARTITION")+
std::string(";PARALLEL_RESOLVE_SHARED_ENTS")+extra_read_opts;
Core moab;
Interface & mb = moab;
EntityHandle euler_set;
ErrorCode rval;
rval = mb.create_meshset(MESHSET_SET, euler_set);
CHECK_ERR(rval);
rval = mb.load_file(filename_mesh1, &euler_set, optsRead.c_str());
ParallelComm* pcomm = ParallelComm::get_pcomm(&mb, 0);
CHECK_ERR(rval);
rval = pcomm->check_all_shared_handles();
CHECK_ERR(rval);
int rank = pcomm->proc_config().proc_rank();
if (0==rank)
{
std::cout << " case 1: use -gtol " << gtol <<
" -R " << radius << " -input " << filename_mesh1 << " -f " << field_type <<
" numSteps: " << numSteps << "\n";
std::cout<<" write debug results: " << (writeFiles ? "yes" : "no") << "\n";
std::cout<< " write tracer in parallel: " << ( parallelWrite ? "yes" : "no") << "\n";
std::cout <<" output velocity: " << (velocity? "yes" : "no") << "\n";
}
// tagTracer is the value at nodes
Tag tagTracer = 0;
std::string tag_name("Tracer");
rval = mb.tag_get_handle(tag_name.c_str(), 1, MB_TYPE_DOUBLE, tagTracer, MB_TAG_DENSE | MB_TAG_CREAT);
CHECK_ERR(rval);
// tagElem is the average computed at each element, from nodal values
Tag tagElem = 0;
std::string tag_name2("TracerAverage");
rval = mb.tag_get_handle(tag_name2.c_str(), 1, MB_TYPE_DOUBLE, tagElem, MB_TAG_DENSE | MB_TAG_CREAT);
CHECK_ERR(rval);
// area of the euler element is fixed, store it; it is used to recompute the averages at each
// time step
Tag tagArea = 0;
std::string tag_name4("Area");
rval = mb.tag_get_handle(tag_name4.c_str(), 1, MB_TYPE_DOUBLE, tagArea, MB_TAG_DENSE | MB_TAG_CREAT);
CHECK_ERR(rval);
// add a field value, quasi smooth first
rval = add_field_value(&mb, euler_set, rank, tagTracer, tagElem, tagArea);
CHECK_ERR(rval);
// iniVals are used for 1-norm error computation
Range redEls;
//.........这里部分代码省略.........
示例7: sizeofForArrayParameter
void sizeofForArrayParameter() {
check("void f() {\n"
" int a[10];\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" unsigned int a = 2;\n"
" unsigned int b = 2;\n"
" int c[(a+b)];\n"
" std::cout << sizeof(c) / sizeof(int) << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" unsigned int a = { 2 };\n"
" unsigned int b[] = { 0 };\n"
" int c[a[b[0]]];\n"
" std::cout << sizeof(c) / sizeof(int) << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" unsigned int a[] = { 1 };\n"
" unsigned int b = 2;\n"
" int c[(a[0]+b)];\n"
" std::cout << sizeof(c) / sizeof(int) << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int a[] = { 1, 2, 3 };\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int a[3] = { 1, 2, 3 };\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f( int a[]) {\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (error) Using 'sizeof' on array given as "
"function argument returns size of a pointer.\n", errout.str());
check("void f( int a[]) {\n"
" std::cout << sizeof a / sizeof(int) << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (error) Using 'sizeof' on array given as "
"function argument returns size of a pointer.\n", errout.str());
check("void f( int a[3] ) {\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (error) Using 'sizeof' on array given as "
"function argument returns size of a pointer.\n", errout.str());
check("void f(int *p) {\n"
" p[0] = 0;\n"
" int unused = sizeof(p);\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" char p[] = \"test\";\n"
" int unused = sizeof(p);\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
// ticket #2495
check("void f() {\n"
" static float col[][3]={\n"
" {1,0,0},\n"
" {0,0,1},\n"
" {0,1,0},\n"
" {1,0,1},\n"
" {1,0,1},\n"
" {1,0,1},\n"
" };\n"
" const int COL_MAX=sizeof(col)/sizeof(col[0]);\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
//.........这里部分代码省略.........
示例8: ExpectedStreamOutput
ExpectedStreamOutput(std::ostream &stream,const char *expected_)
: expected(expected_), original_stream(stream), original_streambuf(*(stream.rdbuf(substituted_stream.rdbuf())))
{
}
示例9: sizeofForNumericParameter
void sizeofForNumericParameter() {
check("void f() {\n"
" std::cout << sizeof(10) << std::endl;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.\n", errout.str());
check("void f() {\n"
" std::cout << sizeof(-10) << std::endl;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.\n", errout.str());
check("void f() {\n"
" std::cout << sizeof 10 << std::endl;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.\n", errout.str());
check("void f() {\n"
" std::cout << sizeof -10 << std::endl;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.\n", errout.str());
}
示例10: check
bool check()
{
return substituted_stream.str() == expected;
}
示例11: checkPointerSizeof
void checkPointerSizeof() {
check("void f() {\n"
" char *x = malloc(10);\n"
" free(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int *x = malloc(sizeof(*x));\n"
" free(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int *x = malloc(sizeof(int));\n"
" free(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int *x = malloc(sizeof(x));\n"
" free(x);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Size of pointer 'x' used instead of size of its data.\n", errout.str());
check("void f() {\n"
" int *x = malloc(sizeof(&x));\n"
" free(x);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Size of pointer 'x' used instead of size of its data.\n", errout.str());
check("void f() {\n"
" int *x = malloc(100 * sizeof(x));\n"
" free(x);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Size of pointer 'x' used instead of size of its data.\n", errout.str());
check("void f() {\n"
" int *x = malloc(sizeof(x) * 100);\n"
" free(x);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Size of pointer 'x' used instead of size of its data.\n", errout.str());
check("void f() {\n"
" int *x = malloc(sizeof *x);\n"
" free(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int *x = malloc(sizeof x);\n"
" free(x);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Size of pointer 'x' used instead of size of its data.\n", errout.str());
check("void f() {\n"
" int *x = malloc(100 * sizeof x);\n"
" free(x);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Size of pointer 'x' used instead of size of its data.\n", errout.str());
check("void f() {\n"
" int *x = calloc(1, sizeof(*x));\n"
" free(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int *x = calloc(1, sizeof *x);\n"
" free(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int *x = calloc(1, sizeof(x));\n"
" free(x);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Size of pointer 'x' used instead of size of its data.\n", errout.str());
check("void f() {\n"
" int *x = calloc(1, sizeof x);\n"
" free(x);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Size of pointer 'x' used instead of size of its data.\n", errout.str());
check("void f() {\n"
" int *x = calloc(1, sizeof(int));\n"
" free(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" char x[10];\n"
" memset(x, 0, sizeof(x));\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" char* x[10];\n"
" memset(x, 0, sizeof(x));\n"
//.........这里部分代码省略.........
示例12: a
void assign11() { // #3942 - FP for x = a(b(p));
check("void f() {\n"
" char *p = malloc(10);\n"
" x = a(b(p));\n"
"}");
ASSERT_EQUALS("[test.c:4]: (information) --check-library: Function b() should have <use>/<ignore> configuration\n", errout.str());
}
示例13: sizeofVoid
void sizeofVoid() {
check("void f() {\n"
" int size = sizeof(void);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (portability) Behaviour of 'sizeof(void)' is not covered by the ISO C standard.\n", errout.str());
check("void f() {\n"
" void* p;\n"
" int size = sizeof(*p);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) '*p' is of type 'void', the behaviour of 'sizeof(void)' is not covered by the ISO C standard.\n", errout.str());
check("void f() {\n"
" void* p = malloc(10);\n"
" int* p2 = p + 4;\n"
" int* p3 = p - 1;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) 'p' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n"
"[test.cpp:4]: (portability) 'p' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n", errout.str());
check("void f() {\n"
" void* p1 = malloc(10);\n"
" void* p2 = malloc(5);\n"
" p1--;\n"
" p2++;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (portability) 'p1' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n"
"[test.cpp:5]: (portability) 'p2' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n", errout.str());
check("void f() {\n"
" void** p1 = malloc(10);\n"
" p1--;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" void** p1;\n"
" int j = sizeof(*p1);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" void* p1[5];\n"
" int j = sizeof(*p1);\n"
"}");
ASSERT_EQUALS("", errout.str());
// Calculations on void* with casts
check("void f(void *data) {\n"
" *((unsigned char *)data + 1) = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(void *data) {\n"
" *((unsigned char *)(data) + 1) = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(void *data) {\n"
" unsigned char* c = (unsigned char *)(data + 1);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (portability) 'data' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n", errout.str());
check("void f(void *data) {\n"
" unsigned char* c = (unsigned char *)data++;\n"
" unsigned char* c2 = (unsigned char *)++data;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (portability) 'data' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n"
"[test.cpp:3]: (portability) 'data' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n", errout.str());
check("void f(void *data) {\n"
" void* data2 = (void *)data + 1;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (portability) 'data' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n", errout.str());
// #4908 (void pointer as a member of a struct/class)
check("struct FOO {\n"
" void *data;\n"
"};\n"
"char f(struct FOO foo) {\n"
" char x = *((char*)(foo.data+1));\n"
" foo.data++;\n"
" return x;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (portability) 'foo.data' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n"
"[test.cpp:6]: (portability) 'foo.data' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n", errout.str());
check("struct FOO {\n"
" void *data;\n"
"};\n"
"char f(struct FOO foo) {\n"
" char x = *((char*)foo.data+1);\n"
" return x;\n"
"}\n"
"char f2(struct FOO foo) {\n"
" char x = *((char*)((FOO)foo).data + 1);\n"
" return x;\n"
"}\n"
"char f3(struct FOO* foo) {\n"
//.........这里部分代码省略.........
示例14: deallocThrow
void deallocThrow()
{
check("int * p;\n"
"void f(int x)\n"
"{\n"
" delete p;\n"
" if (x)\n"
" throw 123;\n"
" p = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Throwing exception in invalid state, p points at deallocated memory\n", errout.str());
}
示例15: check
void test6() { // ticket #2602 segmentation fault
check("class A {\n"
" A& operator=(const A&);\n"
"};");
ASSERT_EQUALS("", errout.str());
}