#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0

    Std::string& in c++


    i'm new in c++, i have a function for enumerating files in a directory :


    Code:
    int EnumerateFiles(const std::string& target_path, const std::string& ext, std::vector<std::string>& res){             
        boost::filesystem::directory_iterator end_itr: // default ctor yields past-the-end
        for(boost::filesystem::directory_iterator i(target_path); i != end_itr; ++it){
          // skip if not a file
          if( !boost::filesystem::is_regular_file( i->status() ) ) continue;
          // skip if no match with extension
          if( i->path().extension() != ext.c_str() ) continue;
          // file matches, store it
          res.push_back(i->path().filename().c_str());
        }
    }
    i would like to understand this function and understand std::string& and why they add & after std::string ?

    thanks in advance.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by salem
    i would like to call this function
    can you help me to use it ?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Today, 05:53 PM Salem said...
    Today, 06:02 PM devos said...
    I would suggest that you should spend more than 10 minutes actually READING some of the links given to you, before bouncing right back with another "feed me" post.

    Since you weren't online when I posted, there must have been a good few minutes delay for the notifications to propagate to you. I'm doubtful you even read anything in your rush to hit the reply button.

    If you're going to give up that easily, then programming isn't for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by salem
    Today, 05:53 PM Salem said...
    Today, 06:02 PM devos said...
    I would suggest that you should spend more than 10 minutes actually READING some of the links given to you, before bouncing right back with another "feed me" post.

    Since you weren't online when I posted, there must have been a good few minutes delay for the notifications to propagate to you. I'm doubtful you even read anything in your rush to hit the reply button.

    If you're going to give up that easily, then programming isn't for you.
    i understood what the & mean but the whol program i didnt got any thinkg i'm new in c++
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    If you had any more questions after your first and only one, which directly concerned use of the amper, then you need to ask. IOW, just what is it about the program that you do not understand? We cannot know that until you tell us, since we are not mind-readers.

    Is the problem the use of namespaces? Certainly, not using a using statement, which thus requires inserting the namespace everywhere in the code, makes for more cluttered looking code.

    Is the problem boost? Are you using boost? I certainly never have and have only ever heard of it on this forum. If you are not using boost and don't know enough to translate the boost elements back to normal C++, then this program is not for you.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by dwise1_aol
    If you had any more questions after your first and only one, which directly concerned use of the amper, then you need to ask. IOW, just what is it about the program that you do not understand? We cannot know that until you tell us, since we are not mind-readers.

    Is the problem the use of namespaces? Certainly, not using a using statement, which thus requires inserting the namespace everywhere in the code, makes for more cluttered looking code.

    Is the problem boost? Are you using boost? I certainly never have and have only ever heard of it on this forum. If you are not using boost and don't know enough to translate the boost elements back to normal C++, then this program is not for you.
    Ok let me tell you,
    i'm working in a project in openCV in C++, i used a program already exist in /samples/c of openCV library.
    this is the following program :

    Code:
        /**
     * @file MatchTemplate_Demo.cpp
     * @brief Sample code to use the function MatchTemplate
     * @author OpenCV team
     */
    
    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    using namespace cv;
    
    /// Global Variables
    Mat img; Mat templ; Mat result;
    const char* image_window = "Source Image";
    const char* result_window = "Result window";
    
    int match_method;
    int max_Trackbar = 5;
    
    /// Function Headers
    void MatchingMethod( int, void* );
    
    /**
     * @function main
     */
    int main( int, char** argv )
    {
      /// Load image and template
      img = imread( argv[1], 1 );
      templ = imread( argv[2], 1 );
    
      /// Create windows
      namedWindow( image_window, WINDOW_AUTOSIZE );
      namedWindow( result_window, WINDOW_AUTOSIZE );
    
      /// Create Trackbar
      const char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
      createTrackbar( trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod );
    
      MatchingMethod( 0, 0 );
    
      waitKey(0);
      return 0;
    }
    
    /**
     * @function MatchingMethod
     * @brief Trackbar callback
     */
    void MatchingMethod( int, void* )
    {
      /// Source image to display
      Mat img_display;
      img.copyTo( img_display );
    
      /// Create the result matrix
      int result_cols =  img.cols - templ.cols + 1;
      int result_rows = img.rows - templ.rows + 1;
    
      result.create( result_cols, result_rows, CV_32FC1 );
    
      /// Do the Matching and Normalize
      matchTemplate( img, templ, result, match_method );
      normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
    
      /// Localizing the best match with minMaxLoc
      double minVal; double maxVal; Point minLoc; Point maxLoc;
      Point matchLoc;
    
      minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
    
    
      /// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
      if( match_method  == TM_SQDIFF || match_method == TM_SQDIFF_NORMED )
        { matchLoc = minLoc; }
      else
        { matchLoc = maxLoc; }
    
      /// Show me what you got
      rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
      rectangle( result, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
    
      imshow( image_window, img_display );
      imshow( result_window, result );
    
      return;
    }
    it's only applied in one picture,i would like to apply this program in many images within a directory.

    somone saggeted me this solution :

    ---------------------------------------

    If you want to apply a method (here matchTemplate) to several images stored in a directory, you should use a function to store the names of every image within this directory into a vector (i.e. std::vector<std::string>) and iterate on it.

    Here is an example using boost:

    Code:
    int EnumerateFiles(const std::string& target_path, const std::string& ext, std::vector<std::string>& res){              
        boost::filesystem::directory_iterator end_itr: // default ctor yields past-the-end
        for(boost::filesystem::directory_iterator i(target_path); i != end_itr; ++it){
          // skip if not a file
          if( !boost::filesystem::is_regular_file( i->status() ) ) continue;
          // skip if no match with extension
          if( i->path().extension() != ext.c_str() ) continue;
          // file matches, store it
          res.push_back(i->path().filename().c_str());
        }
    }
    Then you can iterate in your main function:

    Code:
    for(size_t i=0; i<res.size(); ++i){
       my_method(target_path+"/"+res.at(i), .. );
    }

    any hlp to implement this solution ?

IMN logo majestic logo threadwatch logo seochat tools logo