How to Return a Reference to Vector C

The C/C++ programming language has a Standard Template Library (STL) that provides many useful data containers. One such data container is the vector. A vector is a list of sequential items and is quite like an array. However, an array has a fixed sized determined during declaration. A vector does not have a fixed sized, and it can grow or shrink as its contents are manipulated. A vector should be passed by reference whenever possible, since it can contain many items and may be costly to move by value. Passing a vector by reference uses standard C/C++ reference operator semantics.

Things You'll Need

  • C/C++ Integrated Development Environment (IDE), such as Eclipse CDT
  • C/C++ Compiler, such as GCC
Show More

Instructions

    • 1

      Load up the C/C++ IDE by clicking on its program icon. After it loads, navigate to "File" followed by "New" and "Project." Select "C++ Project" to create a new C++ project. A blank source code file appears in the main editor window of the IDE.

    • 2

      Import the vector library by writing the following line at the top of the source code file:

      #import <vector>

    • 3

      Create a main function by writing these lines of code:

      int main() { }

    • 4

      Declare a vector of integer data types by writing the following between the curly brackets of the main function:

      vector<int> v;

    • 5

      Pass the vector by reference to a function named "Foo()" using the "&" operator. Passing by reference does not copy the entire contents of the vector -- it copies a reference to the vector's location in memory. The syntax for passing by reference looks like this:

      Foo(&v);

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured