C++ Ptr<...>

Discussion in 'Programmer's Corner' started by bug13, Jul 29, 2014.

  1. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
  2. mitko89

    Member

    Sep 20, 2012
    123
    19
    It's a vector.
     
  3. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    So I don't need the keyword vector to declare a vector?
     
    Last edited: Jul 29, 2014
  4. mitko89

    Member

    Sep 20, 2012
    123
    19
    Check http://en.wikipedia.org/wiki/Sequence_container_(C++)

    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3. #include <vector>
    4. #include <algorithm> // sort, max_element, random_shuffle, remove_if, lower_bound
    5. #include <functional> // greater, bind2nd
    6. // used here for convenience, use judiciously in real programs.
    7. using namespace std;
    8.  
    9. int main()
    10. {
    11.   int arr[4] = {1, 2, 3, 4};
    12.   // initialize a vector from an array
    13.   vector<int> numbers(arr, arr+4);
    14.   // insert more numbers into the vector
    15.   numbers.push_back(5);
    16.   numbers.push_back(6);
    17.   numbers.push_back(7);
    18.   numbers.push_back(8);
    19.   // the vector currently holds {1, 2, 3, 4, 5, 6, 7, 8}
    20.  
    21.   // randomly shuffle the elements
    22.   random_shuffle( numbers.begin(), numbers.end() );
    23.  
    24.   // locate the largest element, O(n)
    25.   vector<int>::const_iterator largest =
    26.     max_element( numbers.begin(), numbers.end() );
    27.  
    28.   cout << "The largest number is " << *largest << "\n";
    29.   cout << "It is located at index " << largest - numbers.begin() << "\n";
    30.  
    31.   // sort the elements
    32.   sort( numbers.begin(), numbers.end() );
    33.  
    34.   // find the position of the number 5 in the vector
    35.   vector<int>::const_iterator five =
    36.     lower_bound( numbers.begin(), numbers.end(), 5 );  
    37.  
    38.   cout << "The number 5 is located at index " << five - numbers.begin() << "\n";
    39.  
    40.   // erase all the elements greater than 4  
    41.   numbers.erase( remove_if(numbers.begin(), numbers.end(),
    42.     bind2nd(greater<int>(), 4) ), numbers.end() );
    43.  
    44.   // print all the remaining numbers
    45.   for(vector<int>::const_iterator it = numbers.begin(); it != numbers.end(); ++it)
    46.   {
    47.     cout << *it << " ";
    48.   }
    49.  
    50.   return 0;
    51. }
    52.  
     
    Last edited by a moderator: Jul 29, 2014
  5. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    Hi

    Sorry I am still lost, so what I normally declare a vector like this:

    Code ( (Unknown Language)):
    1. vector <type> vector_name;
    But why:

    Code ( (Unknown Language)):
    1. Prt <type> vector_name;
    Was used? What is the Prt keyword? Is that some special type of vector?
     
  6. vpoko

    Member

    Jan 5, 2012
    258
    47
    Ptr is a template class (AKA a generic class). Templates allow a class to be used with various types, while still checking for type mismatches at compile time.

    Ptr<FaceRecognizer> in your example is declaring an instance of the Ptr class (which, I'm guessing, is some kind of smart pointer, though I'm not sure - there's no type by that name in the C++ standard library) that will work with an instance of FaceRecognizer.

    Vector is, itself, a template class that can work with various types (e.g., vector<int>).
     
    bug13 likes this.
Loading...