cout

Problem F: 字符串类(I)

≯℡__Kan透↙ 提交于 2019-12-08 07:03:11
Home Web Board ProblemSet Standing Status Statistics Problem F: 字符串类(I) Time Limit: 1 Sec Memory Limit: 128 MB Submit: 4684 Solved: 2302 [ Submit ][ Status ][ Web Board ] Description 封装一个字符串类,用于存储字符串和处理的相关功能,支持以下操作: 1. STR::STR()构造方法:创建一个空的字符串对象。 2. STR::STR(const char *)构造方法:创建一个字符串对象,串的内容由参数给出。 3. STR::length()方法:返回字符串的长度。 4. STR::putline()方法:输出串的内容,并换行。 ----------------------------------------------------------------------------- 你设计一个字符串类STR,使得main()函数能够正确运行。 函数调用格式见append.cc。 append.cc中已给出main()函数。 ----------------------------------------------------------------------------- Invalid Word

Program skips second cin

喜夏-厌秋 提交于 2019-12-08 05:26:48
问题 I'm making a C++ Mind Reader program, which is nearly complete. However, it feels the need to skip the second cin. I've searched and I'm not exactly sure what's wrong. I've examined the code and I bet I've done something stupid, but I am still baffled about it. The skipped cin is on line 32, here's my code: #include <iostream> #include <string> #include <cstdlib> using namespace std; int main() { //declaring variables to be used later string name; string country; int age; //header goes below

c++ STL cout source code

六月ゝ 毕业季﹏ 提交于 2019-12-07 21:40:25
问题 I want to see source code of STL std::cout function. I looked at iostream, but I've seen only "extern cout". So, I guess that it's defined somewhere in the library. I downloaded source code from official site I extracted it and did: sh@sh-R528-R728:~/desktop/stl$ grep -F * | grep "cout" but I got nothing. What am I doing wrong? Where is the source code? 回答1: cout is not part of the STL, so you won't find the source for cout in the STL source. You probably want to look for the source for your

Can cout throw an exception?

霸气de小男生 提交于 2019-12-07 17:45:28
问题 cout is an instance of type "ostream" ostream::operator<< says that If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure. This indicates to me that cout can throw an exception. Is this true? What kind of scenario could force this? 回答1: Yes, but practically nobody ever calls cout.exceptions(iostate) to enable that. Edit to expound: std::ios_base is an abstract class that provides basic utility functions

初级C++4IO流

本秂侑毒 提交于 2019-12-07 16:23:35
输入输出流 输入输出的含义: 从操作系统的角度看,每一个与主机相连的输入输出设备都被看作一个文件。 程序的输入:从文件传送给程序。 程序的输出:从程序传送给文件。 分类: 1、标准I/O 键盘输入 显示器输出 2、文件I/O 外存输入 外存输出 3、字符串I/O 内存输入 内存输出 C语言回顾 int i= 1; //假定占2字节 printf("%d",i); //正确输出 printf("%d",f); //f为float型,只输出f变量的前两个字节 printf("%d","C++"); //输出字符串的起始地址 scanf("%d",&i);//将内容写入变量i scanf("%d",i);//将内容写入内存地址为i的地方 C++的输入输出中,编译系统对数据类型进行严格的检查。 C++的I/O操作是类型安全的。 type safe —————————————————————————————— 流stream 输入输出流是指由若干字节组成的字节序列,这些字节中的数据按顺序从一个对象传送到另一对象。 流表示了信息从源到目的端的流动。 实际上,在内存中为每一个数据流开辟一个内存缓冲区,用来存放流中的数据。 流是与缓冲区相对应的。 缓冲区中的数据就是流。 输入输出流被定义为类 I/O库中的类被称为流类 stream class 用流类定义的对象被称为流对象 ios istream

Difference with cout and printf while multithreading in c++

泪湿孤枕 提交于 2019-12-07 08:44:47
问题 Some background: I have a c++ program that is multithreaded using pthreads. The program is a hotel reservation system, with 10 guests (each their own thread), a check-in desk (1 thread) and a check-out desk (1 thread). There are only 5 rooms in the hotel that a guest can be in. I am using semaphores to enforce mutual exclusion and event ordering in this program. Question: here is my code (Just parts that are needed...) sem_init(&openRooms, 0, 5); sem_wait(&openRooms); //waits for there to be

list::splice()函数详解

一笑奈何 提交于 2019-12-07 07:52:54
list::splice实现list拼接的功能。将源list的内容部分或全部元素删除,拼插入到目的list。 函数有以下三种声明: void splice ( iterator position, list<T,Allocator>& x ); // void splice ( iterator position, list<T,Allocator>& x, iterator i ); void splice ( iterator position, list<T,Allocator>& x, iterator first, iterator last ); 函数说明:在list间移动元素: 将x的元素移动到目的list的指定位置,高效的将他们插入到目的list并从x中删除。 目的list的大小会增加,增加的大小为插入元素的大小。x的大小相应的会减少同样的大小。 前两个函数不会涉及到元素的创建或销毁。第三个函数会。 指向被删除元素的迭代器会失效。 参数: position 目的list的位置,用来标明 插入位置 x 源list、 first,last x里需要被移动的元素的迭代器。区间为[first, last). 包含first指向的元素,不包含last指向的元素。 例子: // splicing lists #include <iostream> #include <list>

再说c++虚析构函数

此生再无相见时 提交于 2019-12-07 07:16:34
关于c++类中的虚析构函数。我写这篇博客是为了得出如下3点结论。 1.所有基类的析构函数,都应该声明为虚析构函数!这也是c++标准所指定的。 2.如果设计一个类,可能会被后来的其他类所继承,我们一定要将它的析构函数声明为虚析构。否则被继承会出现内存泄漏等意想不到的问题。 3.如果我们要去继承另外一个类,首先一点是要保证被继承的类的析构函数已经声明为了虚析构函数! 对以上3点,如果你深有理会,如下内容可以不看,不要浪费时间。如果没有很深的概念,可以参考一下。如有问题,希望在评论区或其他方式给我指点,谢谢。 非继承类的析构函数执行会有如下操作 : 1、执行析构函数中的代码 2、如果所在类有成员变量(类对象),再执行类成员变量的析构函数 普通析构函数: 以如下代码为例 class Student { public : Student () { name = new char [ 32 ]; cout << "Student constructor new name char[32]" << endl; } ~ Student () { delete name ; cout << "Student destructor delete Student::name" << endl; } private : char * name ; }; class Normal { public :

An “atomic” call to cout in MPI

被刻印的时光 ゝ 提交于 2019-12-07 06:24:34
问题 I am interested in whether there is a command or a technique within OpenMPI to have an atomic call to write to stdout (or, for that matter, any stream). What I have noticed is that during the execution of MPI programs, calls to write to cout (or other streams) can become confusing, as each proc may write whenever it gets to a certain section of code. When reporting results, a line can be written to by several procs, confusing the issue. So 2 different procs might do something like this: /

Calls to flush cout are ineffective

99封情书 提交于 2019-12-07 01:04:28
问题 I am attempting to have the cout buffer flush to view a string before I manipulate it. Ive attempted to flush the buffer with calls to both std::flush() and std::cout.flush() but neither actually flush my output. Only a call to std::endl has successfully flushed the buffer for me. Here is my code std::istringstream stm (game.date()); int day, month, year; char delim = '/'; std::cout << "Date before: " << game.date() << std::flush; // first flush attempt std::cout.flush(); // second flush