×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

valarray swap() Function in C++ with Examples

C++ valarray swap() Function: Here, we will learn about the swap() function, its usages, syntax and examples.
Submitted by Shivang Yadav, on May 08, 2022

The valarray class in C++ is a special container that is used for holding elements like an array and performing operations on them.

C++ STL std::swap (valarray) Function

The swap() function is defined in valarray class, and it is used to swap the content of one valarray with another valarray.

Syntax

template <class T>
void swap (valarray<T>& x, valarray<T>& y) noexcept;

// or
valarray1.swap(valarray2)

Parameter(s)

The function accepts a single parameter which is the second valarray to be swapped.

Return value

The method does not return any value.

Example 1

#include <iostream>
#include <valarray>
using namespace std;

int main()
{
    // Declaring valarray
    valarray<int> myvalarr1 = { 1, 9, 3, 4, 7 };
    valarray<int> myvalarr2 = { 4, 5, 6, 8, 2 };

    // Printing the elements of valarray
    cout << "\nThe elements stored in valarray1 are : ";
    for (int& ele : myvalarr1)
        cout << ele << " ";

    cout << "\nThe elements stored in valarray2 are : ";
    for (int& ele : myvalarr2)
        cout << ele << " ";

    cout << "\nAfter Shifting the valarrays ";
    myvalarr1.swap(myvalarr2);

    cout << "\nThe elements stored in valarray1 are : ";
    for (int& ele : myvalarr1)
        cout << ele << " ";

    cout << "\nThe elements stored in valarray2 are : ";
    for (int& ele : myvalarr2)
        cout << ele << " ";

    return 0;
}

Output

The elements stored in valarray1 are : 1 9 3 4 7 
The elements stored in valarray2 are : 4 5 6 8 2 
After Shifting the valarrays 
The elements stored in valarray1 are : 4 5 6 8 2 
The elements stored in valarray2 are : 1 9 3 4 7

Example 2

#include <iostream>
#include <valarray>
using namespace std;

int main()
{
    // Declaring valarray
    valarray<int> arr1 = { 10, 20, 30, 40 };
    valarray<int> arr2 = { 11, 22, 33, 44 };

    // printing elements of valarrays
    // before swapping
    cout << "Before swapping: " << endl;
    cout << "arr1: ";
    for (int& i : arr1)
        cout << i << " ";
    cout << endl;

    cout << "arr2: ";
    for (int& i : arr2)
        cout << i << " ";
    cout << endl;

    arr1.swap(arr2);

    // printing elements of valarrays
    // after swapping
    cout << "After swapping: " << endl;
    cout << "arr1: ";
    for (int& i : arr1)
        cout << i << " ";
    cout << endl;

    cout << "arr2: ";
    for (int& i : arr2)
        cout << i << " ";
    cout << endl;

    return 0;
}

Output

Before swapping: 
arr1: 10 20 30 40 
arr2: 11 22 33 44 
After swapping: 
arr1: 11 22 33 44 
arr2: 10 20 30 40 

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.