More in STL - PowerPoint PPT Presentation

About This Presentation
Title:

More in STL

Description:

... want to place the result in set C, If we use C.begin() instead of the last argument it doesn't ... set string C; set_union(A.begin(), A.end(), B.begin(),B.end ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 22
Provided by: Ara8
Category:
Tags: stl | more | string

less

Transcript and Presenter's Notes

Title: More in STL


1
More in STL
2
  • includeltiostreamgt
  • includeltstringgt
  • includeltiteratorgt
  • includeltalgorithmgt
  • int main()
  • const int SIZE100
  • int arraySIZE
  • for(int i0ilt10i)
  • arrayi10-i
  • sort(array,array10)
  • for( i0ilt10i)
  • coutltltarrayiltlt" "
  • coutltltendl

3
some predefined iterators
  • copy() copy data from one container to another
    one.
  • int array106,3,2,9,4,7,1,2,10,11
  • vectorltintgt d(10)
  • copy(array,array10,d.begin())

4
ostream_iterator istream_iterator
  • When we want to copy information to the display,
    we can use copy() with the output iterator.
  • includeltiteratorgt
  • ostream_iteratorltint,chargt out_iter(cout, )
  • We use cout to display information.
  • Data type being sent is int,
  • char indicates that character type used by the
    output stream.

5
  • The first constructor argument (cout, here)
    identifies the output stream being used. It could
    be also the output file stream.
  • Finally is the separator, comes after each
    item.
  • We can write out_iter15
  • Is like coutltlt15ltlt

6
  • int array106,3,2,9,4,7,1,2,10,11
  • vectorltintgt d(10)
  • copy(array,array10,d.begin())
  • ostream_iteratorltint,chargt out_iter(cout, )
  • copy(d.begin(),d.end(),out_iter)

7
  • istream_iteratorltint,chargt in_iter(cin)
  • copy(in_iter, istream_iteratorltint,chargt ()
    d.begin())
  • Dropping the argument in constructor indicates
    input failure, so the previous code means to read
    from the input stream until end of file or type
    mismatch.

8
  • int array106,3,2,9,4,7,1,2,10,11
  • vectorltintgt d(10)
  • copy(array,array10,d.begin())
  • ostream_iteratorltint,chargt out_iter(cout, )
  • copy(d.rbegin(),d.rend(),out_iter)
  • vector ltintgt reverse_iterator ri
  • for( ri d.rbegin() ri ! d.rend() ri)
  • coutltltriltlt
  • coutltltendl

9
  • The list template class represent a doubly link
    list.
  • includeltiostream.hgt
  • includeltiteratorgt
  • includeltvectorgt
  • includeltlistgt
  • int main()
  • list ltintgt one(5,2)
  • int st51,2,4,8,6
  • listltintgt two
  • two.insert(two.begin(),st,st5)
  • int m66,4,2,4,6,5
  • listltintgt three (two)
  • three.insert(three.end(),m,m6)

10
  • coutltltendlltltlist one
  • ostream_iteratorltint,chargt out(cout, )
  • copy(one.begin(),one.end(),out)
  • coutltltendl
  • coutltltlist two
  • copy(two.begin(),two.end(),out)
  • coutltltendl
  • copy(three.begin(),three.end(),out)
  • three.remove(2)
  • coutltltendl
  • copy(three.begin(),three.end(),out)

11
  • three.splice(three.begin(),one)
  • coutltltendl
  • copy(three.begin(),three.end(),out)
  • coutltltlist one
  • copy(one.begin(),one.end(),out)
  • three.unique()
  • coutltltendl
  • copy(three.begin(),three.end(),out)
  • three.sort()
  • three.unique()
  • coutltltendl
  • copy(three.begin(),three.end(),out)
  • two.sort()

12
  • three.merge(two)
  • coutltltendl
  • copy(three.begin(),three.end(),out)
  • copy(two.begin(),two.end(),out)
  • coutltltendl
  • return 0

13
Set
  • setltstringgt A // a set of string objects
  • const int N6
  • string s1Nthis, is , really, cool,
    isnt, it
  • setltstringgtA(s1,s1N)
  • ostream_iteratorltstring,chargt out(cout, )
  • copy(A.begin(),A.end(),out)
  • we have set_union() and set_intersection(),
    set_difference()

14
  • set_union(A.begin(), A.end(), B.begin(),
    B.end(), ostream_iteratorltstring,chargt out(cout,
    ))
  • display the union of A and B

15
  • If we want to place the result in set C,
  • If we use C.begin() instead of the last argument
    it doesnt work.
  • The iterator return by C.begin() is a constant
  • Iterator and cant be use as an output iterator.
  • Set C may not have enough space for the union.

16
  • We use insert_iterator
  • set_union(A.begin(), A.end(), B.begin(),B.end(),
  • insert_iterator ltsetltstringgtgt(C,C.begin()))
  • Some other methods for set.
  • string s(tennis)
  • A.insert(s)
  • B.insert(A.begin(), A.end())

17
  • includeltiostream.hgt
  • includeltsetgt
  • includeltiteratorgt
  • includeltalgorithmgt
  • int main()
  • const int N6
  • string s1Nthis, is , really, cool,
    isnt, it
  • string s2NIt, is, not, C, leave
    us,
  • alone // it is algorithms, not
    interested

18
  • setltstringgtA(s1,s1N)
  • setltstringgtB(s2,s2N)
  • ostream_iteratorltstring,chargt out(cout, )
  • coutltlt A is
  • copy(A.begin(),A.end(), out)
  • coutltltendlltltB is
  • copy(B.begin(),B.end(), out)

19
  • coutltltunion of A and B \n
  • set_union(A.begin(), A.end(), B.begin(), B.end(),
    out)
  • coutltltendl
  • coutltlt intersection A,B \n
  • set_intersection(A.begin(), A.end(), B.begin(),
    B.end(), out)
  • coutltltendl
  • coutltlt difference, A-B \n
  • set_difference(A.begin(), A.end(), B.begin(),
    B.end(), out)
  • coutltltendl

20
  • setltstringgt C
  • set_union(A.begin(), A.end(), B.begin(),B.end(),
  • insert_iterator ltsetltstringgtgt(C,C.begin()))
  • copy(C.begin(),C.end(), out)
  • coutltltendl
  • string s3(real)
  • C.insert(s3)

21
  • copy(C.begin(),C.end(), out)
  • coutltltendl
  • coutltltShow a rang ltltendl
  • copy(C.lower_bound(great),
  • C.upper_bound(speak), out)
  • coutltltendl
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com