Hello
I've got following container:
std::vector<boost::shared_ptr<std::string strings;
strings.push_back( boost::shared_ptr<std::string( new
std::string("AAA") ) );
strings.push_back( boost::shared_ptr<std::string( new
std::string("BBB") ) );
strings.push_back( boost::shared_ptr<std::string( new
std::string("BBB") ) );
std::for_each( strings.begin(), strings.end(),
std::mem_fun( &std::length ) );
Now I want a 0-parameter method from std::string to be called on every
element from the sequence. I tried to use std::mem_fun (as above), but
it claims as follows:
>cannot convert parameter 1 from 'boost::shared_ptr<T>' to 'const std::basic_string<_Elem,_Traits,_Ax*'
The question is - how to construct a correct adapter for it, not using
a custom operand class. I want to use only stl stuff.
Thanks 5 1973
Hello,
I don't know how you can solve the problem,
but at a first glance I'm afraid that a
smart pointer (boost::shared_ptr in your case)
cannot be used where a "real" pointer is needed
(but I'm not sure).
The compiler tells that he expects an object of
type 'const std::string *' (std::string is just an alias
for std::basic_string<char>), he found an object
of type boost::shared_ptr<string>.
I feel that a smart pointer acts _logically_ as a pointer,
but _is not_ a pointer...
Maybe you've already tried, but I remark that your
code compiles fine if you substitute
boost::shared_ptr<std::stringwith string*, i.e.
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
int main() {
std::vector<std::string* strings;
strings.push_back( (new std::string("AAA") ) );
strings.push_back( (new std::string("BBB") ) );
strings.push_back( (new std::string("BBB") ) );
std::for_each( strings.begin(), strings.end(),
std::mem_fun( &std::string::length ) );
}
On Aug 20, 4:55*am, dominolog <dominiktomc...@gmail.comwrote:
I've got following container:
std::vector<boost::shared_ptr<std::string strings;
strings.push_back( boost::shared_ptr<std::string( new
std::string("AAA") ) );
strings.push_back( boost::shared_ptr<std::string( new
std::string("BBB") ) );
strings.push_back( boost::shared_ptr<std::string( new
std::string("BBB") ) );
std::for_each( strings.begin(), strings.end(),
std::mem_fun( &std::length ) );
Now I want a 0-parameter method from std::string to be called on every
element from the sequence. I tried to use std::mem_fun (as above), but
it claims as follows:
cannot convert parameter 1 from 'boost::shared_ptr<T>' to 'const std::basic_string<_Elem,_Traits,_Ax*'
The question is - how to construct a correct adapter for it, not using
a custom operand class. I want to use only stl stuff.
Since you're using Boost (or TR1), you can use boost::mem_fn (or
std::tr1::mem_fn) or boost::bind (or std::tr1::bind):
std::for_each( strings.begin(), strings.end(),
boost::mem_fn(&std::string::length));
Or
std::for_each( strings.begin(), strings.end(),
std::tr1::mem_fn(&std::string::length));
Or
std::for_each( strings.begin(), strings.end(),
boost::bind(&std::string::length, _1));
Or
std::for_each( strings.begin(), strings.end(),
std::tr1::bind(&std::string::length, std::tr1::placeholders::_1));
Cheers! --M
On Aug 20, 10:06*am, Giovanni Gherdovich
<gherdov...@students.math.unifi.itwrote:
[snip]
Maybe you've already tried, but I remark that your
code compiles fine if you substitute
boost::shared_ptr<std::stringwith string*, i.e.
[snip]
Sure, but one of the great things about shared_ptr (and some other
smart pointers) is that it allows easy use of pointers in STL
containers. Switching to raw pointers eliminates automatic cleanup
(especially important in the face of exceptions), shared ownership,
etc., which the OP may need. Using RAII techniques, like that
implemented by shared_ptr, is one of the best ways to eliminate memory
(and other resource) leaks. See Sutter and Alexandrescu, _C++ Coding
Standards_, Item 13.
Cheers! --M
Cheers! --M
On 20 Sie, 17:20, mlimber <mlim...@gmail.comwrote:
On Aug 20, 10:06*am, Giovanni Gherdovich<gherdov...@students.math.unifi..itwrote :
[snip]Maybe you've already tried, but I remark that your
code compiles fine if you substitute
boost::shared_ptr<std::stringwith string*, i.e.
[snip]
Sure, but one of the great things about shared_ptr (and some other
smart pointers) is that it allows easy use of pointers in STL
containers. Switching to raw pointers eliminates automatic cleanup
(especially important in the face of exceptions), shared ownership,
etc., which the OP may need. Using RAII techniques, like that
implemented by shared_ptr, is one of the best ways to eliminate memory
(and other resource) leaks. See Sutter and Alexandrescu, _C++ Coding
Standards_, Item 13.
Cheers! --M
Cheers! --M
Thanks
I haven't considered boost::mem_fun_ref. It works. In terms of
string*, it was an example, I actually use more havy classes in my
containers but for simplicity provided an example with
shared_ptr<string>.
Anyway thanks.
dominolog
Hello,
On Aug 20, 5:20 pm, mlimber <mlim...@gmail.comwrote:
>
Sure, but one of the great things about shared_ptr (and some other
smart pointers) is that it allows easy use of pointers in STL
containers. Switching to raw pointers eliminates automatic cleanup
(especially important in the face of exceptions), shared ownership,
etc., which the OP may need. Using RAII techniques, like that
implemented by shared_ptr, is one of the best ways to eliminate memory
(and other resource) leaks. See Sutter and Alexandrescu, _C++ Coding
Standards_, Item 13.
Thank you for the reference to Sutter & Alexandrescu!
Giovanni This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Senthilvel Samatharman |
last post by:
Hi,
I'm trying to learn a few Algorithms and function objects
But when i try the following program i get an error which i could not
understand.
Can you please help...
Regards,
Senthil.
|
by: Griff |
last post by:
#include <iostream>
using namespace std;
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
template<class C>void PrintAll(C&v)
{
|
by: Old Wolf |
last post by:
I have a member function that acts on an object. I would also like to
have a member function that acts on a container of such objects,
using std::for_each. I tried:
#include <algorithm>...
|
by: franklini |
last post by:
hello people,
just wanted to say thanks again for the help in the past. i have a new
problem which am wondering if any body can help me with.
i have written this abtract class shape and its...
|
by: flyaflya |
last post by:
i use a class static member function to for_each:
class t1
{
static test(string s);
}
main()
{
list<string> slist;
...
|
by: Tony Johansson |
last post by:
Hello!
I have some problem with STL function remove
I have two classes called Handle which is a template class and Integer which
is not a template class. The Integer class is just a wrapper...
|
by: Plissken.s |
last post by:
I have a template function which print out the content of a list: The
following compiles:
void PrintInt2 (int i) {
cout << i << " ";
}
template <class T>
void printList(T& list) {
|
by: learning |
last post by:
I am trying to learn STL but got stuck in for_each().
What I intend to do in the following is to make a list with each
element as a string.
add the string elements to the list until it has 10...
|
by: want.to.be.professer |
last post by:
We know that alomost every algorithm function, such as for_each,
find_if, use funcional as well as function pointer. But when I want
to use another class's member function, how could I do?
See...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |