C++ Programming TI00AA50: Jarkko - Vuori@metropolia - Fi
C++ Programming TI00AA50: Jarkko - Vuori@metropolia - Fi
TI00AA50
Lecture 5 - 08.10.2015
Class string
h e l
o 0
After we have learned the basics of classes, we would be able to define a class string
for example in the following way and implement its operation functions:
class string {
public:
string(const char *string0="");
string operator+(const string &s2) const;
const string &operator=(const string &right);
//and many other operators and member functions
private:
char *c_string;
};
But it is not necessary to implement it ourselves, because it has been done already in
the C++ standard library. We only have to include the header file and use it
#include <string>
TI00AA50/JV
String example
Advantages:
You dont have to give
the length in the
declaration
Memory area expands
automatically
Assignment works (it is a
real deep copy)
Comparison can be done
with comparison operator
(means alphabetical
order)
Input (>>) and output
(<<) operators are
defined for string
#include <iostream>
#include <string>
using namespace std;
void main() {
string str1, str2;
cout << "\nEnter a string";
cin >> str1;
str2 = str1; //assignment is OK
cout << "\nCopy of string 1 is : " << str2;
cout << "\nEnter a string";
cin >> str2;
if (str1 < str2) //comparison is OK
cout << str1 << "is less than" << str2 ;
else
cout << str2 << "is > or equal to" << str1 ;
cout << "\nThe capacity of the str2 is " <<
str2.capacity();
cout << "\nThe size (length) of the str2 is " <<
str2.size();
}
TI00AA50/JV
Constructors
Assignment operator
s1 = "abcde";// assigning a c-string
s2 = X;
// assigning a character
s3 = s1;
// assigning another string
s1 = s2;
s1.assign(s2);
TI00AA50/JV
s1[i];
X;
s1.at(i);
X;
// right val
// left val
// right val
//
--
Concatenation
String comparisons
start
length
TI00AA50/JV
Taking substrings
string s1("Helsinki Metropolia University");
string s2, s3;
s2 = s1.substr(0, 8);
//Helsinki
s3 = s1.substr(9, 10);
//Metropolia
Swapping strings
string s1("Espoo");
string s2("Helsinki");
s1.swap(s2); //s1 contains Helsinki and
s2 Espoo
string s1;
cout << s1.size() << s1.length()
s1 = "abcdef";
cout << s1.size() << s1.length()
s1.resize(s1.size() + 5);
cout << s1.size() << s1.length()
s1.reserve(30);
cout << s1.size() << s1.length()
<< s1.capacity();
<< s1.capacity();
<< s1.capacity();
<< s1.capacity();
Allocation policy
As we saw in the
previous page, the size
(length) of the string can
be different from the
capacity
The memory allocation
policy can be different in
different systems but the
following example gives
an idea of how it could
work (this is the case in
Visual Studio 2010)
Example.
string s;
s = "abcd";
s.reserve(8);
s = s + "efg";
s = s + "hij";
s.resize(14);
s.reserve(20);
s.resize(17)
Size
// 0
// 4
// 4
// 7
// 10
// 14
// 14
// 17
Capacity
15
15
15
15
15
15
31
31
Contents
Empty
|abcd|
|abcd|
|abcdefg|
|abcdefghij|
|abcdefghij|
|abcdefghij...|
|abcdefghij...|
TI00AA50/JV
TI00AA50/JV
Inserting strings
string s1("Espoo Institute of Technology");
string s2("xxxxxVantaayyyyy");
//s1.insert(5, "-Vantaa");
//Espoo-Vantaa Institute of ..
s1.insert(5, "-").insert(6, s2, 5, 6); //----
c_str
const char *c_str() const;
TI00AA50/JV
String streams
In memory I/O
Include files iostream and sstream are
needed
Classes istringstream and
ostringstream
Member function str of ostringstream
Connecting buffer or not connecting
memory buffer
C++-style or C-style string buffer
See example on the right
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// This program demonstrates how to use string
// streams to make conversions from numbers to
// strings and vice versa
int main() {
{ // Convert a number to string
string num_str;
int num_int = 127;
ostringstream string_stream;
string_stream << num_int;
num_str = string_stream.str(); // now we have it
cout << num_str;
// as a string
}
{ // Convert a string to the number
string num_str("127");
int num_int;
istringstream string_stream(num_str);
string_stream >> num_int;
// now we have it
cout << num_int;
// as int
}
class string::const_iterator;
string member functions begin and
end;
iterator comparison operators
dereference operator
increment operators
string::reverse_iterator
string::const_reverse_iterator
TI00AA50/JV
10
String streams
We have used input streams to read information from the keyboard and
output streams to display information
Input stream and output stream make conversions as we see in the following example
int a = 127; double b;
cout << a; // conversion is done from int to chars
cin >> b; // conversion is done from chars to double
When we write (<<) information to the stream cout, it goes to the display
When we read (>>) information from the stream cin it comes from the
keyboard
In the lab exercises 5 well see that we can connect a stream to the disk file
ofstream output_file("file.txt");
ifstream input_file("file.txt");
In this case when we write data to the stream it goes to the string and when we read the
stream, data comes from the string
This is useful when we need to make conversions
See an example program in the following page
TI00AA50/JV
11
C++
Linux:
//dev//tty
Windows:
CON:
output_file = fopen(file.txt, w);
fprintf(output_file, %d %f, a, b);
fclose(output_file);
ifstream input_file(file.txt);
input_file >> a >> b;
3. Stream to the string
int a = 10; float b = 100.5;
string mystring;
ostringstream string_stream;
string_stream << a << << b;
mystring = string_stream.str();
istringstream string_stream(mystring);
TI00AA50/JVstring_stream >> a >> b;
12
Stream formatting
#include <iostream>
#include <iomanip>
using namespace std;
class Time {
friend ostream& operator<<(ostream &out, Time time);
private:
int hour, min;
public:
Time(int hour = 0, int min = 0) {
this->hour = hour;
this->min = min;
}
};
ostream& operator<<(ostream &out, Time time) {
out << setiosflags(ios::right);
out << setfill('0') << setw(2) << time.hour;
out << ":" << setfill('0') << setw(2) << time.min;
return out;
}
void main() {
Time t1(11,1), t2(2,30);
cout << t1 << endl;
cout << t2 << endl;
}
13