Microsoft KB Archive/163359

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

Article ID: 163359

Article Last Modified on 9/17/2003

-

APPLIES TO

 The Standard C++ Library, when used with:  Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul> </li> Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

-

<div class="notice_section">

This article was previously published under Q163359

<div class="notice_section">

NOTE: Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.

<div class="summary_section">

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

<div class="moreinformation_section">

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 #include #include #if _MSC_VER > 1020  // if VC++ version is > 4.2 using namespace std; // std c++ libs implemented in std #endif

typedef list<string, allocator > 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.

<div class="references_section">