Prev: _CrtIsValidHeapPointer Assertion in C++/CLI Application Debug mode.
Next: Error LNK2028 unresolved token / LNK2019 Unrevolved external symbol
From: Gerry Hickman on 9 Aug 2007 16:37 Hi, In an earlier thread entitled "STL vector - which style of for() loop?" I had a function that could populate a vector (with a variable number of strings) and pass it back to the caller, but people pointed out this would create a "copy" of the vector and it may be better to pass by reference. Doug Harrison offered this example: ----- example start ----- I would use pass-by-reference to avoid this needless cost, e.g. vector<string>::size_type void GetDeviceClasses(vector<string>& guids) { guids.clear(); // If you can estimate n, reserve can eliminate reallocations. // guids.reserve(n); ... returns guids.size(); } ----- example end ----- but when I came to actually code this, I ran into some problems. I managed to code something that appears to achieve the objective, but my code is almost "back-to-front" (in terms of * and &) of what Doug posted. Can someone clarify? ----- my attempt ----- using namespace std; // just for this demo vector<string> guids; PopulateStrings(&guids); cout << "Count of guids is now " << guids.size(); // prints 2 void PopulateStrings(vector<string> * guids) { guids->clear(); guids->push_back("test1"); guids->push_back("test2"); } ----- end my attempt ----- -- Gerry Hickman (London UK)
From: Scott McPhillips [MVP] on 9 Aug 2007 16:52 Gerry Hickman wrote: > Hi, > > In an earlier thread entitled "STL vector - which style of for() loop?" > I had a function that could populate a vector (with a variable number of > strings) and pass it back to the caller, but people pointed out this > would create a "copy" of the vector and it may be better to pass by > reference. Doug Harrison offered this example: > > ----- example start ----- > > I would use pass-by-reference to avoid this needless cost, e.g. > > vector<string>::size_type > void GetDeviceClasses(vector<string>& guids) > { > guids.clear(); > // If you can estimate n, reserve can eliminate reallocations. > // guids.reserve(n); > ... > returns guids.size(); > } > > ----- example end ----- > > but when I came to actually code this, I ran into some problems. I > managed to code something that appears to achieve the objective, but my > code is almost "back-to-front" (in terms of * and &) of what Doug > posted. Can someone clarify? > > ----- my attempt ----- > > using namespace std; // just for this demo > > vector<string> guids; > PopulateStrings(&guids); > cout << "Count of guids is now " << guids.size(); // prints 2 > > void PopulateStrings(vector<string> * guids) > { > guids->clear(); > guids->push_back("test1"); > guids->push_back("test2"); > } > > ----- end my attempt ----- > There are two ways to "pass by reference." They are to pass a reference, or to pass a pointer. Doug showed using a reference, your version is using a pointer. Performance would be equal either way. -- Scott McPhillips [MVP VC++]
From: Mikep on 9 Aug 2007 16:53 inline ---- "Gerry Hickman" <gerry666uk(a)newsgroup.nospam> wrote in message news:%23PFJQUs2HHA.4712(a)TK2MSFTNGP04.phx.gbl... > Hi, > > In an earlier thread entitled "STL vector - which style of for() loop?" I > had a function that could populate a vector (with a variable number of > strings) and pass it back to the caller, but people pointed out this would > create a "copy" of the vector and it may be better to pass by reference. > Doug Harrison offered this example: > > ----- example start ----- > > I would use pass-by-reference to avoid this needless cost, e.g. > > vector<string>::size_type > void GetDeviceClasses(vector<string>& guids) > { > guids.clear(); > // If you can estimate n, reserve can eliminate reallocations. > // guids.reserve(n); > ... > returns guids.size(); > } > > ----- example end ----- > > but when I came to actually code this, I ran into some problems. I managed > to code something that appears to achieve the objective, but my code is > almost "back-to-front" (in terms of * and &) of what Doug posted. Can > someone clarify? > > ----- my attempt ----- > > using namespace std; // just for this demo > > vector<string> guids; > PopulateStrings(&guids); ---->>> PopulateStrings(guids); // no& > cout << "Count of guids is now " << guids.size(); // prints 2 > > void PopulateStrings(vector<string> * guids) --->>> void > PopulateStrings(vector<string>& guids) // use & > { > guids->clear(); > guids->push_back("test1"); > guids->push_back("test2"); > } > > ----- end my attempt ----- > > -- > Gerry Hickman (London UK)
From: Ben Voigt [C++ MVP] on 9 Aug 2007 17:05 "Gerry Hickman" <gerry666uk(a)newsgroup.nospam> wrote in message news:%23PFJQUs2HHA.4712(a)TK2MSFTNGP04.phx.gbl... > Hi, > > In an earlier thread entitled "STL vector - which style of for() loop?" I > had a function that could populate a vector (with a variable number of > strings) and pass it back to the caller, but people pointed out this would > create a "copy" of the vector and it may be better to pass by reference. > Doug Harrison offered this example: > > ----- example start ----- > > I would use pass-by-reference to avoid this needless cost, e.g. > > vector<string>::size_type > void GetDeviceClasses(vector<string>& guids) > { > guids.clear(); > // If you can estimate n, reserve can eliminate reallocations. > // guids.reserve(n); > ... > returns guids.size(); > } > > ----- example end ----- > > but when I came to actually code this, I ran into some problems. I managed > to code something that appears to achieve the objective, but my code is > almost "back-to-front" (in terms of * and &) of what Doug posted. Can > someone clarify? What you have looks perfectly fine. You should also either test for NULL or else write documentation comments indicating that NULL is an illegal value. > > ----- my attempt ----- > > using namespace std; // just for this demo > > vector<string> guids; > PopulateStrings(&guids); > cout << "Count of guids is now " << guids.size(); // prints 2 > > void PopulateStrings(vector<string> * guids) > { > guids->clear(); > guids->push_back("test1"); > guids->push_back("test2"); > } > > ----- end my attempt ----- > > -- > Gerry Hickman (London UK)
From: Charles Wang[MSFT] on 9 Aug 2007 22:58
Hi Gerry, Could you please let us know what the problems you ran into if you used Doug's suggestion? It should also work if you use reference as following: =================================== vector<string> guids; PopulateStrings(guids); cout << "Count of guids is now " << guids.size(); // prints 2 void PopulateStrings(vector<string> & guids) { guids.clear(); guids.push_back("test1"); guids.push_back("test2"); } ================================== Best regards, Charles Wang Microsoft Online Community Support ===================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from this issue. ====================================================== This posting is provided "AS IS" with no warranties, and confers no rights. ====================================================== |