HOWTO: STL Sample for list::size,list::resize Functions

Last reviewed: October 9, 1997
Article ID: Q163359
The information in this article applies to:
  • The Standard C++ Library included with: - Microsoft Visual C++, 32-bit Edition, version 4.2

SUMMARY

The sample code below illustrates how to use the list::max_size, list::size, list::resize STL functions in Visual C++.

MORE INFORMATION

Required Header

   <list>
   <string>
   <iostream>

Prototype

   size_type max_size() const;
   size_type size() const;
   void resize(size_type n, T x = T());

NOTE: The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.

Description

The member function max_size returns the length of the longest sequence that the object can control. The member function size returns the length of the controlled sequence. The member function resize ensures that size() henceforth returns n. If the member function resize must make the controlled sequence longer, it appends elements with the value x.

Sample Code

//////////////////////////////////////////////////////////////////////
//
// Compile options needed: -GX
//
// size.cpp :  This example shows how to resize a list<T>.  It also
//             uses list::size() and list::max_size().
//
//  Functions
//  ---------
//
//  list::size
//  list::resize
//  list::max_size
//
// Written by Andrew Bradnan
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////

    #include <list>
    #include <string>
    #include <iostream>

    typedef list<string, allocator<string> > LISTSTR;

    void main()
    {
       LISTSTR test;
       LISTSTR::iterator i;

       cout << "max_size() = " << test.max_size() << endl;

       test.insert(test.begin(), 10, "test");

       cout << "There are " << test.size() << " elements." << endl;
       // test test test test test test test test test test
       for (i = test.begin(); i != test.end(); ++i)
          cout << *i << " ";
       cout << endl;

       // Make it smaller
       test.resize(5);
       cout << "There are " << test.size() << " elements." << endl;
       // test test test test test
       for (i = test.begin(); i != test.end(); ++i)
          cout << *i << " ";
       cout << endl;

       // Make it bigger
       test.resize(10, "bigger");
       // test test test test test bigger bigger bigger bigger bigger
       cout << "There are " << test.size() << " elements." << endl;
       for (i = test.begin(); i != test.end(); ++i)
          cout << *i << " ";
       cout << endl;

       test.clear();
       cout << "There are " << test.size() << " elements." << endl;
    }

The Program Output is:

    max_size() = 268435455
    There are 10 elements.
    test test test test test test test test test test
    There are 5 elements.
    test test test test test
    There are 10 elements.
    test test test test test bigger bigger bigger bigger bigger
    There are 0 elements.

REFERENCES

Visual C++ Books On Line: Visual C++ Books:C/C++:Standard C++ Library Reference


Additional query words: STL STLSample list max_size size resize clear
Keywords : STLIss kbcode
Version : WINNT:4.2;
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 9, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.