Standard Template Library (STL library)

The primary idea in the STL is the container(also known as a collection), which is just what it sounds like, aplace to hold things. You need containets because objects are constantly marching in and out of your program and there must be someplace to put them while they are around. | Now consider taking the form of and reshaping it to display a list of the words used in a document. The solution becomes remarkably simple. C04 include . include string include fstream include iostream include set using namespace std int maintint argc char argv requireArgs argc 1 ifstream sourcetargv 1 assuretsource argv 1 string word set string words whiletsource word ostream_iterator string tcout n cout Number of unique words endl The only substantive difference here is that string is used instead of int. The words are pulled from a file but everything else is the same as in . The operator returns a whitespace-separated group of characters each time it is called until there s no more input from the file. So it approximately breaks an input stream up into words. Each string is placed in the set using insert and the copy function is used to display the results. Because of the way set is implemented as a tree the words are automatically sorted. Consider how much effort it would be to accomplish the same task in C or even in C without the STL. The basic concepts The primary idea in the STL is the container also known as a collection which is just what it sounds like a place to hold things. You need containers because objects are constantly marching in and out of your program and there must be someplace to put them while they re around. You can t make named local objects because in a typical program you don t know how many or what type or the lifetime of the objects you re working with. So you need a container that will expand whenever necessary to fill your needs. Chapter 15 Multiple Inheritance 151 All the containers in the STL hold objects and expand themselves. In addition they hold your objects in a particular way. The difference between one container and another is the way the objects are held and how the sequence is created. Let s start by looking at the .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
94    56    2    26-04-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.