473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

STL: container's values setup by another container

What I want to perform is calling a member function of container 1
(CRunner), using as argument the value of a container 2 (CNames). The
purpose is to transfer values from C2 into C1 using a specific C1 member
function. Both containers contains the same number of elements (or at least,
C1 is larger than C2).

As suggested by S. Meyers (and others), I would like to use an STL algorithm
instead of a custom loop. Let say I have the following:

class CRunner{
string m_name;
int m_order;

public:
bool SetOrder(int a_order) {m_order = a_order; return true;}
bool SetName(const string& a_name) {m_name = a_name; return true;}
};

vector<string> NameTable;
vector<CRunner> RunnerTable;

I created a template functor/function pair to be used in for_each():

template<class R, class T>
class mem_fun_iter_t : unary_function<T*, R>
{
public:
explicit mem_fun_iter_t(R (T::*p)(const T::iterator::value_type&),
vector<T>::iterator i) : pmf(p), iter(i) {}
R operator()(T* p) const {return (p->*pmf)(*iter++);}
private:
R (T::*pmf)(const T::iterator::value_type&);
vector<T>::iterator iter;
};

template<class R, class T>
inline static mem_fun_iter_t<R, T> mem_fun_iter(R (T::*p)(const
T::iterator::value_type&), vector<T>::iterator i)
{
return mem_fun_iter_t<R, T, vector<T> >(p, i);
}

(...unfortunetaly, I wasn't able to specify a generic parameter for
'vector', without getting all other kinds of errors...)

So that once both containers are initalized and filled, I can safely call:

for_each(NameTable.begin(), NameTable.end(), mem_fun_iter(CRunner::SetName,
RunnerTable.begin()));

But my compiler always generate this error:

[...] error C2784: 'mem_fun_iter_t<R,T> mem_fun_iter(R (T::*)(const
generic-type-264 &),generic-type-265)': could not deduce template argument
for '<Unknown>' from 'bool (RUNNER::*)(const string &)'

Is it my compiler (MSVS6) or is it a real deduction problem (missing
parameter)?
Is it a problem of algorithm? Is there any way to do the same with another
algorithm (such as transform(), etc.)?
Is there any way to reuse mem_fun* (with a binder or not) instead of the
above custom template functor/function pair?
Is there a way to make the call "end-of-container"-proof by using
back_inserter()?

I ask the those questions because I tried all those combinations without
success.
The task look simple, but caused me frustrations over the past 3 days.

As a second part, what would be the implications if rather than having
'vector<string> NameTable' I had:

vector<CNames> NameTable;

with:

class CNames
{
//...
public:
string GetName();
//...
};

Thanks for helping.
Jul 22 '05 #1
2 1540

"Maitre Bart" <le*****@cae.com> schrieb im Newsbeitrag
news:c0**********@dns3.cae.ca...
What I want to perform is calling a member function of container 1
(CRunner), using as argument the value of a container 2 (CNames). The
purpose is to transfer values from C2 into C1 using a specific C1 member
function. Both containers contains the same number of elements (or at least, C1 is larger than C2).

As suggested by S. Meyers (and others), I would like to use an STL algorithm instead of a custom loop.


<snip/>
IMHO it would be ok to use std::transform. You can either use it to collect
the results of the function calls, or just ignore the results:

#include <vector>
#include <string>
#include <algorithm>
#include <functional>
struct Xox
{
bool Foo(const std::string& s)
{
std::cout << "Foo( " + s + ")" << std::endl;
return s.size()>3;
}
};
// an iterator that just eats everything up
template<class CONTAINER>
struct dummy_insert_iterator
{
dummy_insert_iterator<CONTAINER>& operator++(){ return *this; }
dummy_insert_iterator<CONTAINER> operator++(int){ return *this; }
dummy_insert_iterator<CONTAINER>& operator*(){ return *this; }
dummy_insert_iterator<CONTAINER>& operator=(
typename CONTAINER::const_reference Val){return *this;}
};
template<class CONTAINER> inline
dummy_insert_iterator<CONTAINER> black_hole_back_inserter(const
CONTAINER&)
{
return dummy_insert_iterator<CONTAINER>();
}
int main()
{
std::vector<Xox> x;
x.push_back(Xox());
x.push_back(Xox());
std::vector<std::string> s;
s.push_back("tst1");
s.push_back("t2");
// ... variant 1: collect results
std::vector<bool> res;
std::transform(x.begin(), x.end(), s.begin(), std::back_inserter(res),
std::mem_fun_ref(Xox::Foo)
);
// ... variant 2: don't care for results:
std::transform(x.begin(), x.end(), s.begin(),
black_hole_back_inserter(res),
std::mem_fun_ref(Xox::Foo)
);
return 0;
}

