C++ Ptr<...>

mitko89

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

Rich (BB code):
#include <iostream>
#include <vector>
#include <algorithm> // sort, max_element, random_shuffle, remove_if, lower_bound 
#include <functional> // greater, bind2nd
// used here for convenience, use judiciously in real programs. 
using namespace std;
 
int main()
{
  int arr[4] = {1, 2, 3, 4};
  // initialize a vector from an array
  vector<int> numbers(arr, arr+4);
  // insert more numbers into the vector
  numbers.push_back(5);
  numbers.push_back(6);
  numbers.push_back(7);
  numbers.push_back(8);
  // the vector currently holds {1, 2, 3, 4, 5, 6, 7, 8}
 
  // randomly shuffle the elements
  random_shuffle( numbers.begin(), numbers.end() );
 
  // locate the largest element, O(n)
  vector<int>::const_iterator largest = 
    max_element( numbers.begin(), numbers.end() );
 
  cout << "The largest number is " << *largest << "\n";
  cout << "It is located at index " << largest - numbers.begin() << "\n";
 
  // sort the elements
  sort( numbers.begin(), numbers.end() );
 
  // find the position of the number 5 in the vector 
  vector<int>::const_iterator five = 
    lower_bound( numbers.begin(), numbers.end(), 5 );  
 
  cout << "The number 5 is located at index " << five - numbers.begin() << "\n";
 
  // erase all the elements greater than 4   
  numbers.erase( remove_if(numbers.begin(), numbers.end(), 
    bind2nd(greater<int>(), 4) ), numbers.end() );
 
  // print all the remaining numbers
  for(vector<int>::const_iterator it = numbers.begin(); it != numbers.end(); ++it)
  {
    cout << *it << " ";
  }
 
  return 0;
}
 
Last edited by a moderator:

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Hi

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

Rich (BB code):
vector <type> vector_name;
But why:

Rich (BB code):
Prt <type> vector_name;
Was used? What is the Prt keyword? Is that some special type of vector?
 

vpoko

Joined Jan 5, 2012
267
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>).
 
Thread starter Similar threads Forum Replies Date
K Programming & Languages 10
Top