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 argumentwhich the compiler does not allow me to pass it as a const, if I remove that 'const' in front of the argumentCode:const 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 inCode:vector<string> & vec_GroupSymbolListthen 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:const vector<string> & vec_GroupSymbolList'
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; }