Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / How to pass a vector to a function?

How to pass a vector to a function?

August 23, 2021 by James Palmer

It depends on if you want to pass the vector as a reference or as a pointer (I am disregarding the option of passing it by value as clearly undesirable).
As a reference:
int binarySearch(int first, int last, int search4, vector& random);

vector random(100);
// …
found = binarySearch(first, last, search4, random);

As a pointer:
int binarySearch(int first, int last, int search4, vector* random);

vector random(100);
// …
found = binarySearch(first, last, search4, &random);

Inside binarySearch, you will need to use . or -> to access the members of random correspondingly.
Issues with your current code

binarySearch expects a vector*, but you pass in a vector (missing a & before random)
You do not dereference the pointer inside binarySearch before using it (for example, random[mid] should be (*random)[mid]
You are missing using namespace std; after the s
The values you assign to first and last are wrong (should be 0 and 99 instead of random[0] and random[99]

You’ll have to pass the pointer to the vector, not the vector itself. Note the additional ‘&’ here:
found = binarySearch(first, last, search4, &random);

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in