String class and STL - PowerPoint PPT Presentation

About This Presentation
Title:

String class and STL

Description:

begin() // return an iterator that refer to the first element in a container, ... If the container elements are user-defined objects, then we should have an ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 18
Provided by: Ara8
Category:
Tags: stl | class | container | string

less

Transcript and Presenter's Notes

Title: String class and STL


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
  • templateltclass Itergt
  • string (Iter begin, Iter end) //initialize a
    string
  • // object to the values in the range begin, end)
    , begin and end act like pointer.

4
  • includeltiostream.hgt
  • includeltstringgt
  • int main()
  • string one(this is test one)
  • coutltltoneltltendl
  • string two (20,)
  • string three(one)
  • coutltltthreeltltendl
  • oneadd something
  • twochange two
  • three0P
  • string four
  • fourtwothree
  • coutltltfour
  • char s the take some part of string
  • string five(s,20)
  • string six(s6,s10)
  • coutltltsixltltendl

5
  • char input100
  • cingtgtinput
  • cin.getline(input,100)
  • cin.get(input,100)
  • string s
  • cingtgts
  • 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)
  • coutltltnot open
  • exit(1)
  • string item
  • int count
  • getline(infile,item, )
  • while(infile )
  • count
  • coutltltcountltltltltitemltltendl
  • getline(infile,item,)
  • infile.close()
  • return 0

7
  • string s1(this is)
  • string s2(is this)
  • char s320that is
  • if( s1 lt s2)
  • if(s1s3)
  • 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 ltintgt v(5) // a vector of 5 int
  • int n
  • cingtgtn
  • vector ltdouble gt scores(n)
  • for(int i0 iltni)
  • coutltltscoresi

10
  • vectorltstringgt 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 ltdoublegt iterator pd
  • // pd an iterator
  • vector ltdoublegt scores
  • pd scores.begin()
  • pd22.3
  • pd

12
  • push_back()
  • Add an element to the of a vector, and increase
    vector size.
  • vectorltdoublegt scores
  • double temp
  • while( cin gtgt temp temp gt0)
  • 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.
  • vectorltintgt old
  • vectorltintgt 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 ltTgt 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())
  • vectorltintgt 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 opeatorlt()
    function.

17
  • struct R
  • string title
  • int rate
  • bool operatorlt ( const R r1, const R r2)
  • if( r1.title lt r2.title )
  • return
  • else if( r1.title r2.title r1.rate lt
    r2.rate)
  • return true
  • else
  • return false
  • vector lt Rgt book
  • sort(book.begin(), book.end())
Write a Comment
User Comments (0)
About PowerShow.com