Results 1 to 5 of 5

Thread: How can I use the swap_ranges() in CPP

  1. #1
    Join Date
    Dec 2009
    Posts
    28

    How can I use the swap_ranges() in CPP

    Hi, Everyone. I am the student of the MSC second year. I had learned the C/C++ language in BSC-IT. I can also have the good knowledge of the C++ language. I have to make the project on the swap_ranges() function in the C++ language. So, I would like to know about the swap_ranges() function of the C++ language. I also would like to know How can I use the swap_ranges() function in the C++ program. Can anyone has the knowledge of the swap_ranges() function in the C++ language.
    Last edited by Gaauge; 17-02-2010 at 09:02 PM.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    How can I use the swap_ranges()

    The swap_ranges() function of the C++ language can be used to swap the values of the each and every element that can be in the given range that can be specified in the [fst, lst) with their respective element that can be begin at the range fst. I hope you should understood from the above explanation that can really helps you. If you can not then reply me. I will try to provide the other explanation.

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

    The swap_ranges()

    I hope you the following code of lines help's you in to gain the information that can be useful for your project. I suggest you to carefully study and then try to run program for output :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main ()
     {
      vector<int> fst (5,10);      
      vector<int> scnd (5,33);  
      vector<int>::iterator i;
      swap_ranges(fst.begin()+1, fst.end()-1, scnd.begin());
      cout << " first contains:";
      for (i=fst.begin(); i!=fst.end(); ++i)
        cout << " " << *i;
      cout << "\nsecond contains:";
      for (i=scnd.begin(); i!=scnd.end(); ++i)
        cout << " " << *i;
      cout << endl;
      return 0;
    }
    Output:
    Code:
    first contains: 10 33 33 33 10
    second contains: 10 10 10 33 33

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Use the swap_ranges() in CPP

    The following can be the parameters of the swap_ranges() function that can be defined in the syntax :
    1. fst2: This parameter of the swap_ranges() function can be used for the forward iterator to the starting position in the another sequence that can be swapped. The range that can be used can includes the same number of an elements as the range [fst1,lst1). The two ranges could not be overlap.
    2. fst1, lst1 : These parameters of the swap_ranges() function can be used for forward iterators to the final and starting positions in one of the sequences that can be swapped. The range used can be as [first1,last1).

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    swap_ranges() in CPP

    Program :
    Code:
    #include <vector>
    #include <deque>
    #include <algorithm>
    #include <iostream>
    int main( ) 
    {
       using namespace std;
       vector <int> w1;
       deque <int> e1;
       vector <int>::iterator w1Iter1;
       deque<int>::iterator e1Iter1;
       int j;
       for ( j = 0 ; j <= 5 ; j++ )
       {
          w1.push_back( j );
       }
       int jj;
       for ( jj =4 ; jj <= 9 ; jj++ )
       {
          e1.pushback( 6 );
       }
       cout << "Vector w1 is ( " ;
       for ( w1Iter1 = w1.begin( ) ; w1Iter1 != w1.end( ) ;w1Iter1 ++ )
       cout << *w1Iter1  << " ";
       cout << ")." << endl;
       cout << "Deque e1 is  ( " ;
       for ( e1Iter1 = e1.begin( ) ; e1Iter1 != e1.end( ) ;e1Iter1 ++ )
       cout << *e1Iter1  << " ";
       cout << ")." << endl;
       swap_ranges ( w1.begin ( ) , w1.end ( ) , w1.begin ( ) );
       cout << "After the swap_range, vector w1 is ( " ;
       for ( w1Iter1 = w1.begin( ) ; w1Iter1 != w1.end( ) ;w1Iter1 ++ )
       cout << *w1Iter1 << " ";
       cout << ")." << endl;
       cout << "After the swap_range deque d1 is   ( " ;
       for ( e1Iter1 = e1.begin( ) ; e1Iter1 != e1.end( ) ;e1Iter1 ++ )
       cout << *e1Iter1 << " ";
       cout << ")." << endl;
    }
    Output :
    Code:
    Vector w1 is ( 0 1 2 3 4 5 ).
    Deque e1 is  ( 6 6 6 6 6 6 ).
    After the swap_range, vector w1 is ( 6 6 6 6 6 6 ).
    After the swap_range deque e1 is   ( 0 1 2 3 4 5 ).

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,803,629.60055 seconds with 15 queries