Help and Support
 

powered byLive Search

How to use the list::insert Standard Template Library (STL) function in Visual C++

Article ID:158092
Last Review:July 14, 2005
Revision:4.0
This article was previously published under Q158092
On This Page

SUMMARY

The sample code below illustrates how to use the list::insert STL function in Visual C++.

Back to the top

MORE INFORMATION

Required header

   <list>
				

Back to the top

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.

Back to the top

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).

Back to the top

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>
   using namespace std;

#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
   #endif

   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
				

Back to the top

REFERENCES

For more information about list::insert, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn.microsoft.com/library/en-us/vclang98/html/SAMPLE_LISTCCINSERT_(STL_SAMPLE).asp?frame=true (http://msdn.microsoft.com/library/en-us/vclang98/html/SAMPLE_LISTCCINSERT_(STL_SAMPLE).asp?frame=true)

Back to the top


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
  Microsoft Visual C++ 5.0 Professional Edition
  Microsoft Visual C++ 6.0 Professional Edition
  Microsoft Visual C++ 6.0 Standard Edition
Microsoft Visual C++ .NET 2002 Standard Edition
Microsoft Visual C++ .NET 2003 Standard Edition

Back to the top

Keywords: 
kbhowto kbinfo kbstl kbcode kbfunctions KB158092

Back to the top

Article Translations

 

Related Support Centers

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.