Saturday, 26 May 2012

Strings in C++


Strings and I/O
Strings and input/output operations are not provided directly by special language constructs in C++.
Instead, the standard library provides string and I/O types. For example:
#i n c l u d e <s t r i n g > // make standard strings available
#i n c l u d e <i o s t r e a m > // make standard I/O available
i n t m a i n ()
{
u s i n g n a m e s p a c e s t d ;
s t r i n g n a m e ;
c o u t << "P l e a s e e n t e r y o u r n a m e : "; // prompt the user
c i n >> n a m e ; // read a name
c o u t << "H e l l o , " << n a m e << ´\ n ´; // output the name followed by a newline
r e t u r n 0 ;
}
This example uses the standard input and output streams, c i n and c o u t with their operators >> (‘‘get
from’’) and << (‘‘put to’’).
The I/O streams support a variety of formatting and buffering facilities, and strings support common
string operations such as concatenation, insertion, and extraction of characters and strings. Both streams
and strings can be used with characters of any character set.
The standard library facilities can be – and usually are – implemented using only facilities available to
all users. Consequently, where the standard facilities happen to be inadequate, a user can provide equally
elegant alternatives.

No comments:

Post a Comment