3.1 Namespace using Declarations
scope operator (::) means we should look for the name of the right-hand operand in the scope of the left-hand operand.
A
using declarations allows us to access a name from a namespace without the prefix
namespace_name::. A
usingdeclaration has the following form:
using namespace::name;
Once the using declaration has been made, we can access name directly without reference to its namespace:
#include <string>
#include <iostream>
// using declarations states our intent to use these names from the namespace std
using std::string;
using std::cin;
int main()
{
string a; //ok
cin >>a; //ok
cout <<a; //error: no using declaration
std::cout <<a; //ok
}
A using declaration introduces only one namespace member at a time. eg:
using std::cin;
using std::cout;
There is one case in which we should alwaysuse the fully qualified library names:inside header files.
3.2 Library string Type
3.2.1 Defining and Initializing strings
The string type supports variable-length character strings. To use strings, we must include the associated header:
#include <string>
using std::string;
String library has several constructors:
string s1; //default constructor; s1 is the empty string
string s2 (s1); //initialize s2 as a copy of s1
string s3 ("value"); //initialize s2 as a copy of the string literal
string s4 (n, 'c'); //initialize s4 with n copies of the character 'c'
character string literals are not the same type as the standard library string type.
3.2.2 Reading and Writing strings
We use iostream and string libraries to do read and write to string:
1. Reads and discards any leading whitespace (eg. spaces, newlines, tabs)
int main
{
string s; //empty string
cin >> s; //read whitespace-separated string into s
cout << s << endl; //write s to the output
}
Rules for string input operator:
1. Reads and discards any leading whitespace (eg. spaces, newlines, tabs)
2. It reads characters until the next whitespace character is encountered
Reading an unknown number of strings
Reading an unknown number of strings
int main()
{
string word;
//read until end-of-file, writing each word to a new line
while (cin >> word)
cout << word <<endl;
return 0;
}
We can also use
getline to read an entire line.
It will read next line of input from the stream and stores what it read, not including the newline, in its string argument. getline will not ignore leading new lines. The effect of encountering a newline as the first character in the input is that the string argument is set to the empty string.
3.2.3 Operations on strings
String operations
Size operation returns a value of string::size_type. The string class and many other library types--defines several companion types. These types make it possible to use the library types in a machine-independent manner.
s.empty() //returns true if s is empty
s.size() //returns number of characters in s
s[n] //returns the character at position n in s; position start at 0
s1 + s2 //returns a string equal to the concatenation of s1 and s2
s1 = s2 //replaces characters in s1 by a copy of s2
v1 == v2 //returns true if v1 and v2 are equal; false otherwise
!=, <, <=, >, >= //normal meanings
1. Size operation
Size operation returns a value of string::size_type. The string class and many other library types--defines several companion types. These types make it possible to use the library types in a machine-independent manner.
It is particularly important to assign the return value of size tostring::size_type, not to int.
2. Relational Operator
String comparisons are case-sensitive: the upper- and lowercase versions of a letter are different characters.
Two strings are equal if they are the same length and contain the same characters.The relational operators compare strings using the same strategy as in a dictionary:
a. if two strings have different lengths and if every character in the shorter string is equal to the corresponding character of the longer string, then
the shorter string is less than the longer one
b. if the characters in two strings differ, then we compare them by comparing
the first character at which the strings differ.
3. Assignment
We can assign one string object to another.
To assign value:
it must delete the storage containing the characters associated with s1, allocate the storage needed to contain a copy of the characters associated with s2, and then copy those characters from s2 into this new storage.
4. Adding two strings
We can use plus operator (+) or compound assignment operator (+=) to do adding.
We can also adding character sting literals and strings. When mixing strings and string literals,
at least one operand to each + operator must be of string type:
s3 = s1 + s2; //ok
s2 +=s1; //ok
s1 = "hello"; //ok
s3 = s1 + ","; //ok
s4 = "hello" + ","; //error
s5 = s1 + "," + "world"; //ok: each + ha sstring operand
s6 = "hello" + "," + s2; //error
5. Fetching a Character from a string
String type uses the
subscript (
[ ]) operator to access the individual character in the string. The subscript must not exceed the boundary:
0 <= subscript <= (s.size() -1)
The variable returned by the subscript operator is a lvalue, which means we can modify it!
//set each character to *
for (string::size_type ix = 0; ix != str.size(); ++ix)
str[ix] = '*';
6. Dealing with characters of a string
There is a
cctype
header that contains lots of character functions.
P89
The standard C headers names use the form
name.h.The C++ version of these headers are named
cname. The C++ version remove the .h suffix and precede the name by the letter c. In particular,
the names defined in the
cname
headers are defined inside the std namespace, whereas those defined in the
.h
versions are not.