iostream

From Wikipedia, the free encyclopedia

(Redirected from Cout)
Jump to: navigation, search
C++ Standard Library
ios
iostream
iomanip
map
fstream
C Standard Library
cassert
cctype
cerrno
cfloat
cmath
cstdio
cstdlib
ctime

iostream is a header file which is used for input/output in the C++ programming language. It is part of the C++ standard library. The name stands for Input/Output Stream. In C++ and its predecessor, the C programming language, there is no directly-included method for streaming data input or output. Instead, these are combined as a library. iostream handles basic input and output for C++, whereas stdio.h is used in C. iostream uses the objects cin, cout, cerr, and clog for sending data to and from the standard streams input, output, error (unbuffered), and error (buffered) respectively. As part of the C++ standard library, these objects are a part of the std namespace.

The canonical Hello world program can be as follows:

#include 
 
int main()
{
    std::cout << "Hello, world!\n";
}

This program would output "Hello, world!" followed by a newline.

The cout object is of type ostream, which overloads the left bit-shift operator to make it perform an operation completely unrelated to bitwise operations. The cerr and clog objects are also of type ostream, so they overload that operator as well. The cin object is of type istream, which overloads the right bit-shift operator. The directions of the bit-shift operators make it seem as though data is flowing towards the output stream or flowing away from the input stream.

endl is an output manipulator that writes a newline and flushes the buffer, ensuring that the data is output immediately. Several other manipulators are listed below. A commonly-used alternative to the newline character \n is endl, which is used as follows:

#include 
#include  // for std::endl
 
int main()
{
    std::cout << "Hello, world!" << std::endl;
}

Contents

width(int x) minimum number of characters for next output
fill(char x) character used to fill with in the case that the width needs to be elongated to fill the minimum.
precision(int x) sets the number of significant digits for floating-point numbers

Example:

cout.width(10);
cout << "ten" << "four" << "four";

Manipulators are objects that can modify a stream using the << or >> operators.

endl inserts a newline into the stream and calls flush.
flush forces an output stream to write any buffered characters
dec changes the output format of number to be in decimal format
oct changes the output format of number to be in octal format
hex changes the output format of number to be in hexadecimal format
ws causes an inputstream to 'eat' whitespace
showpoint tells the stream to show the decimal point and some zeros with whole numbers

Other manipulators can be found using the header iomanip.

Some C++ compiler toolchains are incapable of removing unreachable code when producing an executable that includes through static linking. For instance, a hello world program with the GNU implementation of the C++ standard library produces an executable an order of magnitude larger than an equivalent program that uses ,[1] due partly to a deficiency in GNU ld.[2] There exist partial implementations of the C++ standard library designed for space-constrained environments; their may leave out features that programs in such environments may not need, such as locale support.[3]

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.