Thread: std::find method seems to have the effect of altering the arguments!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User FortranLevelC++'s Avatar
    Join Date
    May 2013
    Location
    United States
    Posts
    81

    std::find method seems to have the effect of altering the arguments!

    Hello, and thank you for your precious time.

    I have a question about the std::find method applied to std::vector. It seems to me that there is a danger that applying the std::find method to find a certain element in a vector might actually have the effect of altering the original vector in some way.

    For the sake of completeness, I am using C++11 in the Ubuntu Linux environment with Code::Blocks, but I am sure that the same thing would happen in any other operating system and compiler.

    I am pasting my own code at the end of the message, but before I do that, I am also pasting the reference to the std::find method:

    http://www.cplusplus.com/

    If I understood what that reference above says, that std::find above would somehow affect the original array (or vector in my case).


    In my own function 'Vol_ForGroupOfSymbols( ... )' that I am pasting below, in the arguments of the function, if I make the second vector argument constant 'const' ( my function will search an element inside that vector argument by means of the std:find method), the compiler gives an error message and it says "error: no match for the operator ='. But if I relax the requirement that the vector argument that I am passing to my function should be const, then the compiler does not give an error message and the executable is produced without problem. Is there a way for me to make sure that the argument is not altered, at least after the function goes out of scope?

    /*-------------------------------------------------------------------------*/
    // Below, in my function ,Vol_ForGroupOfSymbols the issue is the second argument
    Code:
    const vector<string> & vec_GroupSymbolList
    which the compiler does not allow me to pass it as a const, if I remove that 'const' in front of the argument
    Code:
     vector<string> &  vec_GroupSymbolList
    , then everything is fine, and the compiler does not complain. But if I try to make the argument constant as 'const' as in
    Code:
    const vector<string> &  vec_GroupSymbolList'
    then the compiler gives that error message. What can I do to make sure that that vector argument that I am passing to my function is not in danger of being altered when the function is called? As a last resort I can certainly create an extra copy of the original vector and pass that extra copy to the function as argument, but this would take extra memory. Any suggestions will be appreciated.

    Code:
    /// vector<float> Vol_ForGroupOfSymbols( const  vector<RAStruct> & v1_RA_DataForRSSymbol,   vector<string> &  vec_GroupSymbolList,  const vector<vector<RAStruct > > & vec_LocalAllSymRecs  )
    
    
     vector<float> Vol_ForGroupOfSymbols( const  vector<RAStruct> & v1_RA_DataForRSSymbol, const vector<string> &  vec_GroupSymbolList,  const vector<vector<RAStruct > > & vec_LocalAllSymRecs  )
    {
       vector<float> v1_VolForGroupOfSymbols;
       map<string, int> MapLocalDatesToCoordsForGuide;
    
    
       for(int i=0; i<v1_RA_DataForRSSymbol.size(); i++) {
          MapLocalDatesToCoordsForGuide[v1_RA_DataForRSSymbol[i].Date] = i ;
       }
    
    
    
    
       for(int i=0; i<v1_RA_DataForRSSymbol.size(); i++) {
          v1_VolForGroupOfSymbols.push_back(0); ; // Initialize to zero
       }
    
    
    
    
       std::vector<string>::iterator itrSymLoc;
    
    
       int FoundSymbolCounter  =0;
       int MissedSymbolCounter = 0;
       int iCoord;
       for (int i=0; i< vec_LocalAllSymRecs.size(); i++) { // here TotalNumSymbols is the num symbols in core RA file.
          int MissedDateCounter = 0;
    
    
          itrSymLoc = std::find(vec_GroupSymbolList.begin(), vec_GroupSymbolList.end(), vec_LocalAllSymRecs[i][0].Symbol   );
    
    
          if ( itrSymLoc != vec_GroupSymbolList.end()  ) {
             FoundSymbolCounter++;
             for (int j =0; j<vec_LocalAllSymRecs[i].size(); j++) {// Note that index j starts from j=1 instead of j=0
                if ((MapLocalDatesToCoordsForGuide.count( vec_LocalAllSymRecs[i][j].Date  ) >0) &&
                      ((iCoord ==  Map_DateToCoordsRefSym[ vec_LocalAllSymRecs[i][j].Date ]) > 0)) {
    
    
                   v1_VolForGroupOfSymbols[iCoord  ] +=  vec_LocalAllSymRecs[i][j].Volume ;
    
    
    
    
                } else {
                   MissedDateCounter++;
                }
             }
    
    
          } else {
             MissedSymbolCounter++;
    
    
          }
    
    
       }
    
    
       return v1_VolForGroupOfSymbols;
    }
    Last edited by FortranLevelC++; 11-26-2018 at 03:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble passing arguments to method
    By Dale in forum C# Programming
    Replies: 8
    Last Post: 12-02-2011, 12:40 PM
  2. Replies: 8
    Last Post: 11-28-2011, 06:25 PM
  3. Arguments return method is not working for float
    By girish1026 in forum C Programming
    Replies: 3
    Last Post: 09-23-2010, 01:26 AM
  4. Find out which lib supports an method
    By hannehomuth in forum C Programming
    Replies: 1
    Last Post: 07-11-2008, 10:22 AM
  5. trouble with arguments in main method
    By vopo in forum C Programming
    Replies: 3
    Last Post: 07-15-2007, 10:14 PM

Tags for this Thread