
www.Usenet.com
| <-- __Chronological__ --> | <-- __Thread__ |
Hi All,
I am trying to put every element of set A into set B. Sounds easy
enough, right? I check my documentation, and find that set has a
templated insert function that takes two iterators. So I write:
set<string> A,B;
B.insert(A.begin(), A.end());
and VC++6 answers with:
error C2664: [can't convert iterator to string]
No constructor could take the source type, or constructor
overload resolution was ambiguous.
To check my sanity, I try it with 7.1, where it works (but I can't
switch compilers for this project right now). Undaunted, I decide to
try to combine for_each and mem_fun. The fact that insert is
overloaded trips me up for a little bit, but I define a function
pointer to get the one I want. My attempt looks like this:
typedef pair<set<string>::iterator, bool>
(set<string>::*InsertFunc)(const set<string>::value_type&); /*
typedef for the insert I want */
InsertFunc pFunc = set<string>::insert; /* pFunc points to the
member function I want to call */
std::for_each( B.begin(), B.end() ,
std::bind1st( std::mem_fun( &A->*pFunc ) , A) ) ;
It compiles, but generates no code beyond the assignment to pFunc.
Which leads me to think that I've been bitten by the "looks like a
declaration" gotcha that I've read about. Is that the case? I've
tried adding parentheses like in "Exceptional C++", but to no avail.
I've tried this out on other compilers, and they don't accept it
(VC7.1 can't deduce template arguments for mem_fun).
It's not a big deal since I can write the for loop myself, but I'd be
interested in hearing how the experts would use <algorithm> in a
situation like this.
Thanks,
Brian
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
| <-- __Chronological__ --> | <-- __Thread__ |