Using Non-Member Friend Functions With Vector in C++ STL Last Updated : 14 May, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, Non-member buddy functions may access a class's private members while not being members. The class definition declares them friend. Vector containers, dynamic arrays in C++ Standard Library (STL), may dynamically resize as members are added or deleted. You may need to establish a new class with a vector to utilize non-member friend methods on vector data. Example: C++ // C++ Program to use a non-member friend function with a // custom VectorWrapper class that contains a vector from // the C++ STL: #include <iostream> #include <vector> // Custom class containing a vector class VectorWrapper { private: std::vector<int> data; public: // Constructor VectorWrapper(const std::vector<int>& initialValues) : data(initialValues) { } // Declare a non-member friend function friend void printVector(const VectorWrapper& vw); }; // Non-member friend function that has access to // VectorWrapper's private members void printVector(const VectorWrapper& vw) { std::cout << "Vector elements: "; for (int value : vw.data) { std::cout << value << " "; } std::cout << std::endl; } int main() { std::vector<int> initialValues = { 1, 2, 3, 4, 5 }; VectorWrapper vw(initialValues); // Prints "Vector elements: 1 2 3 4 5" printVector(vw); return 0; } OutputVector elements: 1 2 3 4 5 Explanation of the above program In the above example, we have created a Vector Wrapper class that has a private vector named data. We have declared a non-member friend function printVector() that can access and print the vector data. This is just a simple example, and you can use a similar approach to create more complex friend functions to manipulate the vector data in the custom class. Time Complexity: O(n) Space difficulty: O(1) Comment More infoAdvertise with us Next Article std::is_member_pointer in C++ with Examples S sanketnagare Follow Improve Article Tags : C++ STL cpp-vector Practice Tags : CPPSTL Similar Reads std::is_member_function_pointer template in C++ with Examples The std::is_member_function_pointer template of C++ STL is present in the <type_traits> header file. The std::is_member_function_pointer template of C++ STL is used to check whether the T is a pointer to non-static member function or not. It return the boolean value true if T is pointer to non 2 min read Passing Vector to a Function in C++ To perform operations on vector belonging to one function inside other function, we pass this vector to the function as arguments during the function call.C++ provides three methods to pass a vector to a function in C++. Let's look at each of them one by one.Table of ContentPass Vector by ValuePass 5 min read Map of Vectors in C++ STL with Examples Map in STL Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. Vector in STL Vector is same as dynamic arrays with the ability to resize itself automatically when an element is insert 2 min read Initializing Vector using an Existing Vector in C++ STL A vector is a type of container which can store objects of similar data type. Vector acts like a dynamic array where we can insert elements and the size of the array increases depending upon the elements inserted. Syntax: vector<data_structure/type> vector_name(size, item) To know more about v 5 min read std::is_member_pointer in C++ with Examples The std::is_member_pointer template of C++ STL is present in the <type_traits> header file. The std::is_member_pointer template of C++ STL is used to check whether the T is a pointer to non-static member or not. It return the boolean value true if T is a pointer to non-static member type, Othe 2 min read 2D Vector of Tuples in C++ with Examples What is Vector? In C++, a vector is similar to dynamic arrays with the ability to resize itself automatically. Vector elements are stored in contiguous memory locations so that they can be accessed and traversed using iterators. Functions associated with a vector: begin(): Returns an iterator pointi 6 min read Like