Algorithms in C++


The Standard Template Library (STL) in C++ provides a wide range of algorithms to perform operations like searching, sorting, and data manipulation. These algorithms work seamlessly with STL containers, making them highly efficient and easy to use.

1. Searching Algorithms

Searching algorithms are used to find elements in a container. The most commonly used searching algorithms in STL include:

1.1 Binary Search

Binary search checks if an element exists in a sorted container.

Example:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;

    int main() {
        vector numbers = {10, 20, 30, 40, 50};
        if (binary_search(numbers.begin(), numbers.end(), 30)) {
            cout << "Element found" << endl;
        } else {
            cout << "Element not found" << endl;
        }
        return 0;
    }
        

1.2 Find

The find algorithm searches for an element in a container and returns an iterator to it.

Example:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    int main() {
        vector numbers = {10, 20, 30, 40, 50};
        auto it = find(numbers.begin(), numbers.end(), 30);
        if (it != numbers.end()) {
            cout << "Element found at position: " << distance(numbers.begin(), it) << endl;
        } else {
            cout << "Element not found" << endl;
        }
        return 0;
    }
        

2. Sorting Algorithms

Sorting algorithms arrange the elements of a container in a specific order. STL provides the sort function for this purpose.

2.1 Sort

The sort algorithm sorts elements in ascending order by default.

Example:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    int main() {
        vector numbers = {50, 20, 40, 10, 30};
        sort(numbers.begin(), numbers.end());
        for (int num : numbers) {
            cout << num << " ";
        }
        return 0;
    }
        

2.2 Custom Sort

You can define a custom comparison function for sorting.

Example:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    bool customCompare(int a, int b) {
        return a > b; // Descending order
    }

    int main() {
        vector numbers = {50, 20, 40, 10, 30};
        sort(numbers.begin(), numbers.end(), customCompare);
        for (int num : numbers) {
            cout << num << " ";
        }
        return 0;
    }
        

3. Manipulation Algorithms

Manipulation algorithms are used to modify the elements of a container.

3.1 Reverse

The reverse algorithm reverses the order of elements in a container.

Example:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    int main() {
        vector numbers = {10, 20, 30, 40, 50};
        reverse(numbers.begin(), numbers.end());
        for (int num : numbers) {
            cout << num << " ";
        }
        return 0;
    }
        

3.2 Replace

The replace algorithm replaces all occurrences of a specific value with another value.

Example:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    int main() {
        vector numbers = {10, 20, 30, 20, 50};
        replace(numbers.begin(), numbers.end(), 20, 99);
        for (int num : numbers) {
            cout << num << " ";
        }
        return 0;
    }
        

3.3 Remove

The remove algorithm removes elements that match a given value.

Example:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    int main() {
        vector numbers = {10, 20, 30, 20, 50};
        auto it = remove(numbers.begin(), numbers.end(), 20);
        numbers.erase(it, numbers.end()); // Erase the trailing elements
        for (int num : numbers) {
            cout << num << " ";
        }
        return 0;
    }
        

Conclusion

STL algorithms in C++ provide efficient ways to perform common operations like searching, sorting, and manipulation. Using these algorithms helps reduce code complexity and improve performance.





Advertisement