C++ Standard Template Library
C++ Standard Template Library
string
• C++ provides a powerful alternative for the char*, string.
• It is not built-in data type, but it is container in STL.
• To use string in our program, we must include string header file.
• Examples:
#include<string.h>
int main() {
string s0; // s0=“”
string s1=“Hello World”; //s1=“Hello World”
string s2(s1);
string s3(s1,1,3); //s3=“ell”
string s4(5,’*’); //s4=“*****”
string s5(s1.begin(),s1.begin()+3); // s5=“Hel”
}
String – Member Functions
Functions Description
append() Inserts additional characters at the end of the string (can also be done using ‘+’ or ‘+=’ operator).
Its time complexity is O(N) where N is the size of the new string.
assign() Assigns new string by replacing the previous value (can also be done using ‘=’ operator).
at() Returns the character at a particular position (can also be done using ‘[ ]’ operator). Its time
complexity is O(1).
begin() Returns an iterator pointing to the first character. Its time complexity is O(1).
clear() Erases all the contents of the string and assign an empty string (“”) of length zero. Its time
complexity is O(1).
compare() Compares the value of the string with the string passed in the parameter and returns an integer
accordingly. Its time complexity is O(N + M) where N is the size of the first string and M is the
size of the second string.
copy() Copies the substring of the string in the string passed as parameter and returns the number of
characters copied. Its time complexity is O(N) where N is the size of the copied string.
c_str() Convert the string into C-style string (null terminated string) and returns the pointer to the C-
style string. Its time complexity is O(1).
length() Returns the length of the string. Its time complexity is O(1).
String – Member Functions
Functions Description
empty() Returns a boolean value, true if the string is empty and false if the string is not empty. Its time
complexity is O(1).
end() Returns an iterator pointing to a position which is next to the last character. Its time complexity
is O(1).
erase() Deletes a substring of the string. Its time complexity is O(N) where N is the size of the new
string.
find() Searches the string and returns the first occurrence of the parameter in the string. Its time
complexity is O(N) where N is the size of the string.
insert() Inserts additional characters into the string at a particular position. Its time complexity is O(N)
where N is the size of the new string.
replace() Replaces the particular portion of the string. Its time complexity is O(N) where N is size of the
new string
resize() Resize the string to the new length which can be less than or greater than the current length. Its
time complexity is O(N) where N is the size of the new string.
size() Returns the length of the string. Its time complexity is O(1).
substr() Returns a string which is the copy of the substring. Its time complexity is O(N) where N is the size
of the substring.
String-Example
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
string s, s1;
s = "HELLO";
s1= "HELLO";
if(s.compare(s1) == 0)
cout << s << " is equal to " << s1 << endl;
else
cout << s << " is not equal to " << s1 << endl;
s.append(" WORLD!");
cout << s << endl;
printf("%s\n", s.c_str());
if(s.compare(s1) == 0)
cout << s << " is equal to " << s1 << endl;
else
cout << s << " is not equal to " << s1 << endl;
return 0;
}
vector
• Vectors are sequence containers that have dynamic size.
• In other words, vectors are dynamic arrays.
• Just like arrays, vector elements are placed in contiguous storage location so
they can be accessed and traversed using iterators.
• To traverse the vector we need the position of the first and last element in the
vector which we can get through begin() and end() or we can use indexing from
0 to size().
• To use vector include vector header file
void traverse(vector<int> v)
{
vector <int>::iterator it;
for(it = v.begin();it != v.end();++it)
cout << *it << ‘ ‘; cout << endl;
Output:
All elements of List LI are: 1 5 3 4 5
All elements of List LI are after reversing: 5 4 3 5 1
Elements after removing all occurrence of 5 from List 4 3 1
pair
• Pair is a container that can be used to bind together a two values which may be of different
types. Pair provides a way to store two heterogeneous objects as a single unit.
• We can also initialize a pair using make_pair() function. make_pair(x, y) will return a pair with
first element set to x and second element set to y.
p1 = make_pair(2, ‘b’);
• To access the elements we use keywords, first and second to access the first and second
element respectively.
cout << p2.first << ‘ ‘ << p2.second << endl;
Pair-Example
#include <iostream>
#include <utility>
using namespace std;
int main() {
pair <int, char> p; Output:
pair <int, char> p1(2, 'b'); 1a
p = make_pair(1, 'a'); 2b
cout << p.first << ' ' << p.second << endl;
cout << p1.first << ' ' << p1.second << endl;
return 0;
}
sets
• Sets are containers which store only unique values and permit easy look ups.
• The values in the sets are stored in some specific order (like ascending or
descending).
• Elements can only be inserted or deleted, but cannot be modified.
• We can access and traverse set elements using iterators just like vectors.
// It will map value 1 with key ‘b’. We can directly access 1 by using mp[ ‘b’ ].
mp[‘b’] = 1;
mp[‘a’] = 2;