C++ Standard Library
C++ Standard Library
- C++ began a standardization process in 1989 to define exact content of the language
- Included the development of a library which became known as the C++ standard library
- Extends the core language to provide powerful new features including
- Templates
- Namespaces
- New bool variable type
- Standard Template Library (STL)
- Header file conventions
- Error handling and allocators
- string and C++ stream classes
Namespaces
- More and more software is written as libraries, modules, or components
- Combining user code with such software could result in a problem
- What if you named your class the same as a class contained in a library?
- What if you named a function the same as a function contained in a library?
- The names of your classes or functions would clash with names in the library
- Included in the standard library is a feature known as namespaces to solve this problem
- Namespaces solves the problem by grouping names (identifiers) in a named scope
- Names in a namespace scope will not conflict with names in other scopes
- In the example below, a class is designed with the same name as a library class
- It is not clear which string class to use (the one in the program or the library)
#include <iostream>
using namespace std;
class string
{
public:
string() { std::cout << "My string" <<
std::endl; }
};
main()
{
string s;
}
Output
ns4.C: In function 'int main()':
ns4.C:13: error: reference to 'string' is
ambiguous
ns4.C:6: error: candidates are: class string
- Knowing that string is used in the library, we should have never designed a
class with that name
- Although this might be true, what about the class names in the library
we don’t know about?
- What if, in addition, we were linking with third-party libraries containing all kinds of
other names?
- Must a programmer be forced to be aware of all available names in use so as not
to clash?
- By using namespaces, a programmer is relieved from having to keep track of all
names in use
- Let’s examine how we can create a namespace to avoid clashing in the example
above
- Consider, for example, if we wanted this program to use the string class from
the library?
- We need a way to make a distinction between the two different string classes
- We create this distinction by creating a namespace and grouping names within its
scope
#include <iostream>
using namespace std;
namespace MySpace
{
class string;
}
class MySpace::string
{
public:
string() { std::cout << "My string" <<
std::endl; }
};
- Names in the program that might clash with others are grouped in a namespace
- In this example, we’ve named our scope MySpace although any name will do
namespace MySpace
{
class string;
}
- The elements are identified with the namespace using the scoping operator in the
program
class MySpace::string
- This informs the compiler to use all names in the namespace that is named std
- The program will now use the string class from the standard library (std) as in
the example below
namespace MySpace
{
class string;
}
#include <iostream>
using namespace std;
class MySpace::string
{
public:
string() { std::cout << "My string" <<
std::endl; }
};
main()
{
string s;
}
- To use the string from our string class, we simply change the using directive
in our example above
namespace MySpace
{
class string;
}
#include <iostream>
using MySpace::string;
class MySpace::string
{
public:
string() { std::cout << "My string" <<
std::endl; }
};
main()
{
string s;
}
- In this example, only the string name from the MySpace namespace is to be
used
using MySpace::string;
- Switching out the namespace, we could use the string class from the std
namespace MySpace
{
class string;
}
#include <iostream>
using std::string;
class MySpace::string
{
public:
string() { std::cout << "My string" <<
std::endl; }
};
main()
{
string s;
}
namespace MySpace
{
class string;
}
#include <iostream>
class MySpace::string
{
public:
string() { std::cout << "My string" <<
std::endl; }
};
main()
{
MySpace::string s1;
std::string s2;
}
- This provides a way to use the same name from different namespaces in a
program
- s1 is an object from the string class in the program and s2 is a std string
- Namespaces can include multiple names as in the following example
- Names can include variable, function, or class names
#include <iostream>
main()
{
// Explicit use of namespace identifiers
MySpace::Test t;
MySpace::print();
}
Output
Test object
Hello world
- We could have also employed the using directive to accomplish the same result
#include <iostream>
// Using directive
using MySpace::Test;
using MySpace::print;
main()
{
Test t;
print();
}
Output
Test object
Hello world
- Finally, all names are distinguished with the using namespace directive
#include <iostream>
// Using namespace
using namespace MySpace;
main()
{
Test t;
print();
}
Output
Test object
Hello world
#include <iostream>
namespace ns1
{
void print();
}
namespace ns2
{
void print();
}
void ns1::print()
{
cout << "Print from namespace ns1" << endl;
}
void ns2::print()
{
cout << "Print from namespace ns2" << endl;
}
using ns2::print;
main()
{
print();
}
Output
Print from namespace ns2
bool Type
- Oftentimes a program variable is needed to act as a simple switch or flag
- The value needs to be either on or off, 1 or 0, true or false
- The standard library introduced a new Boolean type, bool, to support this
behavior
- Boolean variables store one of two values, true or false
- bool variables also provide automatic type conversion
- 0 is equivalent to false
- any other value is equivalent to true
#include <iostream>
main()
{
bool flag1 = true;
bool flag2 = false;
bool flag3 = 77;
bool flag4 = 0;
Output
flag1: 1
flag2: 0
flag3: 1
flag4: 0
#include <iostream>
#include <string>
General features
Error and exception handling
- Organized method of handling errors or unexpected situations
- Dealing with errors outside of where they actually occur
- Makes easier to read code ("unpolluted")
- Library provides specialized classes for handling exceptions
Allocators
- Special objects to handle the allocation and deallocation of memory
- Different memory models (shared memory, garbage collection, ...)
- Interface to different memory models remains the same
Containers
- different methods of managing collections of objects
Iterators
- used to step through elements of collections of objects
Algorithms
- used to process the elements of collections
- search, sort, modify