Thread: Finding common values in the rows of a vector [Need Help]

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,458
    C++ passes everything into functions by value. This means that the compiler copies the variable when passing to a function. Copying a vector or any other non-trivially constructed class (like a std::string) can be expensive to copy therefore you want to try to eliminate any unnecessary copying.

    In C++ this can be done two ways, passing by reference, or passing by pointer reference.

    For more information I suggest you use your favourite search engine and look up some documentation on "C++ pass by reference".

  2. #17
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Quote Originally Posted by jimblumberg View Post
    C++ passes everything into functions by value. This means that the compiler copies the variable when passing to a function. Copying a vector or any other non-trivially constructed class (like a std::string) can be expensive to copy therefore you want to try to eliminate any unnecessary copying.

    In C++ this can be done two ways, passing by reference, or passing by pointer reference.

    For more information I suggest you use your favourite search engine and look up some documentation on "C++ pass by reference".
    Like so?:
    Code:
    int gcd(const int& a, const int& b) {
        return a == 0 ? b : gcd(b % a, a);
    }
    
     unsigned int find_gcd(const vector <int>& arr) {
        unsigned int result = arr[0];
        for (unsigned int i = 1; i < arr.size(); i++)
            result = gcd(arr[i], result);
        return result;
    }
    I'm still a still confused about when pointer is used over reference and why "const" is used though.

    Thanks for the tip jim. I would have never known about this.
    Last edited by Nwb; 10-06-2018 at 10:45 AM.

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,458
    Well since this is C++ and in C++ you try to avoid pointers, passing by reference should be preferred.

    You pass by non-const reference when you want to be able to change the item inside your function, and by const reference when you don't want the function to alter the item.

    By the way don't forget that last line in my previous post. If you have doubts your first step should be reading documentation, you can find most documentation online with your search engine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array Problem - Finding each rows minimum value
    By Jordan Velez in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2015, 01:04 PM
  2. Finding a common suffix
    By echo1525 in forum C Programming
    Replies: 3
    Last Post: 11-12-2012, 12:35 AM
  3. finding common numbers
    By BB89 in forum C Programming
    Replies: 14
    Last Post: 11-10-2009, 08:22 AM
  4. finding most common char in a string
    By scwizzo in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2007, 01:22 PM
  5. Finding the most common char
    By Mikro in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 09:00 AM

Tags for this Thread