Results 1 to 6 of 6

Thread: How the iter_swap() can be used in the C++

  1. #1
    Join Date
    Jan 2010
    Posts
    29

    How the iter_swap() can be used in the C++

    hello, I am the student of the BscIT. I have to submit the Project on the C++ language in two days. i can have the good knowledge of the functions that can be used in the C++ language. Apart of this, I can not know anything about the iter_swap() function of the C++ language. So, I want to know about the iter_swap() function of the C++ language. I also want to know how can I used the iter_swap() function in the C++ language.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    iter_swap() in the C++

    Hi, The iter_swap() function can be used in the C++ language to an exchange the values of an objects that can be pointed by the two iterators. The following can be the general syntax of the iter_swap() function in the C++ language :
    Code:
    template <class FrwrdItrtr1, class FrwrdItrtr2>
      void iter_swap ( FrwrdItrtr1 p, FrwrdItrtr2 q );

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    The iter_swap()

    The following program can help's you that how to use the iter_swap() function in the C++ language :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main () 
    {
      int mint[]={10,20,30,40,50 };   
      vector<int> mvctr (4,99);        
      iter_swap(mint,mvctr.begin());    
      iter_swap(mint+3,mvctr.begin()+2); 
     cout << "mvctr contains:";
      for (vector<int>::iterator t=mvctr.begin(); t!=mvctr.end(); ++t)
        cout << " " << *t;
      cout << endl;
      return 0;
    }
    The above code can generate the following Output :
    Code:
    mvctr contains: 10 99 40 99

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    iter_swap()

    The iter_swap() function of the C++ language can assigns the content of the argument *p to *q and the content of the argument *q to *p. The behavior of iter_swap() function template can be similar to the following :
    Code:
    template <class FrwrdItrtr1, class FrwrdItrtr2>
      void iter_swap ( FrwrdItrtr1 p, FrwrdItrtr2 q )
    {
      swap (*p, *q);
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    iter_swap() can be used in the C++

    There can be the one more example on the iter_swap() function of the C++ language :
    Code:
    #include <iostream>
    using std::ct;
    using std::endl;
    #include <algorithm>
    #include <iterator>
    int main()
    {
       int b[ 9 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
       std::ostreamiterator< int > otpt( ct, " " );
       cout << "Array a contains:\n   ";
       std::copy( b, b + 9, otpt );
       std::iter_swap( &b[ 0 ], &b[ 1 ] );
       cout << "\n Array a after swapping b[0] and b[1] using iter_swap:\n   ";
       std::copy( b, b + 9, otpt );
       cout << endl;
       return 0;
    }
    Code:
     Output :
    Array b contains:
       1 2 3 4 5 6 7 8 9
    Array b after swapping b[0] and b[1] using iter_swap:
       2 1 3 4 5 6 7 8 9
    Last edited by Zecho; 18-02-2010 at 04:29 PM.

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How the iter_swap() can be used in the C++

    The iter_swap() function of the C++ language can be comes under the Algorithm Header Files. The iter_swap() function algorithm can exchanges the two values in the two locations.
    Code:
    Syntax:
        #include <algorithm>
        void iter_swap( frwrditrtr p, frwrditrtr q );
    Whenever the iter_swap() function can be called then the iter_swap() function can exchanges the values of two elements same as that of when you called the function :
    Code:
    swap( *p, *q );

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,830,932.95542 seconds with 15 queries