#include <string>
string: :size_type //An unsigned integer (data) type
string: :npos //The maximum value of the (data) type string:: size_type ,
//a number such as 4294967295 on many machines
string strVar;
strVar.length()
strVar.size()
strVar.find(strExp)
strVar.find(strExp, pos)
strVar.substr(expr1, expr2) //sentence.substr(6, 16)
strVar1.swap(strVar2) //interchange—the contents of two string
Additionalstr in g functions, such as empty, clear , erase, insert , and replace,
#include <cstring>
strcpy (s1, s2 ) //Copies the string s2 into the string variable s1
//The length of s1 should be at least as large as s2
strcmp (s1, s2) //Returns a value <0 if s1 is less than s2
//Returns 0 if s1 and s2 are the same
//Returns a value >0 if s1 is greater than s2
strlen (s ) //Returns the length of the string s, excluding the null character
cin.get(str, m + 1); // This statement stores the nextm characters, or all characters until the newline character '\ n' is found, into str
// The newline character is not stored in st r. If the input C –string
// has fewer than m characters, then the reading stops at the newline character.
The header file string contains the function c_str, which converts a value of type string
to a null-terminated character array (that is,C-string).
strVar.c_str()
ifstream infile;
string fileName;
cout << "Enter the input file name: ";
cin >> fileName;
infile.open(fileName.c_str ()); //open the input file
Arrays as Parameters to Functions
void funcArrayAsParam(int listOne[], double listTwo[]){}
Constant Arrays as Formal Parameters
void example(int x[], const int y[], int sizeX, int sizeY){}
Other Ways to Declare Arrays
const int SIZE = 50;
typedef double list[SIZE];
list yourList;
list myList;
Arrays of Strings and the string Type
string list[100];
Arrays of Strings andC -Strings (Character Arrays)
char list[100][16];
Multidimensional Arrays
dataType arrayName[intExp1][intExp2] ... [intExpn];
arrayName[indexExp1][indexExp2] ... [indexExpn]
struct structName
{
dataType1 identifier1;
dataType2 identifier2;
.
.
.
dataTypen identifiern;
};
Abstract data type (ADT):A data type that separates the logical properties from the
implementation details.
You can define a list as an ADT as follows:
dataTypeName
listType
domain
Every listType value is an array of, say, 1000 numbers
operations
Check to see whether the list is empty.
Check to see whether the list is full.
Search the list for a given item.
Delete an item from the list.
Insert a n item in the list.
Sort the list.
Destroy the list.
Print the list.
class listType
{
public:
bool isEmptyList() const ;
bool isFullList() const ;
int search(int searchItem) const ;
void insert(int newElement);
void remove(int removeElement);
void destroyList();
void printList() const ;
listType(); //constructor
private:
int list[1000];
int length;
};
浙公网安备 33010602011771号