HTH,
Andy
Jul 22 '05 #2

"Andreas Müller" <me@privacy.net> wrote in message
news:c0*************@ID-83644.news.uni-berlin.de...

"Maitre Bart" <le*****@cae.com> schrieb im Newsbeitrag
news:c0**********@dns3.cae.ca...
What I want to perform is calling a member function of container 1
(CRunner), using as argument the value of a container 2 (CNames). The
purpose is to transfer values from C2 into C1 using a specific C1 member
function. Both containers contains the same number of elements (or at least,
C1 is larger than C2).

As suggested by S. Meyers (and others), I would like to use an STL

algorithm
instead of a custom loop.


<snip/>
IMHO it would be ok to use std::transform. You can either use it to

collect the results of the function calls, or just ignore the results:

#include <vector>
#include <string>
#include <algorithm>
#include <functional>
struct Xox
{
bool Foo(const std::string& s)
{
std::cout << "Foo( " + s + ")" << std::endl;
return s.size()>3;
}
};
// an iterator that just eats everything up
template<class CONTAINER>
struct dummy_insert_iterator
{
dummy_insert_iterator<CONTAINER>& operator++(){ return *this; }
dummy_insert_iterator<CONTAINER> operator++(int){ return *this; }
dummy_insert_iterator<CONTAINER>& operator*(){ return *this; }
dummy_insert_iterator<CONTAINER>& operator=(
typename CONTAINER::const_reference Val){return *this;}
};
template<class CONTAINER> inline
dummy_insert_iterator<CONTAINER> black_hole_back_inserter(const
CONTAINER&)
{
return dummy_insert_iterator<CONTAINER>();
}
int main()
{
std::vector<Xox> x;
x.push_back(Xox());
x.push_back(Xox());
std::vector<std::string> s;
s.push_back("tst1");
s.push_back("t2");
// ... variant 1: collect results
std::vector<bool> res;
std::transform(x.begin(), x.end(), s.begin(), std::back_inserter(res),
std::mem_fun_ref(Xox::Foo)
In fact, "std::mem_fun1_ref" worked on my side.
);
// ... variant 2: don't care for results:
std::transform(x.begin(), x.end(), s.begin(),
black_hole_back_inserter(res),
std::mem_fun_ref(Xox::Foo)
);
return 0;
}

HTH,
Andy


Thanks for your precious help.

BTW, did you get experienced by yourself with this stuf, or did you read a
good book on putting in practice stl algorithms?
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Maitre Bart | last post by:
First, I describe my setup in 3 points. Then I describe what I want to do, and describe the error I get. 1) I have a container GTTable container pointer to data (class GT *). 2) I made a...
22
by: Claudio Jolowicz | last post by:
Is it possible to store unique objects in an STL container? Suppose an object of class C is unique: class C { public: C() {} ~C() {} private:
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
4
by: Marko.Cain.23 | last post by:
How can I copy elements from one STL container to another STL container if a condition is met and if it find an element fails that condition, it stops the copying? I can't use the original...
10
by: Steve Edwards | last post by:
Hi, I'm using a map with the key of type string, and value of type int. typedef map<string, int, less<string> >concordance; I'm finding words within some text and keeping a count of their...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
4
by: Daniel Marques | last post by:
I would like to know how to get the address of a container of the STL without using iterators. I have a class with three STL lists and a index which points to an element of any ot the other lists,...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.