STL Sample for the List::insert Function

Last reviewed: October 9, 1997
Article ID: Q158092
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::insert STL function in Visual C++.

MORE INFORMATION

Required Header

   <list>

Prototype

   iterator insert(iterator it, const T& x = T());
   void insert(iterator it, size_type n, const T& x);
   void insert(iterator it, const_iterator first, const_iterator last);
   void insert(iterator it, const T *first, const T *last);

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

Each of the member functions inserts, after the element pointed to by it in the controlled sequence, a sequence specified by the remaining operands. The first member function inserts a single element with value x and returns an iterator that points to the newly inserted element. The second member function inserts a repetition of n elements of value x. The last two member functions insert the sequence [first, last).

Sample Code

   //////////////////////////////////////////////////////////////////////
   //
   // Compile options needed: -GX
   //
   // insert.cpp :  Shows the various ways to insert elements into a
   //               list<T>.
   //
   // Functions:
   //
   //    list::insert
   //
   // Written by Andrew Bradnan
   // Copyright (c) 1996 Microsoft Corporation. All rights reserved.
   //////////////////////////////////////////////////////////////////////

   #include <list>
   #include <iostream>

   typedef list<int, allocator<int> > LISTINT;

   void main()
   {
       int rgTest1[] = {5,6,7};
       int rgTest2[] = {10,11,12};

       LISTINT listInt;
       LISTINT listAnother;
       LISTINT::iterator i;

       // Insert one at a time
       listInt.insert (listInt.begin(), 2);
       listInt.insert (listInt.begin(), 1);
       listInt.insert (listInt.end(), 3);

       // 1 2 3
       for (i = listInt.begin(); i != listInt.end(); ++i)
           cout << *i << " ";
       cout << endl;

       // Insert 3 fours
       listInt.insert (listInt.end(), 3, 4);

       // 1 2 3 4 4 4
       for (i = listInt.begin(); i != listInt.end(); ++i)
           cout << *i << " ";
       cout << endl;

       // Insert an array in there
       listInt.insert (listInt.end(), rgTest1, rgTest1 + 3);

       // 1 2 3 4 4 4 5 6 7
       for (i = listInt.begin(); i != listInt.end(); ++i)
           cout << *i << " ";
       cout << endl;

       // Insert another LISTINT
       listAnother.insert (listAnother.begin(), rgTest2, rgTest2+3);
       listInt.insert (listInt.end(), listAnother.begin(),
       listAnother.end());

       // 1 2 3 4 4 4 5 6 7 10 11 12
       for (i = listInt.begin(); i != listInt.end(); ++i)
           cout << *i << " ";
       cout << endl;
   }

Program Output is as follows:

1 2 3 1 2 3 4 4 4 1 2 3 4 4 4 5 6 7 1 2 3 4 4 4 5 6 7 10 11 12

REFERENCES

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


Additional query words: STL STLSample list::insert
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.