c++ - Return an iterator to an STL container from a template function -
I was trying to return a vector to a template function with a template function (not yet template class members - I'm still writing that the compiler kept me errors (originally copied below to facilitate Google searches). I basically knew what was the problem, but the exact syntax was elusive.
I've read the Internet, searched the SO, but did not find any answer in it which worked. I thought I should ask questions and answer myself here.
(signed) The original code is as follows:
#include & lt; Vector & gt; # Include & lt; Algorithm & gt; // std :: low_bound template & lt; Typename T & gt; Std :: vector & lt; T & gt; :: Iterator insertIntoVector (std :: vector & lt; T & gt; vec, const T & amp; val) {itr = [some study :: vector & lt; T & gt; Iterator]; Return IRR; // return iterator back to val in vec} // insert insert intersector ();
Compiler errors:
error C2145: syntax error: missing ';' Identifier 'insertIntoVector' error before C2065: 'T': Undeclared identifier error C2923: 'std :: vector': 'T' for parameter '_Ty' is not a valid template type argument
Being intelligent, I tried this:
template & lt; Typename T & gt;
More compiler errors:
error C1075: end of file found before left bamboo '{'
If this question is unlocked then I will post my answer below. Otherwise, see my answer.
Oddly, I had to add the parentheses around the return type to get it. This is,
template (Typename std :: vector & Lt; T & gt; :: iterator) // test - as expected
is used continuously, but
template
I have been given problems on MSVC 2013, but compiled on MSVC 2012. I do not know why any?
The code given below is available on MSWC 2013
// is based on the article of Matt Austerne, // "Set up what you should not use (and what you should do instead)" // (Http://lafstern.org/matt/col1.pdf ) // I have modified it a bit, mostly just refatting and adding comments, // but I changed it to return an intractor to the inserted element. // Also the sorted_ vector has been converted into sorted vector (semelcase) #include & lt; Vector & gt; # Include & lt; Algorithm & gt; // std :: low_bound template & lt; Typename T & gt; (Typename std :: vector & gt; :: iterator) // test - works as expected InsertIntoVector (std :: vector & lt; T & gt; & vec, const T & amp; Val) {// Insert in vector vec, such that v remains in ordered order / and all elements of vec are unique (like a set). // It only makes sense that vector elements // are created in indexed order. // A sorted vector can perform better than one set if // is smaller than // // smaller operation is accessible, // faster access through smaller storage, [...] .. // // very slow entry of the cost) Note: Type T has a defined & lt; Operator /// Todo: To pass a comparison () /// function indicator, surcharge this function. // std :: low_bound () returns log2 (N) + O (1) display Typename std :: vector & lt; T & gt; :: Iterator IRR = Bottom_bound (vec.begin (), vec.end (), Val); If return (return to iterator) to return to val (; vec.end () == IRR) ((val & lt; * itr)} {itr = vec.inert (itr, val)}} Return to IRR; // vec} // Insert end of the detector ();
Comments
Post a Comment