Presentation is loading. Please wait.

Presentation is loading. Please wait.

String class and STL. string class has the following constructors: string (const char *s) //initialize a string object with the string pointed by s string(size_type.

Similar presentations


Presentation on theme: "String class and STL. string class has the following constructors: string (const char *s) //initialize a string object with the string pointed by s string(size_type."— Presentation transcript:

1 String class and STL

2 string class has the following constructors: string (const char *s) //initialize a string object with the string pointed by s string(size_type n,char c) // create a string object with n element every of them char c string() creates a default string object of 0 size

3 string(const char *s, size_type n) // initialize n byte of string with s template string (Iter begin, Iter end) //initialize a string // object to the values in the range [begin, end), begin and end act like pointer.

4 #include int main() { string one(“this is test one”); cout<<one<<endl; string two (20,’$’); string three(one); cout<<three<<endl; one+=“add something”; two=“change two”; three[0]=‘P’; string four; four=two+three; cout<<four; char s[ ]=“the take some part of string”; string five(s,20); string six(s+6,s+10); cout<<six<<endl; string seven(&five[6], &five[10]); cout<<seven<<endl; }

5 char input[100]; cin>>input; cin.getline(input,100); cin.get(input,100); string s; cin>>s; getline(cin,s); //read a line, discard \n cin.getline(input,100,’:’); // read up to :, discard : getline(cin,stuff,’:’); //read up to :, discard :

6 int main() { ifstream infile; infile.open(“test.txt”); if( infile.is_open()==false) { cout<<“not open”; exit(1); } string item; int count; getline(infile,item, ‘:’ ); while(infile ){ ++count; cout<<count<<“:”<<item<<endl; getline(infile,item,’:’); } infile.close(); return 0; }

7 string s1(“this is”); string s2(“is this”); char s3[20]=“that is”; if( s1 < s2) … if(s1==s3) … if(s3 !=s2)

8 Standard Template Library STL provides a collection of templates such as containers, iterators and algorithms. A container is a unit like an array that can hold several values.

9 vector template class // vector is a container #include vector vector v(5); // a vector of 5 int int n; cin>>n; vector scores(n); for(int i=0; i<n;i++) cout<<scores[i];

10 vector title(5); We can get the size of the vector by size(); // number of elements swap() exchange the contents of two containers begin() // return an iterator that refer to the first element in a container, end() returns an iterator that refers to the last element.

11 Iterator is a generalization of a pointer, in fact it can be a pointer. It can be an object for which operations such as ++, -- has been defined. Each container class defines a iterator. vector :: iterator pd; // pd an iterator vector scores; pd =scores.begin(); *pd=22.3; ++pd;

12 push_back(); Add an element to the of a vector, and increase vector size. vector scores; double temp; while( cin >> temp && temp >=0) scores.push_back(temp);

13 erase() // remove a given range of a vector. scores.erase(scores.begin(), scores.begin()+2); insert() takes three arguments //first give the position of which new elements are to be inserted. vector old; vector new;

14 old.insert(old.begin(),new.begin()+1,new.end()); More methods for_each(), random_shuffle(), sort() For_each() // can be used with any container class. It takes three argumnet, the first two are iterators defining the range and the third one in a pointer to a function.

15 vector V:: iterator pr; for( pr =V.begin(); pr !=V.end(); pr++) show(*pr); for_each( V.begin(), V.end(), show); //the function must NOT alter the element of the vector

16 The randon_shuffle() function takes two iterators that specify a range and rearrange the element in that range in the random order. Random_shuffle(V.begin(), V.end()); vector s; sort(s.begin(), s.end()); // it sorts in non-decreasing order. If the container elements are user-defined objects, then we should have an opeator<() function.

17 struct R{ string title; int rate; }; bool operator< ( const R & r1, const R & r2) { if( r1.title < r2.title ) return ; else if( r1.title == r2.title && r1.rate < r2.rate) return true; else return false; } vector book; sort(book.begin(), book.end());


Download ppt "String class and STL. string class has the following constructors: string (const char *s) //initialize a string object with the string pointed by s string(size_type."

Similar presentations


Ads by Google