C++ Strings, Enums Data Processing Course, I. Hrivnacova, IPN Orsay Using strings Manipulating strings as fundamental types (using operators) Manipulating strings as objects (using object-oriented syntax) Useful string operations Enums I. Hrivnacova @ Data Processing Course 2019 1
C++ Strings In C++, there are no built-in types for the use of strings. The string type is provided by the standard library String classes enable you to use strings as normal types that cause no problems for the user Different from ordinary strings of type char* or const char* Copy, assignment and compare functions, as for fundamental types, are already available - no worry about memory corruption Defined in the header <string> Type: string = chain of characters of char type I. Hrivnacova @ Data Processing Course 2019 2
#include <iostream> #include <string> using namespace std; Using Strings - I // Create two strings string firstname = "bjarne"; string lastname = "stroustrup"; string name; // Manipulate strings firstname[0] = 'B'; lastname[0] = 'S'; // Chain strings name = firstname + lastname; // Compare strings if ( name!= "" ) { cout << name << " is founder of C++" << endl; firstname, lastname Variables of type string (in view of traditional programing) Objects or instances of the class string in view of object-oriented programming) In object-oriented programming types are called classes The strings can be manipulated with operators as fundamental types I. Hrivnacova @ Data Processing Course 2019 3
#include <iostream> #include <string> using namespace std; Using Strings - I // Create two strings string firstname = "bjarne"; string lastname = "stroustrup"; string name; // Manipulate strings firstname[0] = 'B'; lastname[0] = 'S'; // Chain strings name = firstname + " " + lastname; // Compare strings if ( name!= "" ) { cout << name << " is founder of C++" << endl; Program output: >./usestrings Bjarne Stroustrup is founder of C++ I. Hrivnacova @ Data Processing Course 2019 4
#include <iostream> #include <string> using namespace std; // Create a string string name = "Agatha"; // Append it with another string name.append("chriistie"); // Find position of 'Chriistie' auto idx = name.find("chriistie"); // Insert a space name.insert(idx, " "); // Find position of the first i auto idxi = name.find("i"); // Erase this character name.erase(idxi, 1); Using Strings - II Object-oriented syntax for operations that are defined for string objects objectname.function() This kind of function is called a member function in C++ The effect of these functions depends on the implementation of these operations in the string class auto automatic type deduction (C++11) // Print the result cout << name << endl; I. Hrivnacova @ Data Processing Course 2019 5
#include <iostream> #include <string> using namespace std; // Create a string string name = "Agatha"; // Append it with another string name.append("chriistie"); Using Strings - II // Find position of 'Chriistie' auto idx = name.find("chriistie"); // Insert a space name.insert(idx, " "); // Find position of the first i auto idxi = name.find("i"); // Erase this character name.erase(idxi, 1); Program output: >./usestrings2 Agatha Christie // Print the result cout << name << endl; I. Hrivnacova @ Data Processing Course 2019 6
#include <iostream> #include <string> using namespace std; // Create a string string name = "Agatha"; // Append it with another string name.append("chriistie"); // Find position of 'Chriistie' auto idx = name.find("chriistie"); // Insert a space name.insert(idx, " "); // Find position of the first i auto idxi = name.find("i"); // Erase this character name.erase(idxi, 1); Using Strings - II Object-oriented syntax for operations that are defined for string objects objectname.function() This kind of function is called a member function in C++ The effect of these functions depends on the implementation of these operations in the string class auto automatic type deduction (C++11) // Print the result cout << name << endl; I. Hrivnacova @ Data Processing Course 2019 7
Useful String Operations =, assign() Assign new value swap() +=, append(), push_back() insert() erase() clear() resize() replace() Swap values between two strings Append characters Insert characters Erase characters Erase all characters (emoty string) Change number of characters (erase or add characters at the end) Replace characters + Create sum string I. Hrivnacova @ Data Processing Course 2019 9
==,!=, <, <=, >, >=, compare() length(), size() empty() Useful String Operations Compare two strings Return number of characters Test whether a string is empty [], at Access a single character >>, getline() Feed in string << Output string find functions substr() c_str() copy() Search for a part string or character Issue a part string Use string as C-string Copy a character into a character buffer An overview of string operations: see also C++ book, chapter 3.4.3, page 67 I. Hrivnacova @ Data Processing Course 2019 10
Enumerations An enumerated type (also called an enumeration) is a data type where every possible value is defined as a symbolic constant, called an enumerator Enumerations are declared via the enum keyword. // Enumeration named Color enum Color { kred, kgreen, kblue ; // Variables of enumerated type Color Color grass = kgreen; Color sky = kblue; I. Hrivnacova @ Data Processing Course 2019 11
Enumerations Each enumerator is automatically assigned an integer value based on its position in the enumeration list. By default, the first enumerator is assigned the integer value 0 Each subsequent enumerator has a value one greater than the previous enumerator It is possible to explicitly define the value of enumerator // Enumeration named Color enum Color { kred, // assigned 0 kgreen, // assigned 1 kblue = 6, kwhite // assigned 7 ; // Variables of enumerated type Color Color grass = kgreen; Color sky = kblue; Color show = kwhite; cout << grass << endl; // 1 cout << snow << endl; // 7 I. Hrivnacova @ Data Processing Course 2019 12