template problems

Gokhan Kisacikoglu kisa@centropolisfx.com
Fri Jul 26 11:08:00 GMT 2002


I am not sure you can pass address of template functions, especially
when these functions are relative to a certain instance (not static), I
don't know how it would all work. Is this described in the ISO spec? I
am not sure... Maybe somebody can tell us more...

> wanted to write an assoc_insert_iterator to insert a value into a map
> like a back_inserter (gives a back_insert_iterator that) inserts into a
> list, for example.  The iterator needs to be parameterized with some X

You can already do this, if you have your value_types (pair of key and
data) stored in an array let's say, that you want to insert them into a
map by using the insert_iterator, you could easily type the following;

#include <map>
#include <vector>
#include <iterator>
#include <string>
#include <iostream>

using namespace std; 	// for simplicity

// a simple mapping
//
typedef pair <int, string> int_string_pair;

typedef map <int_string_pair :: first_type, 
	    int_string_pair :: second_type> mapped_strings;

// a simple array (let's say)
//	
typedef vector <int_string_pair> map_value_array;

int main(void)
{
    // instances
    //
    
    // value in an arrays
    //	
    map_value_array array;
    
    array.reserve(3);
    array.push_back( make_pair( 1, "one" ) );
    array.push_back( make_pair( 3, "three" ) );	// randomly ordered
    array.push_back( make_pair( 2, "two" ) );
    
    // insert into a map instance
    //	
    mapped_strings mstrs;
    
    copy( array.begin(), array.end(), 
	insert_iterator <mapped_strings> (mstrs, mstrs.end()) );
    
    mapped_strings :: iterator iter = mstrs.begin();
    cerr << iter->first << " " << iter->second << endl; ++ iter;
    cerr << iter->first << " " << iter->second << endl; ++ iter;
    cerr << iter->first << " " << iter->second << endl; ++ iter;
    
    assert( iter == mstrs.end() );
    
    return 0;
}

check out:

http://www.sgi.com/tech/stl/insert_iterator.html

The second parameter is the hint to tell where to start the search for
the appropriate insertion location in the case of map, it would be the
exact insertion point in the case of list for example...

HTH,
Gokhan



More information about the Gcc-help mailing list